Class Reference for E1039 Core & Analysis Software
Fun4AllRUSInputManager.cc
Go to the documentation of this file.
14 #include <fun4all/Fun4AllServer.h>
17 #include <fun4all/Fun4AllUtils.h>
18 #include <fun4all/PHTFileServer.h>
19 #include <ffaobjects/RunHeader.h>
21 #include <phool/getClass.h>
22 #include <phool/PHCompositeNode.h>
23 #include <phool/PHDataNode.h>
24 #include <phool/recoConsts.h>
25 #include <cstdlib>
26 #include <memory>
27 #include <TFile.h>
28 #include <TTree.h>
29 
30 using namespace std;
31 
32 Fun4AllRUSInputManager::Fun4AllRUSInputManager(const std::string& name, const std::string& topnodename)
33  : Fun4AllInputManager(name, ""),
34  segment(-999),
35  isopen(0),
36  is_mc(false),
37  events_total(0),
38  events_thisfile(0),
39  topNodeName(topnodename),
40  _tree_name("save"),
41  run_header(nullptr),
42  spill_map(nullptr),
43  event_header(nullptr),
44  hit_vec(nullptr),
45  trk_vec(nullptr),
46  _fin(nullptr),
47  _tin(nullptr)
48 {
50  topNode = se->topNode(topNodeName.c_str());
51  syncobject = new SyncObjectv2();
52 }
53 
55  {
56  if(isopen)
57  {
58  fileclose();
59  }
60  delete syncobject;
61  }
62 
63 
66 
67  SQSpill* spill = spill_map->get(spillID);
68  if (!spill) {
69  spill = new SQSpill_v2();
70  spill->set_spill_id(spillID);
71  spill->set_run_id(runID);
72  spill_map->insert(spill);
74  }
75 
79 
80  for (int i = -16; i <= 16; ++i) {
81  event_header->set_qie_rf_intensity(i, rfIntensity[i+16]); // need to be added
82  }
83 
84  // Apply the FPGA triggers to the event header
90 
91  // Apply the NIM triggers to the event header
97 
100 
101  // Hits
102  for (size_t i = 0; i < elementID->size(); ++i) {
103  SQHit* hit = nullptr;
104 
105  if (is_mc) {
106  hit = new SQMCHit_v1();
107  hit->set_track_id(hitTrackID->at(i));
108  } else {
109  hit = new SQHit_v1();
110  }
111 
112  hit->set_hit_id(hitID->at(i));
113  hit->set_detector_id(detectorID->at(i));
114  hit->set_element_id(elementID->at(i));
115  hit->set_tdc_time(tdcTime->at(i));
116  hit->set_drift_distance(driftDistance->at(i));
117  hit_vec->push_back(hit);
118  }
119 
120 
121  // MC true Tracks
122  if (is_mc){
123  const double MUON_MASS = 0.105658; // GeV
124  for (size_t i = 0; i < gTrackID->size(); ++i) {
125  SQTrack* trk = new SQTrack_v1();
126 
127  trk->set_charge(gCharge->at(i));
128  trk->set_track_id(gTrackID->at(i));
129  trk->set_pos_vtx(TVector3(gvx->at(i), gvy->at(i), gvz->at(i)));
130 
131  {
132  TLorentzVector p4;
133  p4.SetXYZM(gpx->at(i), gpy->at(i), gpz->at(i), MUON_MASS);
134  trk->set_mom_vtx(p4);
135  }
136 
137  trk->set_pos_st1(TVector3(gx_st1->at(i), gy_st1->at(i), gz_st1->at(i)));
138  {
139  TLorentzVector p4;
140  p4.SetXYZM(gpx_st1->at(i), gpy_st1->at(i), gpz_st1->at(i), MUON_MASS);
141  trk->set_mom_st1(p4);
142  }
143 
144  trk->set_pos_st3(TVector3(gx_st3->at(i), gy_st3->at(i), gz_st3->at(i)));
145  {
146  TLorentzVector p4;
147  p4.SetXYZM(gpx_st3->at(i), gpy_st3->at(i), gpz_st3->at(i), MUON_MASS);
148  trk->set_mom_st3(p4);
149  }
150 
151  trk_vec->push_back(trk);
152  }
153  }
154 }
155 
156 int Fun4AllRUSInputManager::fileopen(const std::string &filenam) {
157  if (isopen) {
158  std::cout << "Closing currently open file "
159  << filename
160  << " and opening " << filenam << std::endl;
161  fileclose();
162  }
163  filename = filenam;
164 
165 
166  if (verbosity > 0) {
167  std::cout << ThisName << ": opening file " << filename.c_str() << std::endl;
168  }
169 
170  events_thisfile = 0;
171 
172  _fin = TFile::Open(filenam.c_str(), "READ"); // Open the file dynamically
173  if (!_fin || _fin->IsZombie()) {
174  std::cerr << "!!ERROR!! Failed to open file " << filenam << std::endl;
175  }
176 
177  _tin = (TTree*) _fin->Get(_tree_name.c_str());
178  if (!_tin) {
179  std::cerr << "!!ERROR!! Tree " << _tree_name << " not found in file " << filenam << std::endl;
180  return -1;
181  }
182  _tin->SetBranchAddress("eventID", &eventID);
183  _tin->SetBranchAddress("runID", &runID);
184  _tin->SetBranchAddress("spillID", &spillID);
185  _tin->SetBranchAddress("rfID", &rfID);
186  _tin->SetBranchAddress("turnID", &turnID);
187  _tin->SetBranchAddress("fpgaTrigger", fpgaTrigger);
188  _tin->SetBranchAddress("nimTrigger", nimTrigger);
189  _tin->SetBranchAddress("rfIntensity", rfIntensity);
190  _tin->SetBranchAddress("hitID", &hitID);
191  _tin->SetBranchAddress("hitTrackID", &hitTrackID);
192  _tin->SetBranchAddress("detectorID", &detectorID);
193  _tin->SetBranchAddress("elementID", &elementID);
194  _tin->SetBranchAddress("driftDistance", &driftDistance);
195  _tin->SetBranchAddress("tdcTime", &tdcTime);
196 
197  if (_tin->GetBranch("gCharge") != nullptr) {
198  std::cout << "Detected MC true-track branches." << std::endl;
199  is_mc = true;
200  _tin->SetBranchAddress("gCharge", &gCharge);
201  _tin->SetBranchAddress("gTrackID", &gTrackID);
202  _tin->SetBranchAddress("gvx", &gvx);
203  _tin->SetBranchAddress("gvy", &gvy);
204  _tin->SetBranchAddress("gvz", &gvz);
205  _tin->SetBranchAddress("gpx", &gpx);
206  _tin->SetBranchAddress("gpy", &gpy);
207  _tin->SetBranchAddress("gpz", &gpz);
208 
209  _tin->SetBranchAddress("gx_st1", &gx_st1);
210  _tin->SetBranchAddress("gy_st1", &gy_st1);
211  _tin->SetBranchAddress("gz_st1", &gz_st1);
212  _tin->SetBranchAddress("gpx_st1", &gpx_st1);
213  _tin->SetBranchAddress("gpy_st1", &gpy_st1);
214  _tin->SetBranchAddress("gpz_st1", &gpz_st1);
215 
216  _tin->SetBranchAddress("gx_st3", &gx_st3);
217  _tin->SetBranchAddress("gy_st3", &gy_st3);
218  _tin->SetBranchAddress("gz_st3", &gz_st3);
219  _tin->SetBranchAddress("gpx_st3", &gpx_st3);
220  _tin->SetBranchAddress("gpy_st3", &gpy_st3);
221  _tin->SetBranchAddress("gpz_st3", &gpz_st3);
222  }else {
223  is_mc = false;
224  std::cout << "No MC Track branches found. Running in RUS basic exp mode." << std::endl;
225  }
226 
227  if (!run_header) MakeNode();
228 
229  segment = 0;
230  isopen = 1;
231  AddToFileOpened(filenam); // Add file to the list of opened files
232  return 0;
233 }
234 
236  readagain:
237  if (!isopen) {
238  if (filelist.empty()) {
239  if (verbosity > 0) {
240  std::cout << Name() << ": No Input file open" << std::endl;
241  }
242  return -1;
243  } else {
244  if (OpenNextFile()) {
245  std::cout << Name() << ": No Input file from filelist opened" << std::endl;
246  return -1;
247  }
248  }
249  }
250 
251  if (verbosity > 3) {
252  std::cout << "Getting Event from " << Name() << std::endl;
253  }
254 
255  if (events_thisfile >= _tin->GetEntries()) {
256  fileclose();
257  goto readagain;
258  }
259 
260  _tin->GetEntry(events_thisfile);
261  events_thisfile++;
262  events_total++;
263 
268 
273  VectToE1039();
275  ResetEvent();
276  goto readagain;
277  }
278  return 0;
279 }
280 
282 {
283  syncobject->Reset();
284  return 0;
285 }
286 
288  {
289  if (!isopen) {
290  cout << Name() << ": fileclose: No Input file open" << endl;
291  return -1;
292  }
293 
294  _fin->Close();
295  isopen = 0;
296 
297  // if we have a file list, move next entry to top of the list
298  // or repeat the same entry again
299  if (!filelist.empty()) {
300  if (repeat) {
301  filelist.push_back(*(filelist.begin()));
302  if (repeat > 0) {
303  repeat--;
304  }
305  }
306  filelist.pop_front();
307  }
308 
309  return 0;
310  }
311 
312  void
313  Fun4AllRUSInputManager::Print(const string &what) const
314  {
316  return ;
317  }
318 
319  int
321  {
322  while (!filelist.empty())
323  {
324  list<string>::const_iterator iter = filelist.begin();
325  if (verbosity)
326  {
327  cout << PHWHERE << " opening next file: " << *iter << endl;
328  }
329  if (fileopen(*iter))
330  {
331  cout << PHWHERE << " could not open file: " << *iter << endl;
332  filelist.pop_front();
333  }
334  else
335  {
336  return 0;
337  }
338 
339  }
340  return -1;
341  }
342 
344  {
345  cerr << "!!ERROR!! PushBackEvents(): Not implemented yet." << endl;
346  // PushBackEvents is supposedly pushing events back on the stack which works
347  // easily with root trees (just grab a different entry) but hard in these HepMC ASCII files.
348  // A special case is when the synchronization fails and we need to only push back a single
349  // event. In this case we save the evt pointer as save_evt which is used in the run method
350  // instead of getting the next event.
351  // if (i > 0)
352  // {
353  // if (i == 1 && evt) // check on evt pointer makes sure it is not done from the cmd line
354  // {
355  // save_evt = evt;
356  // return 0;
357  // }
358  // cout << PHWHERE << ThisName
359  // << " Fun4AllRUSInputManager cannot push back " << i << " events into file"
360  // << endl;
361  // return -1;
362  // }
363  // if (!parser->coda)
364  // {
365  // cout << PHWHERE << ThisName
366  // << " no file open" << endl;
367  // return -1;
368  // }
369  // Skipping events is implemented as
370  // pushing a negative number of events on the stack, so in order to implement
371  // the skipping of events we read -i events.
372  // int nevents = -i; // negative number of events to push back -> skip num events
373  // int errorflag = 0;
374  // while (nevents > 0 && ! errorflag)
375  // {
376  // int * data_ptr = nullptr;
377  // unsigned int coda_id = 0;
378  // if(parser->coda->NextCodaEvent(coda_id, data_ptr))
379  // evt = new EVIO_Event(data_ptr);
380  // if (! evt)
381  // {
382  // cout << "Error after skipping " << i - nevents
383  // << " file exhausted?" << endl;
384  // errorflag = -1;
385  // fileclose();
386  // }
387  // else
388  // {
389  // if (verbosity > 3)
390  // {
391  // //TODO implement this
392  // //cout << "Skipping evt no: " << evt->getEvtSequence() << endl;
393  // }
394  // }
395  // delete evt;
396  // nevents--;
397  // }
398  // return errorflag;
399  return -1;
400  }
401 
402  int
404  {
405  // here we copy the sync object from the current file to the
406  // location pointed to by mastersync. If mastersync is a 0 pointer
407  // the syncobject is cloned. If mastersync allready exists the content
408  // of syncobject is copied
409  if (!(*mastersync))
410  {
411  if (syncobject) *mastersync = syncobject->clone();
412  }
413  else
414  {
415  *(*mastersync) = *syncobject; // copy syncobject content
416  }
418  }
419 
420  int
422  {
423  if (!mastersync)
424  {
425  cout << PHWHERE << Name() << " No MasterSync object, cannot perform synchronization" << endl;
426  cout << "Most likely your first file does not contain a SyncObject and the file" << endl;
427  cout << "opened by the Fun4AllDstInputManager with Name " << Name() << " has one" << endl;
428  cout << "Change your macro and use the file opened by this input manager as first input" << endl;
429  cout << "and you will be okay. Fun4All will not process the current configuration" << endl << endl;
431  }
432  int iret = syncobject->Different(mastersync);
433  if (iret)
434  {
435  cout << "big problem" << endl;
436  exit(1);
437  }
439  }
440 
442 {
443  PHNodeIterator iter(topNode);
444 
445  PHCompositeNode* runNode = static_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "RUN"));
446  if (!runNode) {
447  runNode = new PHCompositeNode("RUN");
448  topNode->addNode(runNode);
449  }
450 
451  PHCompositeNode* eventNode = static_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "DST"));
452  if (!eventNode) {
453  eventNode = new PHCompositeNode("DST");
454  topNode->addNode(eventNode);
455  }
456 
457  run_header = new SQRun_v1();
458  runNode->addNode(new PHIODataNode<PHObject>(run_header, "SQRun", "PHObject"));
459 
460  spill_map = new SQSpillMap_v1();
461  runNode->addNode(new PHIODataNode<PHObject>(spill_map, "SQSpillMap", "PHObject"));
462 
463  event_header = new SQEvent_v1();
464  eventNode->addNode(new PHIODataNode<PHObject>(event_header, "SQEvent", "PHObject"));
465 
466  hit_vec = new SQHitVector_v1();
467  eventNode->addNode(new PHIODataNode<PHObject>(hit_vec, "SQHitVector", "PHObject"));
468 
469  if (is_mc) {
470  trk_vec = new SQTrackVector_v1();
471  eventNode->addNode(new PHIODataNode<PHObject>(trk_vec, "SQTruthTrackVector", "PHObject"));
472  }
473 
474  std::cout << "Fun4AllRUSInputManager::MakeNode(): Created "
475  << (is_mc ? "MC" : "Data")
476  << " node structure under DST and RUN." << std::endl;
477 }
478 
TFile clean handling.
int verbosity
The verbosity level. 0 means not verbose at all.
Definition: Fun4AllBase.h:75
virtual const std::string Name() const
Returns the name of this module.
Definition: Fun4AllBase.h:23
std::string ThisName
Definition: Fun4AllBase.h:72
virtual void Print(const std::string &what="ALL") const
Fun4AllSyncManager * mySyncManager
std::list< std::string > filelist
void AddToFileOpened(const std::string &filename)
virtual void SetRunNumber(const int runno)
std::vector< double > * gpx_st3
std::vector< int > * gTrackID
std::vector< double > * gy_st1
std::vector< int > * hitID
std::vector< int > * elementID
std::vector< int > * detectorID
int SyncIt(const SyncObject *mastersync)
std::vector< double > * driftDistance
std::vector< int > * hitTrackID
std::vector< double > * gvx
std::vector< double > * gpz_st3
std::vector< double > * gz_st3
std::vector< double > * gy_st3
std::vector< double > * gpy_st1
int fileopen(const std::string &filename)
std::vector< double > * gpz
std::vector< double > * gz_st1
std::vector< double > * gpy_st3
std::vector< double > * gvz
std::vector< double > * gpy
std::vector< double > * tdcTime
int run(const int nevents=0)
int GetSyncObject(SyncObject **mastersync)
std::vector< double > * gpx
std::vector< double > * gx_st1
std::vector< double > * gvy
std::vector< double > * gpx_st1
void Print(const std::string &what="ALL") const
std::vector< int > * gCharge
Fun4AllRUSInputManager(const std::string &name="DUMMY", const std::string &topnodename="TOP")
std::vector< double > * gpz_st1
std::vector< double > * gx_st3
static Fun4AllServer * instance()
PHCompositeNode * topNode() const
Definition: Fun4AllServer.h:59
void CurrentEvent(const int evt)
void SegmentNumber(const int iseg)
void PrdfEvents(const int i)
PHBoolean addNode(PHNode *)
PHNode * findFirst(const std::string &, const std::string &)
virtual void set_data_quality(const int a)=0
@ MATRIX2
Definition: SQEvent.h:28
@ NIM5
Definition: SQEvent.h:26
@ MATRIX1
Definition: SQEvent.h:27
@ NIM4
Definition: SQEvent.h:25
@ NIM2
Definition: SQEvent.h:23
@ NIM1
Definition: SQEvent.h:22
@ MATRIX3
Definition: SQEvent.h:29
@ NIM3
Definition: SQEvent.h:24
@ MATRIX4
Definition: SQEvent.h:30
@ MATRIX5
Definition: SQEvent.h:31
virtual void set_qie_turn_id(const int a)=0
virtual void set_spill_id(const int a)=0
virtual void set_event_id(const int a)=0
virtual void set_qie_rf_id(const int a)=0
virtual void set_qie_rf_intensity(const short i, const int a)=0
virtual void set_run_id(const int a)=0
virtual void set_trigger(const SQEvent::TriggerMask i, const bool a)=0
virtual void push_back(const SQHit *hit)=0
An SQ interface class to hold one detector hit.
Definition: SQHit.h:20
virtual void set_element_id(const short a)
Definition: SQHit.h:46
virtual void set_hit_id(const int a)
Definition: SQHit.h:40
virtual void set_detector_id(const short a)
Definition: SQHit.h:43
virtual void set_tdc_time(const float a)
Definition: SQHit.h:55
virtual void set_track_id(const int a)
Definition: SQHit.h:67
virtual void set_drift_distance(const float a)
Definition: SQHit.h:58
virtual void set_n_spill(const int a)
Definition: SQRun.h:69
virtual const SQSpill * get(unsigned int idkey) const
Return the SQSpill entry having spill ID = 'idkey'. Return '0' if no entry exists.
Definition: SQSpillMap.h:41
virtual SQSpill * insert(const SQSpill *hit)
Insert the given SQSpill object.
Definition: SQSpillMap.h:43
virtual size_t size() const
Return the number of spills held.
Definition: SQSpillMap.h:37
An SQ interface class to hold the data of one spill.
Definition: SQSpill.h:19
virtual void set_spill_id(const int a)
Definition: SQSpill.h:38
virtual void set_run_id(const int a)
Definition: SQSpill.h:34
virtual void push_back(const SQTrack *trk)=0
An SQ interface class to hold one true or reconstructed track.
Definition: SQTrack.h:8
virtual void set_track_id(const int a)=0
virtual void set_pos_vtx(const TVector3 a)=0
virtual void set_mom_vtx(const TLorentzVector a)=0
virtual void set_pos_st1(const TVector3 a)=0
virtual void set_pos_st3(const TVector3 a)=0
virtual void set_charge(const int a)=0
virtual void set_mom_st1(const TLorentzVector a)=0
virtual void set_mom_st3(const TLorentzVector a)=0
virtual int Different(const SyncObject *other) const
Definition: SyncObject.cc:47
virtual void SegmentNumber(const int)
set Segment Number
Definition: SyncObject.h:40
virtual void Reset()
Clear Sync.
Definition: SyncObject.cc:9
virtual void EventNumber(const int)
set Event Number
Definition: SyncObject.h:36
virtual SyncObject * clone() const
Virtual copy constructor.
Definition: SyncObject.cc:28
virtual void EventCounter(const int)
set Event Counter
Definition: SyncObject.h:33
virtual void RunNumber(const int)
set Run Number
Definition: SyncObject.h:43
#define PHWHERE
Definition: phool.h:23