Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllPrdfInputPoolManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllPrdfInputPoolManager.cc
2 
3 #include "SinglePrdfInput.h"
4 
5 #include <fun4all/Fun4AllInputManager.h> // for Fun4AllInputManager
8 
9 #include <ffaobjects/SyncObject.h> // for SyncObject
11 
12 #include <phool/PHCompositeNode.h>
13 #include <phool/PHDataNode.h>
14 #include <phool/PHNode.h> // for PHNode
15 #include <phool/PHNodeIterator.h> // for PHNodeIterator
16 #include <phool/PHObject.h> // for PHObject
17 #include <phool/phool.h> // for PHWHERE
18 
19 #include <Event/A_Event.h>
20 #include <Event/Event.h>
21 #include <Event/oEvent.h>
22 #include <Event/packet.h>
23 
24 #include <cassert>
25 #include <climits>
26 #include <cstdlib>
27 #include <iostream> // for operator<<, basic_ostream, endl
28 #include <utility> // for pair
29 
31  : Fun4AllInputManager(name, prdfnodename, topnodename)
32  , m_SyncObject(new SyncObjectv1())
33  , m_PrdfNodeName(prdfnodename)
34 {
36  m_topNode = se->topNode(TopNodeName());
38  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
39  if (!PrdfNode)
40  {
42  m_topNode->addNode(newNode);
43  }
44  oph = new oEvent(workmem, 4 * 1024 * 1024, 1, 1, 1);
45  return;
46 }
47 
49 {
50  if (IsOpen())
51  {
52  fileclose();
53  }
54  delete m_SyncObject;
55  for (auto iter : m_PrdfInputVector)
56  {
57  delete iter;
58  }
59  for (const auto &pktinfoiter : m_PacketMap)
60  {
61  for (auto const &pktiter : pktinfoiter.second.PacketVector)
62  {
63  delete pktiter;
64  }
65  }
66  delete oph;
67 }
68 
69 int Fun4AllPrdfInputPoolManager::run(const int /*nevents*/)
70 {
71  if (m_StartUpFlag)
72  {
73  for (auto iter : m_PrdfInputVector)
74  {
75  iter->FillPool(m_InitialPoolDepth);
76  m_RunNumber = iter->RunNumber();
77  }
79  m_StartUpFlag = false;
80  }
81  bool event_ok = false;
82  while (!event_ok)
83  {
84  event_ok = true;
85  if (m_PacketMap.size() < m_PoolDepth)
86  {
87  for (auto iter : m_PrdfInputVector)
88  {
89  iter->FillPool(m_PoolDepth);
90  m_RunNumber = iter->RunNumber();
91  }
93  }
94 
95  if (m_PacketMap.empty())
96  {
97  std::cout << "we are done" << std::endl;
98  return -1;
99  }
100  // std::cout << "next event is " << m_PacketMap.begin()->first << std::endl;
101  auto pktinfoiter = m_PacketMap.begin();
102  int eventnumber = pktinfoiter->first;
103 
104  // if we don't have this event in our reference input - ditch it (messes with the ref beam clock counter)
105  if (m_RefClockCounters.find(eventnumber) == m_RefClockCounters.end())
106  {
107  event_ok = false;
108  DitchEvent(eventnumber);
109  }
110  else
111  {
112  int refclock = m_RefClockCounters[eventnumber];
113  for (auto veciter : m_ClockCounters[eventnumber])
114  {
115  int diffclock = CalcDiffBclk(veciter.first, refclock);
116  if (diffclock != m_SinglePrdfInputInfo[veciter.second].bclkoffset)
117  {
118  std::cout << "Houston we have a problem with event " << eventnumber << std::endl;
119  std::cout << "name " << veciter.second->Name() << ", diffclk: 0x" << std::hex
120  << diffclock << ", my bclk: 0x" << veciter.first
121  << ", ref clk: 0x" << refclock << std::dec << std::endl;
122  Resynchronize();
123  event_ok = false;
124  break;
125  }
126  }
127  }
128  }
129  auto pktinfoiter = m_PacketMap.begin();
130  oph->prepare_next(pktinfoiter->first, m_RunNumber);
131 
132  for (auto &pktiter : pktinfoiter->second.PacketVector)
133  {
134  oph->addPacket(pktiter);
135  }
136  m_Event = new A_Event(workmem);
137  if (Verbosity() > 1)
138  {
139  m_Event->identify();
140  }
142  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
143  PrdfNode->setData(m_Event);
144  for (auto &pktiter : pktinfoiter->second.PacketVector)
145  {
146  delete pktiter;
147  }
148  m_ClockCounters.erase(pktinfoiter->first);
149  m_RefClockCounters.erase(pktinfoiter->first);
150  m_PacketMap.erase(pktinfoiter);
151  return 0;
152  // readagain:
153  // if (!IsOpen())
154  // {
155  // if (FileListEmpty())
156  // {
157  // if (Verbosity() > 0)
158  // {
159  // std::cout << Name() << ": No Input file open" << std::endl;
160  // }
161  // return -1;
162  // }
163  // else
164  // {
165  // if (OpenNextFile())
166  // {
167  // std::cout << Name() << ": No Input file from filelist opened" << std::endl;
168  // return -1;
169  // }
170  // }
171  // }
172  // if (Verbosity() > 3)
173  // {
174  // std::cout << "Getting Event from " << Name() << std::endl;
175  // }
176  // // Fill Event combiner
177  // unsigned int watermark = m_EventCombiner.size();
178  // if (watermark < m_LowWaterMark)
179  // {
180  // for (unsigned int i = watermark; i < m_CombinerDepth; i++)
181  // {
182  // Event *evt = m_EventIterator->getNextEvent();
183  // std::cout << "Filling combiner with event " << evt->getEvtSequence() << std::endl;
184  // m_EventCombiner.insert(std::make_pair(evt->getEvtSequence(), evt));
185  // }
186  // }
187  // // std::cout << "running event " << nevents << std::endl;
188  // PHNodeIterator iter(m_topNode);
189  // PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
190  // if (m_SaveEvent) // if an event was pushed back, copy saved pointer and reset m_SaveEvent pointer
191  // {
192  // m_Event = m_SaveEvent;
193  // m_SaveEvent = nullptr;
194  // m_EventsThisFile--;
195  // m_EventsTotal--;
196  // }
197  // else
198  // {
199  // m_Event = m_EventCombiner.begin()->second;
200  // }
201  // PrdfNode->setData(m_Event);
202  // if (!m_Event)
203  // {
204  // fileclose();
205  // goto readagain;
206  // }
207  // if (Verbosity() > 1)
208  // {
209  // std::cout << Name() << " PRDF run " << m_Event->getRunNumber() << ", evt no: " << m_Event->getEvtSequence() << std::endl;
210  // }
211  // m_EventsTotal++;
212  // m_EventsThisFile++;
213  // SetRunNumber(m_Event->getRunNumber());
214  // MySyncManager()->PrdfEvents(m_EventsThisFile);
215  // MySyncManager()->SegmentNumber(m_Segment);
216  // MySyncManager()->CurrentEvent(m_Event->getEvtSequence());
217  // m_SyncObject->EventCounter(m_EventsThisFile);
218  // m_SyncObject->SegmentNumber(m_Segment);
219  // m_SyncObject->RunNumber(m_Event->getRunNumber());
220  // m_SyncObject->EventNumber(m_Event->getEvtSequence());
221  // // check if the local SubsysReco discards this event
222  // if (RejectEvent() != Fun4AllReturnCodes::EVENT_OK)
223  // {
224  // ResetEvent();
225  // goto readagain;
226  // }
227  // return 0;
228 }
229 
231 {
232  for (auto iter : m_PrdfInputVector)
233  {
234  delete iter;
235  }
236  m_PrdfInputVector.clear();
237  return 0;
238 }
239 
241 {
242  // Fun4AllInputManager::Print(what);
243  if (what == "ALL" || what == "DROPPED")
244  {
245  std::cout << "-----------------------------" << std::endl;
246  std::cout << "dropped packets:" << std::endl;
247  for (auto iter : m_DroppedPacketMap)
248  {
249  std::cout << "Packet " << iter.first << " was dropped " << iter.second << " times" << std::endl;
250  }
251  }
252  return;
253 }
254 
256 {
258  PHDataNode<Event> *PrdfNode = dynamic_cast<PHDataNode<Event> *>(iter.findFirst("PHDataNode", m_PrdfNodeName));
259  PrdfNode->setData(nullptr); // set pointer in Node to nullptr before deleting it
260  delete m_Event;
261  m_Event = nullptr;
262  // m_SyncObject->Reset();
263  return 0;
264 }
265 
267 {
268  return 0;
269  // PushBackEvents is supposedly pushing events back on the stack which works
270  // easily with root trees (just grab a different entry) but hard in these HepMC ASCII files.
271  // A special case is when the synchronization fails and we need to only push back a single
272  // event. In this case we save the m_Event pointer as m_SaveEvent which is used in the run method
273  // instead of getting the next event.
274  // if (i > 0)
275  // {
276  // if (i == 1 && m_Event) // check on m_Event pointer makes sure it is not done from the cmd line
277  // {
278  // m_SaveEvent = m_Event;
279  // return 0;
280  // }
281  // std::cout << PHWHERE << Name()
282  // << " Fun4AllPrdfInputPoolManager cannot push back " << i << " events into file"
283  // << std::endl;
284  // return -1;
285  // }
286  // if (!m_EventIterator)
287  // {
288  // std::cout << PHWHERE << Name()
289  // << " no file open" << std::endl;
290  // return -1;
291  // }
292  // // Skipping events is implemented as
293  // // pushing a negative number of events on the stack, so in order to implement
294  // // the skipping of events we read -i events.
295  // int nevents = -i; // negative number of events to push back -> skip num events
296  // int errorflag = 0;
297  // while (nevents > 0 && !errorflag)
298  // {
299  // m_Event = m_EventIterator->getNextEvent();
300  // if (!m_Event)
301  // {
302  // std::cout << "Error after skipping " << i - nevents
303  // << " file exhausted?" << std::endl;
304  // errorflag = -1;
305  // fileclose();
306  // }
307  // else
308  // {
309  // if (Verbosity() > 3)
310  // {
311  // std::cout << "Skipping evt no: " << m_Event->getEvtSequence() << std::endl;
312  // }
313  // }
314  // delete m_Event;
315  // m_Event = nullptr;
316  // nevents--;
317  // }
318  // return errorflag;
319 }
320 
322 {
323  // here we copy the sync object from the current file to the
324  // location pointed to by mastersync. If mastersync is a 0 pointer
325  // the syncobject is cloned. If mastersync allready exists the content
326  // of syncobject is copied
327  if (!(*mastersync))
328  {
329  if (m_SyncObject)
330  {
331  *mastersync = dynamic_cast<SyncObject *>(m_SyncObject->CloneMe());
332  assert(*mastersync);
333  }
334  }
335  else
336  {
337  *(*mastersync) = *m_SyncObject; // copy syncobject content
338  }
340 }
341 
343 {
344  if (!mastersync)
345  {
346  std::cout << PHWHERE << Name() << " No MasterSync object, cannot perform synchronization" << std::endl;
347  std::cout << "Most likely your first file does not contain a SyncObject and the file" << std::endl;
348  std::cout << "opened by the Fun4AllDstInputManager with Name " << Name() << " has one" << std::endl;
349  std::cout << "Change your macro and use the file opened by this input manager as first input" << std::endl;
350  std::cout << "and you will be okay. Fun4All will not process the current configuration" << std::endl
351  << std::endl;
353  }
354  int iret = m_SyncObject->Different(mastersync);
355  if (iret)
356  {
357  std::cout << "big problem" << std::endl;
358  exit(1);
359  }
361 }
362 
364 {
365  if (what == "PRDFNODENAME")
366  {
367  return m_PrdfNodeName;
368  }
369  return "";
370 }
371 
373 {
374  SinglePrdfInput *prdfin = new SinglePrdfInput("PRDFIN_" + std::to_string(m_PrdfInputVector.size()), this);
375  prdfin->AddFile(filenam);
376  m_PrdfInputVector.push_back(prdfin);
377  return m_PrdfInputVector.back();
378 }
379 
381 {
382  SinglePrdfInput *prdfin = new SinglePrdfInput("PRDFIN_" + std::to_string(m_PrdfInputVector.size()), this);
383  prdfin->AddListFile(filenam);
384  m_PrdfInputVector.push_back(prdfin);
385  return m_PrdfInputVector.back();
386 }
387 
389 {
390  m_PrdfInputVector.push_back(prdfin);
391  return m_PrdfInputVector.back();
392 }
393 
395 {
396  if (Verbosity() > 1)
397  {
398  std::cout << "Adding packet " << p->getIdentifier() << " to event no " << evtno << std::endl;
399  }
400  m_PacketMap[evtno].PacketVector.push_back(p);
401 }
402 
403 void Fun4AllPrdfInputPoolManager::AddBeamClock(const int evtno, const int bclk, SinglePrdfInput *prdfin)
404 {
405  if (Verbosity() > 1)
406  {
407  std::cout << "Adding event " << evtno << ", clock 0x" << std::hex << bclk << std::dec
408  << " snglinput: " << prdfin->Name() << std::endl;
409  }
410  m_ClockCounters[evtno].push_back(std::make_pair(bclk, prdfin));
411 }
412 
414 {
415  m_PacketMap[evtno].EventFoundCounter++;
416 }
417 
419 {
421 }
422 
423 void Fun4AllPrdfInputPoolManager::SetReferenceClock(const int evtno, const int bclk)
424 {
425  m_RefClockCounters[evtno] = bclk;
426 }
427 
429 {
430  if (!m_RefPrdfInput)
431  {
432  std::cout << PHWHERE << " No reference input manager given" << std::endl;
433  exit(1);
434  }
435  std::map<SinglePrdfInput *, std::map<int, int>> clockcounters;
436  for (const auto &iter : m_ClockCounters)
437  {
438  int refclock = m_RefClockCounters[iter.first];
439  for (auto veciter : iter.second)
440  {
441  int diffclk = CalcDiffBclk(veciter.first, refclock);
442  if (Verbosity() > 1)
443  {
444  std::cout << "diffclk for " << veciter.second->Name() << ": " << std::hex
445  << diffclk << ", clk: 0x" << veciter.first
446  << ", refclk: 0x" << refclock << std::dec << std::endl;
447  }
448  auto clkiter = clockcounters.find(veciter.second);
449  if (clkiter == clockcounters.end())
450  {
451  std::map<int, int> mymap;
452  clkiter = clockcounters.insert(std::make_pair(veciter.second, mymap)).first;
453  }
454  clkiter->second[diffclk]++;
455  }
456  }
457  // now loop over the clock counter diffs for each input manager and find the majority vote
458  for (const auto &iter : clockcounters)
459  {
460  int imax = -1;
461  int diffmax = INT_MAX;
462  for (auto initer : iter.second)
463  {
464  if (Verbosity() > 0)
465  {
466  std::cout << iter.first->Name() << " initer.second " << initer.second << std::hex
467  << " initer.first: " << initer.first << std::dec << std::endl;
468  }
469  if (initer.second > imax)
470  {
471  diffmax = initer.first;
472  imax = initer.second;
473  }
474  m_SinglePrdfInputInfo[iter.first].bclkoffset = diffmax;
475  }
476  }
477  for (auto iter : m_SinglePrdfInputInfo)
478  {
479  if (Verbosity() > 0)
480  {
481  std::cout << "prdf mgr " << iter.first->Name() << " clkdiff: 0x" << std::hex
482  << iter.second.bclkoffset << std::dec << std::endl;
483  }
484  }
485 }
486 
487 int Fun4AllPrdfInputPoolManager::CalcDiffBclk(const int bclk1, const int bclk2)
488 {
489  int diffclk = (bclk1 - bclk2) & 0xFFFF;
490  return diffclk;
491 }
492 
494 {
495  if (Verbosity() > 1)
496  {
497  std::cout << "Killing event " << eventno << std::endl;
498  }
499  m_ClockCounters.erase(eventno);
500  m_RefClockCounters.erase(eventno);
501  auto pktinfoiter = m_PacketMap.find(eventno);
502  if (pktinfoiter == m_PacketMap.end())
503  {
504  return;
505  }
506  for (auto const &pktiter : pktinfoiter->second.PacketVector)
507  {
508  delete pktiter;
509  }
510  m_PacketMap.erase(pktinfoiter);
511  return;
512 }
513 
515 {
516  // just load events to give us a chance to find the match
517  struct LocalInfo
518  {
519  int clockcounter;
520  int eventdiff;
521  };
522  for (auto iter : m_PrdfInputVector)
523  {
524  iter->FillPool(10);
525  // iter->FillPool(m_InitialPoolDepth);
526  m_RunNumber = iter->RunNumber();
527  }
528  std::map<SinglePrdfInput *, LocalInfo> matchevent;
529  std::vector<int> ditchevents;
530  for (auto iter : m_RefClockCounters)
531  {
532  if (Verbosity() > 1)
533  {
534  std::cout << "looking for matching event " << iter.first
535  << std::hex << " with clk 0x" << iter.second << std::dec << std::endl;
536  }
537  for (const auto &clockiter : m_ClockCounters)
538  {
539  if (Verbosity() > 1)
540  {
541  std::cout << "testing for matching with event " << clockiter.first << std::endl;
542  }
543  for (auto eventiter : clockiter.second)
544  {
545  int diffclock = CalcDiffBclk(eventiter.first, iter.second);
546  if (Verbosity() > 1)
547  {
548  std::cout << "Event " << iter.first << " match with event " << clockiter.first
549  << " clock 0x" << std::hex << eventiter.first << ", ref clock 0x" << iter.second
550  << " diff 0x" << diffclock << std::dec
551  << " for " << eventiter.second->Name() << std::endl;
552  }
553  if (diffclock == m_SinglePrdfInputInfo[eventiter.second].bclkoffset)
554  {
555  if (Verbosity() > 1)
556  {
557  std::cout << "looking good for " << eventiter.second->Name() << std::endl;
558  }
559  matchevent[eventiter.second].clockcounter = clockiter.first;
560  matchevent[eventiter.second].eventdiff = clockiter.first - iter.first;
561  }
562  else
563  {
564  if (Verbosity() > 1)
565  {
566  std::cout << "not so great for " << eventiter.second->Name() << std::endl;
567  }
568  }
569  }
570  if (matchevent.size() == m_SinglePrdfInputInfo.size())
571  {
572  if (Verbosity() > 1)
573  {
574  std::cout << "found all matches" << std::endl;
575  }
576  break;
577  }
578  }
579  if (matchevent.size() == m_SinglePrdfInputInfo.size())
580  {
581  if (Verbosity() > 1)
582  {
583  std::cout << "found all matches" << std::endl;
584  }
585  break;
586  }
587  ditchevents.push_back(iter.first);
588  }
589  for (auto ievent : ditchevents)
590  {
591  DitchEvent(ievent);
592  }
593  int minoffset = INT_MAX;
594  for (auto matches : matchevent)
595  {
596  if (Verbosity() > 1)
597  {
598  std::cout << matches.first->Name() << " update event offset with: " << matches.second.eventdiff
599  << ", current offset : " << matches.first->EventNumberOffset()
600  << " would go to " << matches.first->EventNumberOffset() - matches.second.eventdiff << std::endl;
601  }
602  if (minoffset > matches.first->EventNumberOffset() - matches.second.eventdiff)
603  {
604  minoffset = matches.first->EventNumberOffset() - matches.second.eventdiff;
605  }
606  }
607  // we cannot have negative offsets right now (this would require re-reading the previous event which is gone)
608  int addoffset = 0;
609  if (minoffset < 0)
610  {
611  if (Verbosity() > 1)
612  {
613  std::cout << "minoffset < 0: " << minoffset << " this will be interesting" << std::endl;
614  }
615  addoffset = -minoffset;
616  }
617  for (auto matches : matchevent)
618  {
619  matches.first->EventNumberOffset(matches.first->EventNumberOffset() - matches.second.eventdiff + addoffset);
620  if (Verbosity() > 1)
621  {
622  std::cout << matches.first->Name() << " update event offset to: " << matches.first->EventNumberOffset()
623  << std::endl;
624  }
625  }
626  ClearAllEvents();
627  return;
628 }
629 
631 {
632  for (const auto &pktinfoiter : m_PacketMap)
633  {
634  for (auto const &pktiter : pktinfoiter.second.PacketVector)
635  {
636  delete pktiter;
637  }
638  }
639  m_ClockCounters.clear();
640  m_RefClockCounters.clear();
641  m_PacketMap.clear();
642 }