Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleZdcInput.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SingleZdcInput.cc
1 #include "SingleZdcInput.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  : SinglePrdfInput(name, inman)
18 {
19  plist = new Packet *[100];
20  m_PacketEventNumberOffset = new int[100]{};
21  rollover.fill(0);
23 }
24 
26 {
27  delete[] plist;
29 }
30 
31 void SingleZdcInput::FillPool(const unsigned int nevents)
32 {
33  if (AllDone()) // no more files and all events read
34  {
35  return;
36  }
37  while (GetEventIterator() == nullptr) // at startup this is a null pointer
38  {
39  if (!OpenNextFile())
40  {
41  AllDone(1);
42  return;
43  }
44  }
45  for (unsigned int ievt = 0; ievt < nevents; ievt++)
46  {
48  if (!evt)
49  {
50  fileclose();
51  if (!OpenNextFile())
52  {
53  AllDone(1);
54  return;
55  }
56  evt = GetEventIterator()->getNextEvent();
57  if (!evt)
58  {
59  std::cout << PHWHERE << "Event is nullptr" << std::endl;
60  AllDone(1);
61  return;
62  }
63  }
64 
65  RunNumber(evt->getRunNumber());
66  if (Verbosity() > 1)
67  {
68  evt->identify();
69  }
70  if (evt->getEvtType() != DATAEVENT)
71  {
73  delete evt;
74  continue; // need handling for non data events
75  }
76 
77  int EventSequence = evt->getEvtSequence();
78  int npackets = evt->getPacketList(plist, 100);
79  if (npackets == 100)
80  {
81  exit(1);
82  }
83  for (int i = 0; i < npackets; i++)
84  {
85  if (plist[i]->iValue(0, "EVENCHECKSUMOK") != 0 && plist[i]->iValue(0, "ODDCHECKSUMOK") != 0)
86  {
87  int evtno = plist[i]->iValue(0, "EVTNR") + rollover[i];
88  if (evtno < previous_eventnumber[i])
89  {
90  if (Verbosity() > 1)
91  {
92  std::cout << "rolling over, event " << std::hex << evtno
93  << ", prev: " << previous_eventnumber[i]
94  << ", rollover counter: " << (rollover[i] << 16)
95  << std::dec << std::endl;
96  }
97  rollover[i] ++;
98  }
99  previous_eventnumber[i] = evtno;
100  evtno += (rollover[i] << 16);
101  unsigned int bclk = plist[i]->iValue(0, "CLOCK");
102 
103  bool useFEMInfo = ((plist[i]->getIdentifier() / 1000 == 12) && evtno != ((EventSequence - 2) & 0xffff));
104 
105  if (useFEMInfo == true)
106  {
107  evtno = ((plist[i]->iValue(0, "FEMEVTNR") - 1) & 0xffff); // hard coded since FEM event starts at 1 and packet event starts at 0
108  bclk = ((plist[i]->iValue(0, "FEMCLOCK") + 30) & 0xffff); // hardcoded since level 1 delay for ZDC is 30 beam clocks.
109  }
110  if (Verbosity() > 1)
111  {
112  std::cout << "packet " << plist[i]->getIdentifier() << " evt: " << evtno
113  << std::hex << " clock: 0x" << bclk << std::dec << std::endl;
114  }
115  // dummy check for the first event which is the problem for the calorimeters
116  // it is the last event from the previous run, so it's event number is > 0
117  // if (evtno > EventSequence)
118  // {
119  // delete plist[i];
120  // plist[i] = nullptr;
121  // continue;
122  // }
123  plist[i]->convert();
124  // calculate "real" event number
125  // special events are counted, so the packet event counter is never the
126  // Event Sequence (bc the begin run event)
127  // also our packets are just 16bit counters, so we need to add the upper bits
128  // from the event sequence
129  // and our packet counters start at 0, while our events start at 1
131  m_PacketMap[bclk].push_back(plist[i]);
132  m_EvtSet.insert(evtno);
133  m_Event.emplace_back(std::make_pair(evtno, bclk));
134  }
135  else
136  {
137  delete plist[i];
138  }
139  }
140  // here we have all packets of a given event in our maps/vectors
141  // first pass - check if beam clocks are identical
142  if (Verbosity() > 1)
143  {
144  std::cout << "pktmap size : " << m_PacketMap.size() << std::endl;
145  std::cout << "evt set size : " << m_EvtSet.size() << std::endl;
146  }
147  int common_event_number = *(m_EvtSet.begin());
148  int common_beam_clock = m_PacketMap.begin()->first;
149  if (m_PacketMap.size() == 1) // all packets from the same beam clock
150  {
151  if (m_EvtSet.size() == 1)
152  {
153  if (Verbosity() > 1)
154  {
155  std::cout << "we are good evtno: " << *(m_EvtSet.begin())
156  << ", clock: " << m_PacketMap.begin()->first << std::endl;
157  }
158  }
159  else
160  {
161  if (Verbosity() > 1)
162  {
163  std::cout << "We have multiple event numbers for bclk: 0x" << std::hex
164  << m_PacketMap.begin()->first << std::dec << std::endl;
165  for (auto iter : m_EvtSet)
166  {
167  std::cout << "Event " << iter << std::endl;
168  }
169  }
170  common_event_number = majority_eventnumber();
171  if (Verbosity() > 1)
172  {
173  std::cout << "picked event no " << common_event_number << std::endl;
174  }
175  adjust_eventnumber_offset(common_event_number);
176  }
177  for (auto const &iter : m_PacketMap)
178  {
179  for (auto const &pktiter : iter.second)
180  {
181  InputMgr()->AddPacket(common_event_number, pktiter);
182  }
183  }
184  }
185  else
186  {
187  if (Verbosity() > 1)
188  {
189  std::cout << "We have multiple beam clocks per event" << std::endl;
190  }
191  if (m_EvtSet.size() == 1)
192  {
193  if (Verbosity() > 1)
194  {
195  std::cout << "we are good evtno: " << *(m_EvtSet.begin())
196  << ", clock: " << m_PacketMap.begin()->first << std::endl;
197  }
198  }
199  else
200  {
201  if (Verbosity() > 1)
202  {
203  std::cout << "We have multiple event numbers for bclk: 0x" << std::hex
204  << m_PacketMap.begin()->first << std::dec << std::endl;
205  for (auto iter : m_EvtSet)
206  {
207  std::cout << "Event " << iter << std::endl;
208  }
209  }
210  common_event_number = majority_eventnumber();
211  if (Verbosity() > 1)
212  {
213  std::cout << "picked event no " << common_event_number << std::endl;
214  }
215  adjust_eventnumber_offset(common_event_number);
216  }
217  common_beam_clock = majority_beamclock();
218  if (Verbosity() > 1)
219  {
220  std::cout << "picked bclk: " << std::hex << common_beam_clock << std::dec << std::endl;
221  }
222  // for time being clean out packets which do not match
223  for (auto const &iter : m_PacketMap)
224  {
225  for (auto pktiter : iter.second)
226  {
227  if (pktiter->iValue(0, "CLOCK") == common_beam_clock)
228  {
229  if (Verbosity() > 1)
230  {
231  std::cout << "adding packet " << pktiter->getIdentifier() << " beam clock "
232  << std::hex << pktiter->iValue(0, "CLOCK") << std::dec << std::endl;
233  }
234  InputMgr()->AddPacket(common_event_number, pktiter);
235  }
236  else
237  {
238  if (Verbosity() > 1)
239  {
240  std::cout << "Deleting packet " << pktiter->getIdentifier() << " beam clock "
241  << std::hex << pktiter->iValue(0, "CLOCK") << " common bclk: "
242  << common_beam_clock << std::dec << std::endl;
243  }
244  InputMgr()->UpdateDroppedPacket(pktiter->getIdentifier());
245  delete pktiter;
246  }
247  }
248  }
249  }
250  InputMgr()->AddBeamClock(common_event_number, common_beam_clock, this);
251  if (ReferenceFlag())
252  {
253  InputMgr()->SetReferenceClock(common_event_number, common_beam_clock);
254  }
255  m_PacketMap.clear();
256  m_EvtSet.clear();
257  m_Event.clear();
258  delete evt;
259  }
260 }
261 
262 void SingleZdcInput::adjust_eventnumber_offset(const int decided_evtno)
263 {
264  for (unsigned int i = 0; i < m_Event.size(); i++)
265  {
266  if (m_Event[i].first != decided_evtno)
267  {
268  m_PacketEventNumberOffset[i] -= (m_Event[i].first - decided_evtno);
269  if (Verbosity() > 1)
270  {
271  std::cout << "my evtno: " << m_Event[i].first << ", decided: " << decided_evtno
272  << ", adjustment: " << m_Event[i].first - decided_evtno << std::endl;
273  std::cout << "adjusting event number offset for " << i << " to " << m_PacketEventNumberOffset[i] << std::endl;
274  }
275  }
276  }
277 }
278 
280 {
281  std::map<int, int> evtcnt;
282  for (auto iter : m_Event)
283  {
284  evtcnt[iter.first]++;
285  }
286  int imax = -1;
287  int evtno = -1;
288  for (auto iter : evtcnt)
289  {
290  if (iter.second > imax)
291  {
292  evtno = iter.first;
293  imax = iter.second;
294  }
295  }
296  return evtno;
297 }
298 
300 {
301  std::map<int, int> evtcnt;
302  for (auto iter : m_Event)
303  {
304  evtcnt[iter.second]++;
305  if (Verbosity() > 1)
306  {
307  std::cout << "adding clk: " << std::hex << iter.second << std::dec
308  << " current counter: " << evtcnt[iter.second] << std::endl;
309  }
310  }
311  int imax = -1;
312  int bclk = std::numeric_limits<int>::max();
313  for (auto iter : evtcnt)
314  {
315  if (iter.second > imax)
316  {
317  bclk = iter.first;
318  imax = iter.second;
319  }
320  }
321  return bclk;
322 }