LOGO

RestFrames  v1.0.1
RestFrames HEP Event Analysis Software Library
LabGenFrame.cc
Go to the documentation of this file.
1 // RestFrames: particle physics event analysis library
3 // --------------------------------------------------------------------
4 // Copyright (c) 2014-2016, Christopher Rogan
14 // This file is part of RestFrames.
15 //
16 // RestFrames is free software; you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation; either version 2 of the License, or
19 // (at your option) any later version.
20 //
21 // RestFrames is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU General Public License for more details.
25 //
26 // You should have received a copy of the GNU General Public License
27 // along with RestFrames. If not, see <http://www.gnu.org/licenses/>.
29 
31 
32 namespace RestFrames {
33 
35  // LabGenFrame class
37  LabGenFrame::LabGenFrame(const std::string& sname,
38  const std::string& stitle) :
39  LabFrame<GeneratorFrame>(sname, stitle)
40  {
41  m_PT = 0.;
42  m_PToM = -1.;
43  m_PL = 0.;
44  m_Phi = -1.;
45 
46  m_MaxM = -1.;
47 
48  m_NBurnInMCMC = 1000;
49  m_NDiscardMCMC = 5;
50 
51  m_FailTolerance = 1000;
52  }
53 
54  LabGenFrame::~LabGenFrame() {}
55 
58  }
59 
60  void LabGenFrame::SetThreeVector(const TVector3& P){
61  m_PT = P.Pt();
62  m_PL = P.Z();
63  SetPhi(P.Phi());
64  }
65 
66  void LabGenFrame::SetPToverM(double val){
67  if(val < 0.){
68  m_Log << LogWarning;
69  m_Log << "Unable to set transverse momentum ";
70  m_Log << "to negative value: " << val << "*mass";
71  m_Log << LogEnd;
72  } else {
73  m_PToM = val;
74  m_PT = 0.;
75  }
76  }
77 
79  if(val < 0.){
80  m_Log << LogWarning;
81  m_Log << "Unable to set transverse momentum ";
82  m_Log << "to negative value: " << val;
83  m_Log << LogEnd;
84  } else {
85  m_PT = val;
86  m_PToM = -1.;
87  }
88  }
89 
91  m_PL = val;
92  }
93 
94  void LabGenFrame::SetPhi(double val){
95  while(val > acos(-1.)*2.) val -= acos(-1.)*2.;
96  while(val < 0.) val += acos(-1.)*2.;
97  m_Phi = val;
98  }
99 
100  void LabGenFrame::ResetGenFrame(){
101  SetSpirit(false);
102  m_Phi = -1.;
103  }
104 
106  m_Log << LogVerbose << "Initializing this tree for analysis..." << LogEnd;
107 
108  if(!IsSoundBody()){
109  UnSoundBody(RF_FUNCTION);
110  return SetMind(false);
111  }
112 
114  m_Log << LogWarning << "...Unable to recursively initialize analysis" << LogEnd;
115  return SetMind(false);
116  }
117 
118  for(int i = 0; i < m_NBurnInMCMC; i++)
119  if(!IterateRecursiveMCMC()){
120  m_Log << LogWarning << "...Unable to recursively initialize analysis" << LogEnd;
121  return SetMind(false);
122  }
123 
124  m_Log << LogVerbose << "...Done" << LogEnd;
125  return SetMind(true);
126  }
127 
129  SetMind(false);
130  m_NBurnInMCMC = std::max(0,N);
131  }
132 
134  SetMind(false);
135  m_NDiscardMCMC = std::max(1,N);
136  }
137 
139  m_FailTolerance = Nfail;
140  }
141 
142  bool LabGenFrame::InitializeGenAnalysis(){
143  if(!IsSoundBody()){
144  UnSoundBody(RF_FUNCTION);
145  return SetMind(false);
146  }
147 
148  GeneratorFrame& child = GetChildFrame();
149  if(child.IsVariableMassMCMC()){
150  double ChildMass, ChildProb;
151  child.GenerateMassMCMC(ChildMass, ChildProb, m_MaxM);
152  m_ChildMassMCMC = ChildMass;
153  m_ChildProbMCMC = ChildProb;
154  SetMassMCMC(ChildMass, child);
155  } else {
156  m_ChildMassMCMC = child.GetMass();
157  m_ChildProbMCMC = 1.;
158  }
159 
160  return SetMind(true);
161  }
162 
163  bool LabGenFrame::IterateMCMC(){
164  GeneratorFrame& child = GetChildFrame();
165  if(child.IsVariableMassMCMC()){
166  double ChildMass, ChildProb = 0.;
167  child.GenerateMassMCMC(ChildMass, ChildProb, m_MaxM);
168 
169  double probOld = GetProbMCMC(m_ChildMassMCMC)*
170  child.GetProbMCMC(m_ChildMassMCMC)/m_ChildProbMCMC;
171 
172  double probNew = GetProbMCMC(ChildMass)*
173  child.GetProbMCMC(ChildMass)/ChildProb;
174 
175  if(probNew/probOld > GetRandom()){
176  m_ChildMassMCMC = ChildMass;
177  m_ChildProbMCMC = ChildProb;
178  SetMassMCMC(ChildMass, child);
179  } else {
180  SetMassMCMC(m_ChildMassMCMC, child);
181  }
182  }
183 
184  return SetMind(true);
185  }
186 
187  bool LabGenFrame::GenerateFrame(){
188  if(!IsSoundBody())
189  return false;
190 
191  TLorentzVector P;
192  double M = GetChildFrame().GetMass();
193  if(m_Phi < 0.) m_Phi = 2.*acos(-1.)*GetRandom();
194 
195  if(m_PToM > 0.)
196  P.SetPxPyPzE(m_PToM*M*cos(m_Phi), m_PToM*M*sin(m_Phi), m_PL,
197  sqrt(m_PL*m_PL + M*M*(1. + m_PToM*m_PToM)));
198  else
199  P.SetPxPyPzE(m_PT*cos(m_Phi), m_PT*sin(m_Phi), m_PL,
200  sqrt(m_PT*m_PT + m_PL*m_PL + M*M));
201  m_Phi = -1.;
202 
203  std::vector<TLorentzVector> ChildVector;
204  ChildVector.push_back(P);
205  SetChildren(ChildVector);
206 
207  return SetSpirit(true);
208  }
209 
211  SetSpirit(false);
212  if(!IsSoundBody())
213  return false;
214  return ClearEventRecursive();
215  }
216 
218  bool pass = false;
219  int tries = 0;
220 
221  while(!pass){
222  for(int i = 0; i < m_NDiscardMCMC; i++)
223  if(!IterateRecursiveMCMC())
224  return SetSpirit(false);
225 
226  if(!AnalyzeEventRecursive()){
227  return SetSpirit(false);
228  }
229 
230  pass = EventInAcceptance();
231 
232  if(!pass){
233  tries++;
234  if(tries > m_FailTolerance &&
235  m_FailTolerance > 0){
236  m_Log << LogWarning;
237  m_Log << "Failed to generate event in ";
238  m_Log << "acceptance in " << tries;
239  m_Log << " tries. Giving up." << LogEnd;
240  return SetSpirit(false);
241  }
242  }
243  }
244 
245  return SetSpirit(true);
246  }
247 
248 }
RestFrames::LabGenFrame::SetPToverM
virtual void SetPToverM(double val)
Sets transverse momentum over mass.
Definition: LabGenFrame.cc:66
RestFrames::LabGenFrame::AnalyzeEvent
virtual bool AnalyzeEvent()
Analyzes event.
Definition: LabGenFrame.cc:217
RestFrames::LabGenFrame::Clear
virtual void Clear()
Clears LabGenFrame from all connections to other objects
Definition: LabGenFrame.cc:56
RestFrames::LabGenFrame::SetPhi
virtual void SetPhi(double val)
Sets phi of this frame.
Definition: LabGenFrame.cc:94
RestFrames::LabGenFrame::SetFailTolerance
void SetFailTolerance(int N)
Sets tolerance number of fail events before stopping the generation.
Definition: LabGenFrame.cc:138
RestFrames::GeneratorFrame
Definition: GeneratorFrame.hh:42
RestFrames::LabGenFrame::SetThreeVector
virtual void SetThreeVector(const TVector3 &P)
Sets momentum's three-vector of this frame.
Definition: LabGenFrame.cc:60
RestFrames::GeneratorFrame::GetRandom
double GetRandom() const
Returns random value.
Definition: GeneratorFrame.cc:193
RestFrames::LabGenFrame::SetLongitudinalMomentum
virtual void SetLongitudinalMomentum(double val)
Sets longitudinal momentum of this frame.
Definition: LabGenFrame.cc:90
RestFrames::GeneratorFrame::AnalyzeEventRecursive
bool AnalyzeEventRecursive()
Recursively analyze event in this frame and its children.
Definition: GeneratorFrame.cc:133
RestFrames::LabGenFrame::SetN_MCMCDiscard
void SetN_MCMCDiscard(int N)
Sets which Nth events to keep.
Definition: LabGenFrame.cc:133
RestFrames::LabGenFrame::ClearEvent
virtual bool ClearEvent()
Clears event.
Definition: LabGenFrame.cc:210
RestFrames::GeneratorFrame::GetChildFrame
virtual GeneratorFrame & GetChildFrame(int i=0) const
Returns the frame of the i th child.
Definition: GeneratorFrame.cc:112
LabGenFrame.hh
RestFrames::LabGenFrame::LabGenFrame
LabGenFrame(const std::string &sname, const std::string &stitle)
Standard constructor.
Definition: LabGenFrame.cc:37
RestFrames::LabGenFrame::SetTransverseMomentum
virtual void SetTransverseMomentum(double val)
Sets transverse momentum of this frame.
Definition: LabGenFrame.cc:78
RestFrames::LabGenFrame::InitializeAnalysis
virtual bool InitializeAnalysis()
Initialize analysis capability of a tree to generate events.
Definition: LabGenFrame.cc:105
RestFrames::GeneratorFrame::GetProbMCMC
virtual double GetProbMCMC(double mass=-1.) const
Evaluates probability of the state is in right now or the probablility of a state it could go in.
Definition: GeneratorFrame.cc:269
RestFrames::GeneratorFrame::GeneratorFrame
GeneratorFrame()
Empty constructor.
Definition: GeneratorFrame.cc:72
RestFrames::LabFrame
Definition: LabFrame.hh:41
RestFrames::GeneratorFrame::InitializeAnalysisRecursive
bool InitializeAnalysisRecursive()
Recursively initialize this frame and its children for analysis.
Definition: GeneratorFrame.cc:169
RestFrames::LabGenFrame::SetN_MCMCBurnIn
void SetN_MCMCBurnIn(int N)
Sets number of events to be thrown away before keeping any.
Definition: LabGenFrame.cc:128
RestFrames::GeneratorFrame::ClearEventRecursive
bool ClearEventRecursive()
Recursively clear event information from this frame and its children.
Definition: GeneratorFrame.cc:120
RestFrames::GeneratorFrame::GetMass
virtual double GetMass() const
Get the mass of this frame.
Definition: GeneratorFrame.cc:88
RestFrames::GeneratorFrame::Clear
virtual void Clear()
Clears GeneratorFrame of all connections to other objects.
Definition: GeneratorFrame.cc:79