EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EventFactory.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file EventFactory.cxx
1 
11 
12 #include <memory>
13 #include <stdexcept>
14 #include <string>
15 
16 #include <TClass.h>
17 #include <TProcessID.h>
18 
31 #include "eicsmear/functions.h" // For getFirstNonBlank()
35 
36 #include <TVector3.h>
37 #include <TParticlePDG.h>
38 #include <TLorentzVector.h>
39 #include <TDatabasePDG.h>
40 
41 #include<map>
42 
43 using std::cout;
44 using std::cerr;
45 using std::endl;
46 using std::map;
47 
48 namespace erhic {
49 
50  template<typename T>
52  return !mLine.empty()
53  && mLine.find("finished") != std::string::npos;
54  }
55 
56  // Use this struct to automatically reset TProcessID object count.
58  // Initialse object with current TProcessID object count.
60  count = TProcessID::GetObjectCount();
61  }
62  // Restore object count to the value at initialisation.
63  // See example in $ROOTSYS/test/Event.cxx
64  // To save space in the table keeping track of all referenced objects
65  // we assume that our events do not address each other.
67  TProcessID::SetObjectCount(count);
68  }
69  int count;
70  };
71 
72  template<typename T>
74  // Save current object count. Will reset it when this function returns.
75  TProcessIdObjectCount objectCount;
76  mEvent.reset(new T);
77  // We use this flag to check input doesn't end mid-event.
78  // Initialised finished flag to "success" in case of no input.
79  int finished(0);
80  std::string error;
81  // Read line-by-line until the stream is not good or we break out.
82  while (std::getline(*mInput, mLine).good()) {
83  // Reached end-of-event marker
84  if (AtEndOfEvent()) {
85  // If we built a good event (i.e. no errors reading strings)
86  // perform end-of-event calculations.
87  if (!error.empty()) {
88  throw std::runtime_error(error);
89  } else {
90  finished = FinishEvent(); // 0 upon success
91  break;
92  } // if
93  } else if ('0' == getFirstNonBlank(mLine)) {
94  // '0' indicates the event header line
95  // An event started, set finished flag to "unfinished".
96  finished = -1;
97  // Parse string and check for validity.
98  if (!mEvent->Parse(mLine)) {
99  // Set error message based on bad event input.
100  // Don't break out of the loop yet, so that we continue reading
101  // lines until the end-of-event marker. That way we stay
102  // "aligned" with the input data ready for the next event.
103  error = "Bad event input: " + mLine;
104  } // if
105  } else if ('=' != getFirstNonBlank(mLine)) {
106  // Anything remaining other than a line of '=' is a particle line
107  // Don't raise an exception for a failed track, as the event in
108  // general may be OK. AddParticle will print a message though.
109  if (!AddParticle()) {
110  error = "Bad particle input in event";
111  } // if
112  } // if
113  } // if
114  // Check for events that started but did not finish.
115  // We should have ended with an end-of-event marker, meaning the finished
116  // flag is set to zero. If not, the finished flag will be non-zero.
117  if (finished != 0) {
118  mEvent.reset(NULL);
119  throw std::runtime_error("Ended mid-event");
120  } // if
121  // Return a NULL event *without* throwing an exception to indicate
122  // end-of-file. We shouldn't have hit eof if we have read a good event,
123  // as we won't have yet tried to read past the end (mLine will still be
124  // at the end-of-file marker line).
125  if (mInput->eof()) {
126  mEvent.reset(NULL);
127  } // if
128  return mEvent.release();
129  }
130 
131  template<typename T>
133  // First, find the beams, exchange boson, scattered lepton.
134  // if this isn't a DIS event, skip Kinematics computation
135  BeamParticles beams;
136  if (!ParticleIdentifier::IdentifyBeams(*mEvent, beams)) {
137  return 0; // must not signal error.
138  // std::cerr << "EventFromAsciiFactory::FinishEvent(): failed to find beams" << std::endl;
139  // return -1;
140  } // if
141 
142  std::unique_ptr<DisKinematics> nm( LeptonKinematicsComputer(*mEvent).Calculate());
143  std::unique_ptr<DisKinematics> jb( JacquetBlondelComputer(*mEvent).Calculate());
144  std::unique_ptr<DisKinematics> da( DoubleAngleComputer(*mEvent).Calculate());
145  if (nm.get()) {
146  mEvent->SetLeptonKinematics(*nm);
147  } // if
148  for (unsigned n(0); n < mEvent->GetNTracks(); ++n) {
149  mEvent->GetTrack(n)->ComputeEventDependentQuantities(*mEvent);
150  } // for
151  if (jb.get()) {
152  mEvent->SetJacquetBlondelKinematics(*jb);
153  } // if
154  if (da.get()) {
155  mEvent->SetDoubleAngleKinematics(*da);
156  } // if
157 
158  // We also have to set the remaining variables not taken care of
159  // by the general DIS event kinematic computations.
160  const TLorentzVector h = beams.BeamHadron();
161  TLorentzVector l = beams.BeamLepton();
162  TLorentzVector s = beams.ScatteredLepton();
163  TVector3 boost = -h.BoostVector();
164  l.Boost(boost);
165  s.Boost(boost);
166  mEvent->SetELeptonInNuclearFrame(l.E());
167  mEvent->SetEScatteredInNuclearFrame(s.E());
168  return 0;
169  }
170 
171  template<typename T>
173  //return true;
174  try {
175  if (mEvent.get()) {
176  ParticleMC particle(mLine, mEvent->RequiresEaParticleFields()); // Throws if the string is bad
177  particle.SetEvent(mEvent.get());
178  mEvent->AddLast(&particle);
179  //ParticleMCeA *particle = new ParticleMCeA(mLine); // Throws if the string is bad
180  //particle->SetEvent(mEvent.get());
181  //mEvent->AddLast(particle);
182  //delete particle;
183  } // if
184  return true;
185  } // try
186  catch(std::exception& error) {
187  std::cerr << "Exception building particle: " << error.what() << std::endl;
188  return false;
189  }
190  }
191 
192  template<typename T>
194  return T::Class()->GetName();
195  }
196 
197  template<typename T>
199  for (int i=0; i<5; i++)
200  {
201  std::getline(*mInput,mLine);
202  }
203  }
204 
205 
206 } // namespace erhic
207 
208 namespace {
209 
210  // Need this to generate the CINT code for each version
223 
224 } // namespace