Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SinglePrdfInput.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SinglePrdfInput.cc
1 #include "SinglePrdfInput.h"
2 
4 
5 #include <frog/FROG.h>
6 
7 #include <phool/phool.h>
8 
9 #include <Event/Event.h>
10 #include <Event/EventTypes.h>
11 #include <Event/Eventiterator.h>
12 #include <Event/fileEventiterator.h>
13 
14 #include <limits>
15 
17  : Fun4AllBase(name)
18  , m_InputMgr(inman)
19 {
20  plist = new Packet *[100];
21  m_PacketEventNumberOffset = new int[100]{};
22  rollover.fill(0);
24  // std::fill_n(m_PacketEventNumberOffset, 100, 0);
25 }
26 
28 {
29  delete m_EventIterator;
30  delete[] plist;
32 }
33 
34 void SinglePrdfInput::FillPool(const unsigned int nevents)
35 {
36  if (AllDone()) // no more files and all events read
37  {
38  return;
39  }
40  while (m_EventIterator == nullptr) // at startup this is a null pointer
41  {
42  if (!OpenNextFile())
43  {
44  AllDone(1);
45  return;
46  }
47  }
48  for (unsigned int ievt = 0; ievt < nevents; ievt++)
49  {
51  if (!evt)
52  {
53  fileclose();
54  if (!OpenNextFile())
55  {
56  AllDone(1);
57  return;
58  }
60  if (!evt)
61  {
62  std::cout << PHWHERE << "Event is nullptr" << std::endl;
63  AllDone(1);
64  return;
65  }
66  }
67  m_RunNumber = evt->getRunNumber();
68  if (Verbosity() > 1)
69  {
70  evt->identify();
71  }
72  if (evt->getEvtType() != DATAEVENT)
73  {
75  delete evt;
76  continue; // need handling for non data events
77  }
78 // int EventSequence = evt->getEvtSequence();
79  int npackets = evt->getPacketList(plist, 100);
80  if (npackets == 100)
81  {
82  exit(1);
83  }
84  for (int i = 0; i < npackets; i++)
85  {
86  if (plist[i]->iValue(0, "CHECKSUMOK") != 0)
87  {
88  int evtno = plist[i]->iValue(0, "EVTNR") + rollover[i];
89  if (evtno < previous_eventnumber[i])
90  {
91  if (Verbosity() > 1)
92  {
93  std::cout << "rolling over, event " << std::hex << evtno
94  << ", prev: " << previous_eventnumber[i]
95  << ", rollover counter: " << (rollover[i] << 16)
96  << std::dec << std::endl;
97  }
98  rollover[i] ++;
99  }
100  previous_eventnumber[i] = evtno;
101  evtno += (rollover[i] << 16);
102  unsigned int bclk = plist[i]->iValue(0, "CLOCK");
103  if (Verbosity() > 1)
104  {
105  std::cout << "packet " << plist[i]->getIdentifier() << " evt: " << evtno
106  << std::hex << " clock: 0x" << bclk << std::dec << std::endl;
107  }
108  // dummy check for the first event which is the problem for the calorimeters
109  // it is the last event from the previous run, so it's event number is > 0
110  // if (evtno > EventSequence)
111  // {
112  // delete plist[i];
113  // plist[i] = nullptr;
114  // continue;
115  // }
116  plist[i]->convert();
117  // calculate "real" event number
118  // special events are counted, so the packet event counter is never the
119  // Event Sequence (bc the begin run event)
120  // also our packets are just 16bit counters, so we need to add the upper bits
121  // from the event sequence
122  // and our packet counters start at 0, while our events start at 1
124  m_PacketMap[bclk].push_back(plist[i]);
125  m_EvtSet.insert(evtno);
126  m_Event.emplace_back(std::make_pair(evtno, bclk));
127  }
128  else
129  {
130  delete plist[i];
131  }
132  }
133  // here we have all packets of a given event in our maps/vectors
134  // first pass - check if beam clocks are identical
135  if (Verbosity() > 1)
136  {
137  std::cout << "pktmap size : " << m_PacketMap.size() << std::endl;
138  std::cout << "evt set size : " << m_EvtSet.size() << std::endl;
139  }
140  int common_event_number = *(m_EvtSet.begin());
141  int common_beam_clock = m_PacketMap.begin()->first;
142  if (m_PacketMap.size() == 1) // all packets from the same beam clock
143  {
144  if (m_EvtSet.size() == 1)
145  {
146  if (Verbosity() > 1)
147  {
148  std::cout << "we are good evtno: " << *(m_EvtSet.begin())
149  << ", clock: " << m_PacketMap.begin()->first << std::endl;
150  }
151  }
152  else
153  {
154  if (Verbosity() > 1)
155  {
156  std::cout << "We have multiple event numbers for bclk: 0x" << std::hex
157  << m_PacketMap.begin()->first << std::dec << std::endl;
158  for (auto iter : m_EvtSet)
159  {
160  std::cout << "Event " << iter << std::endl;
161  }
162  }
163  common_event_number = majority_eventnumber();
164  if (Verbosity() > 1)
165  {
166  std::cout << "picked event no " << common_event_number << std::endl;
167  }
168  adjust_eventnumber_offset(common_event_number);
169  }
170  for (auto const &iter : m_PacketMap)
171  {
172  for (auto const &pktiter : iter.second)
173  {
174  m_InputMgr->AddPacket(common_event_number, pktiter);
175  }
176  }
177  }
178  else
179  {
180  if (Verbosity() > 1)
181  {
182  std::cout << "We have multiple beam clocks per event" << std::endl;
183  }
184  if (m_EvtSet.size() == 1)
185  {
186  if (Verbosity() > 1)
187  {
188  std::cout << "we are good evtno: " << *(m_EvtSet.begin())
189  << ", clock: " << m_PacketMap.begin()->first << std::endl;
190  }
191  }
192  else
193  {
194  if (Verbosity() > 1)
195  {
196  std::cout << "We have multiple event numbers for bclk: 0x" << std::hex
197  << m_PacketMap.begin()->first << std::dec << std::endl;
198  for (auto iter : m_EvtSet)
199  {
200  std::cout << "Event " << iter << std::endl;
201  }
202  }
203  common_event_number = majority_eventnumber();
204  if (Verbosity() > 1)
205  {
206  std::cout << "picked event no " << common_event_number << std::endl;
207  }
208  adjust_eventnumber_offset(common_event_number);
209  }
210  common_beam_clock = majority_beamclock();
211  if (Verbosity() > 1)
212  {
213  std::cout << "picked bclk: " << std::hex << common_beam_clock << std::dec << std::endl;
214  }
215  // for time being clean out packets which do not match
216  for (auto const &iter : m_PacketMap)
217  {
218  for (auto pktiter : iter.second)
219  {
220  if (pktiter->iValue(0, "CLOCK") == common_beam_clock)
221  {
222  if (Verbosity() > 1)
223  {
224  std::cout << "adding packet " << pktiter->getIdentifier() << " beam clock "
225  << std::hex << pktiter->iValue(0, "CLOCK") << std::dec << std::endl;
226  }
227  m_InputMgr->AddPacket(common_event_number, pktiter);
228  }
229  else
230  {
231  if (Verbosity() > 1)
232  {
233  std::cout << "Deleting packet " << pktiter->getIdentifier() << " beam clock "
234  << std::hex << pktiter->iValue(0, "CLOCK") << " common bclk: "
235  << common_beam_clock << std::dec << std::endl;
236  }
237  m_InputMgr->UpdateDroppedPacket(pktiter->getIdentifier());
238  delete pktiter;
239  }
240  }
241  }
242  }
243  m_InputMgr->AddBeamClock(common_event_number, common_beam_clock, this);
244  if (m_MeReferenceFlag)
245  {
246  m_InputMgr->SetReferenceClock(common_event_number, common_beam_clock);
247  }
248  m_PacketMap.clear();
249  m_EvtSet.clear();
250  m_Event.clear();
251  delete evt;
252  }
253 }
254 
255 void SinglePrdfInput::adjust_eventnumber_offset(const int decided_evtno)
256 {
257  for (unsigned int i = 0; i < m_Event.size(); i++)
258  {
259  if (m_Event[i].first != decided_evtno)
260  {
261  m_PacketEventNumberOffset[i] -= (m_Event[i].first - decided_evtno);
262  if (Verbosity() > 1)
263  {
264  std::cout << "my evtno: " << m_Event[i].first << ", decided: " << decided_evtno
265  << ", adjustment: " << m_Event[i].first - decided_evtno << std::endl;
266  std::cout << "adjusting event number offset for " << i << " to " << m_PacketEventNumberOffset[i] << std::endl;
267  }
268  }
269  }
270 }
271 
273 {
274  std::map<int, int> evtcnt;
275  for (auto iter : m_Event)
276  {
277  evtcnt[iter.first]++;
278  }
279  int imax = -1;
280  int evtno = -1;
281  for (auto iter : evtcnt)
282  {
283  if (iter.second > imax)
284  {
285  evtno = iter.first;
286  imax = iter.second;
287  }
288  }
289  return evtno;
290 }
291 
293 {
294  std::map<int, int> evtcnt;
295  for (auto iter : m_Event)
296  {
297  evtcnt[iter.second]++;
298  if (Verbosity() > 1)
299  {
300  std::cout << "adding clk: " << std::hex << iter.second << std::dec
301  << " current counter: " << evtcnt[iter.second] << std::endl;
302  }
303  }
304  int imax = -1;
305  int bclk = std::numeric_limits<int>::max();
306  for (auto iter : evtcnt)
307  {
308  if (iter.second > imax)
309  {
310  bclk = iter.first;
311  imax = iter.second;
312  }
313  }
314  return bclk;
315 }
316 
318 {
319  std::cout << PHWHERE << Name() << ": trying to open " << filenam << std::endl;
320  if (IsOpen())
321  {
322  std::cout << "Closing currently open file "
323  << FileName()
324  << " and opening " << filenam << std::endl;
325  fileclose();
326  }
327  FileName(filenam);
328  FROG frog;
330  if (Verbosity() > 0)
331  {
332  std::cout << Name() << ": opening file " << FileName() << std::endl;
333  }
334  int status = 0;
335  m_EventIterator = new fileEventiterator(fname.c_str(), status);
336  m_EventsThisFile = 0;
337  if (status)
338  {
339  delete m_EventIterator;
340  m_EventIterator = nullptr;
341  std::cout << PHWHERE << Name() << ": could not open file " << fname << std::endl;
342  return -1;
343  }
344  IsOpen(1);
345  AddToFileOpened(fname); // add file to the list of files which were opened
346  return 0;
347 }
348 
350 {
351  if (!IsOpen())
352  {
353  std::cout << Name() << ": fileclose: No Input file open" << std::endl;
354  return -1;
355  }
356  delete m_EventIterator;
357  m_EventIterator = nullptr;
358  IsOpen(0);
359  // if we have a file list, move next entry to top of the list
360  // or repeat the same entry again
361  UpdateFileList();
362  return 0;
363 }
364 
366 {
368  if (b && m_InputMgr)
369  {
371  }
372  return;
373 }