ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllPrdfInputManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllPrdfInputManager.cc
2 
3 #include <fun4all/Fun4AllInputManager.h> // for Fun4AllInputManager
7 #include <fun4all/Fun4AllUtils.h>
8 
9 #include <ffaobjects/SyncObject.h> // for SyncObject
11 
12 #include <frog/FROG.h>
13 
14 #include <phool/PHCompositeNode.h>
15 #include <phool/PHDataNode.h>
16 #include <phool/PHNode.h> // for PHNode
17 #include <phool/PHNodeIterator.h> // for PHNodeIterator
18 #include <phool/PHObject.h> // for PHObject
19 #include <phool/phool.h> // for PHWHERE
20 
21 #include <Event/Event.h>
22 #include <Event/Eventiterator.h> // for Eventiterator
23 #include <Event/fileEventiterator.h>
24 
25 #include <cassert>
26 #include <cstdlib>
27 #include <iostream> // for operator<<, basic_ostream, endl
28 #include <utility> // for pair
29 
30 using namespace std;
31 
32 Fun4AllPrdfInputManager::Fun4AllPrdfInputManager(const string &name, const string &prdfnodename, const string &topnodename)
33  : Fun4AllInputManager(name, prdfnodename, topnodename)
34  , m_Segment(-999)
35  , m_EventsTotal(0)
36  , m_EventsThisFile(0)
37  , m_Event(nullptr)
38  , m_SaveEvent(nullptr)
39  , m_EventIterator(nullptr)
40  , m_SyncObject(new SyncObjectv1())
41  , m_PrdfNodeName(prdfnodename)
42 {
44  m_topNode = se->topNode(TopNodeName());
46  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
47  if (!PrdfNode)
48  {
50  m_topNode->addNode(newNode);
51  }
52  return;
53 }
54 
56 {
57  if (IsOpen())
58  {
59  fileclose();
60  }
61  delete m_SyncObject;
62 }
63 
64 int Fun4AllPrdfInputManager::fileopen(const string &filenam)
65 {
66  if (IsOpen())
67  {
68  cout << "Closing currently open file "
69  << FileName()
70  << " and opening " << filenam << endl;
71  fileclose();
72  }
73  FileName(filenam);
74  FROG frog;
75  string fname = frog.location(FileName());
76  if (Verbosity() > 0)
77  {
78  cout << Name() << ": opening file " << FileName() << endl;
79  }
80  int status = 0;
81  m_EventIterator = new fileEventiterator(fname.c_str(), status);
82  m_EventsThisFile = 0;
83  if (status)
84  {
85  delete m_EventIterator;
86  m_EventIterator = nullptr;
87  cout << PHWHERE << Name() << ": could not open file " << fname << endl;
88  return -1;
89  }
90  pair<int, int> runseg = Fun4AllUtils::GetRunSegment(fname);
91  m_Segment = runseg.second;
92  IsOpen(1);
93  AddToFileOpened(fname); // add file to the list of files which were opened
94  return 0;
95 }
96 
97 int Fun4AllPrdfInputManager::run(const int nevents)
98 {
99 readagain:
100  if (!IsOpen())
101  {
102  if (FileListEmpty())
103 
104  {
105  if (Verbosity() > 0)
106  {
107  cout << Name() << ": No Input file open" << endl;
108  }
109  return -1;
110  }
111  else
112  {
113  if (OpenNextFile())
114  {
115  cout << Name() << ": No Input file from filelist opened" << endl;
116  return -1;
117  }
118  }
119  }
120  if (Verbosity() > 3)
121  {
122  cout << "Getting Event from " << Name() << endl;
123  }
124  // cout << "running event " << nevents << endl;
126  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
127  if (m_SaveEvent) // if an event was pushed back, copy saved pointer and reset m_SaveEvent pointer
128  {
130  m_SaveEvent = nullptr;
132  m_EventsTotal--;
133  }
134  else
135  {
137  }
138  PrdfNode->setData(m_Event);
139  if (!m_Event)
140  {
141  fileclose();
142  goto readagain;
143  }
144  if (Verbosity() > 1)
145  {
146  cout << Name() << " PRDF run " << m_Event->getRunNumber() << ", evt no: " << m_Event->getEvtSequence() << endl;
147  }
148  m_EventsTotal++;
158  // check if the local SubsysReco discards this event
160  {
161  ResetEvent();
162  goto readagain;
163  }
164  return 0;
165 }
166 
168 {
169  if (!IsOpen())
170  {
171  cout << Name() << ": fileclose: No Input file open" << endl;
172  return -1;
173  }
174  delete m_EventIterator;
175  m_EventIterator = nullptr;
176  IsOpen(0);
177  // if we have a file list, move next entry to top of the list
178  // or repeat the same entry again
179  UpdateFileList();
180  return 0;
181 }
182 
183 void Fun4AllPrdfInputManager::Print(const string &what) const
184 {
186  return;
187 }
188 
190 {
192  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
193  PrdfNode->setData(nullptr); // set pointer in Node to nullptr before deleting it
194  delete m_Event;
195  m_Event = nullptr;
196  m_SyncObject->Reset();
197  return 0;
198 }
199 
201 {
202  // PushBackEvents is supposedly pushing events back on the stack which works
203  // easily with root trees (just grab a different entry) but hard in these HepMC ASCII files.
204  // A special case is when the synchronization fails and we need to only push back a single
205  // event. In this case we save the m_Event pointer as m_SaveEvent which is used in the run method
206  // instead of getting the next event.
207  if (i > 0)
208  {
209  if (i == 1 && m_Event) // check on m_Event pointer makes sure it is not done from the cmd line
210  {
212  return 0;
213  }
214  cout << PHWHERE << Name()
215  << " Fun4AllPrdfInputManager cannot push back " << i << " events into file"
216  << endl;
217  return -1;
218  }
219  if (!m_EventIterator)
220  {
221  cout << PHWHERE << Name()
222  << " no file open" << endl;
223  return -1;
224  }
225  // Skipping events is implemented as
226  // pushing a negative number of events on the stack, so in order to implement
227  // the skipping of events we read -i events.
228  int nevents = -i; // negative number of events to push back -> skip num events
229  int errorflag = 0;
230  while (nevents > 0 && !errorflag)
231  {
233  if (!m_Event)
234  {
235  cout << "Error after skipping " << i - nevents
236  << " file exhausted?" << endl;
237  errorflag = -1;
238  fileclose();
239  }
240  else
241  {
242  if (Verbosity() > 3)
243  {
244  cout << "Skipping evt no: " << m_Event->getEvtSequence() << endl;
245  }
246  }
247  delete m_Event;
248  m_Event = nullptr;
249  nevents--;
250  }
251  return errorflag;
252 }
253 
255 {
256  // here we copy the sync object from the current file to the
257  // location pointed to by mastersync. If mastersync is a 0 pointer
258  // the syncobject is cloned. If mastersync allready exists the content
259  // of syncobject is copied
260  if (!(*mastersync))
261  {
262  if (m_SyncObject)
263  {
264  *mastersync = dynamic_cast<SyncObject *>(m_SyncObject->CloneMe());
265  assert(*mastersync);
266  }
267  }
268  else
269  {
270  *(*mastersync) = *m_SyncObject; // copy syncobject content
271  }
273 }
274 
276 {
277  if (!mastersync)
278  {
279  cout << PHWHERE << Name() << " No MasterSync object, cannot perform synchronization" << endl;
280  cout << "Most likely your first file does not contain a SyncObject and the file" << endl;
281  cout << "opened by the Fun4AllDstInputManager with Name " << Name() << " has one" << endl;
282  cout << "Change your macro and use the file opened by this input manager as first input" << endl;
283  cout << "and you will be okay. Fun4All will not process the current configuration" << endl
284  << endl;
286  }
287  int iret = m_SyncObject->Different(mastersync);
288  if (iret)
289  {
290  cout << "big problem" << endl;
291  exit(1);
292  }
294 }