Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleInttPoolInput.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SingleInttPoolInput.cc
1 #include "SingleInttPoolInput.h"
2 #include "intt_pool.h"
3 
5 
8 
10 #include <phool/PHIODataNode.h> // for PHIODataNode
11 #include <phool/PHNodeIterator.h> // for PHNodeIterator
12 #include <phool/getClass.h>
13 
14 #include <Event/Event.h>
15 #include <Event/EventTypes.h>
16 #include <Event/Eventiterator.h>
17 
18 #include <algorithm> // for max
19 #include <cstdint> // for uint64_t
20 #include <cstdlib> // for exit
21 #include <iostream> // for operator<<, basic_o...
22 #include <set>
23 #include <utility> // for pair
24 
26  : SingleStreamingInput(name)
27 {
29  plist = new Packet *[1];
30 }
31 
33 {
34  delete[] plist;
35  for (auto iter : poolmap)
36  {
37  if (Verbosity() > 2)
38  {
39  std::cout << "deleting intt pool for id " << (iter.second)->getIdentifier() << std::endl;
40  }
41  delete (iter.second);
42  }
43 }
44 
45 void SingleInttPoolInput::FillPool(const unsigned int)
46 {
47  if (AllDone()) // no more files and all events read
48  {
49  return;
50  }
51  while (GetEventiterator() == nullptr) // at startup this is a null pointer
52  {
53  if (!OpenNextFile())
54  {
55  AllDone(1);
56  return;
57  }
58  }
59 
60 // std::set<uint64_t> saved_beamclocks;
61  while (GetSomeMoreEvents(0))
62  {
64  while (!evt)
65  {
66  fileclose();
67  if (!OpenNextFile())
68  {
69  AllDone(1);
70  return;
71  }
72  evt = GetEventiterator()->getNextEvent();
73  }
74  if (Verbosity() > 2)
75  {
76  std::cout << "Fetching next Event" << evt->getEvtSequence() << std::endl;
77  }
78  RunNumber(evt->getRunNumber());
79  if (GetVerbosity() > 1)
80  {
81  evt->identify();
82  }
83  // not interested in special events, really
84  if (evt->getEvtType() != DATAEVENT)
85  {
87  delete evt;
88  continue;
89  }
90 
91  int EventSequence = evt->getEvtSequence();
92  int npackets = evt->getPacketList(plist, 1);
93 
94  if (npackets > 1)
95  {
96  exit(1);
97  }
98 
99  for (int i = 0; i < npackets; i++)
100  {
101  if (Verbosity() > 2)
102  {
103  plist[i]->identify();
104  }
105 
106  if (poolmap.find(plist[i]->getIdentifier()) == poolmap.end()) // we haven't seen this one yet
107  {
108  if (Verbosity() > 1)
109  {
110  std::cout << "starting new intt pool for packet " << plist[i]->getIdentifier() << std::endl;
111  }
112  poolmap[plist[i]->getIdentifier()] = new intt_pool(1000, 100);
113  poolmap[plist[i]->getIdentifier()]->Verbosity(Verbosity());
114  poolmap[plist[i]->getIdentifier()]->Name(std::to_string(plist[i]->getIdentifier()));
115  }
116  poolmap[plist[i]->getIdentifier()]->addPacket(plist[i]);
117 
118  delete plist[i];
119  }
120 
121  delete evt;
122 
123  for (auto iter : poolmap)
124  {
125  intt_pool *pool = iter.second; // less typing
126  if (pool->depth_ok())
127  {
128  int num_hits = pool->iValue(0, "NR_HITS");
129  if (Verbosity() > 1)
130  {
131  std::cout << "Number of Hits: " << num_hits << " for packet "
132  << pool->getIdentifier() << std::endl;
133  }
134  std::set<uint64_t> bclk_set;
135  for (int j = 0; j < num_hits; j++)
136  {
137  InttRawHit *newhit = new InttRawHitv1();
138  int FEE = pool->iValue(j, "FEE");
139  uint64_t gtm_bco = pool->lValue(j, "BCO");
140  newhit->set_packetid(pool->getIdentifier());
141  newhit->set_fee(FEE);
142  newhit->set_bco(gtm_bco);
143  newhit->set_adc(pool->iValue(j, "ADC"));
144  newhit->set_amplitude(pool->iValue(j, "AMPLITUDE"));
145  newhit->set_chip_id(pool->iValue(j, "CHIP_ID"));
146  newhit->set_channel_id(pool->iValue(j, "CHANNEL_ID"));
147  newhit->set_word(pool->iValue(j, "DATAWORD"));
148  newhit->set_FPHX_BCO(pool->iValue(j, "FPHX_BCO"));
149  newhit->set_full_FPHX(pool->iValue(j, "FULL_FPHX"));
150  newhit->set_full_ROC(pool->iValue(j, "FULL_ROC"));
151 
152  gtm_bco += m_Rollover[FEE];
153  bclk_set.insert(gtm_bco);
154  if (gtm_bco < m_PreviousClock[FEE])
155  {
156  m_Rollover[FEE] += 0x10000000000;
157  gtm_bco += 0x10000000000; // rollover makes sure our bclks are ascending even if we roll over the 40 bit counter
158  }
159  m_PreviousClock[FEE] = gtm_bco;
160  m_BeamClockFEE[gtm_bco].insert(FEE);
161  m_FEEBclkMap[FEE] = gtm_bco;
162  if (Verbosity() > 2)
163  {
164  std::cout << "evtno: " << EventSequence
165  << ", hits: " << j
166  << ", nr_hits: " << num_hits
167  << ", FEE: " << FEE
168  << ", bco: 0x" << std::hex << gtm_bco << std::dec
169  << ", channel: " << newhit->get_channel_id() << std::endl;
170  }
171  if (StreamingInputManager())
172  {
173  StreamingInputManager()->AddInttRawHit(gtm_bco, newhit);
174  }
175  m_InttRawHitMap[gtm_bco].push_back(newhit);
176  m_BclkStack.insert(gtm_bco);
177  }
178  // Print("FEEBCLK");
179  }
180  pool->next();
181  }
182  }
183 }
184 
186 {
187  if (what == "ALL" || what == "FEE")
188  {
189  for (const auto &bcliter : m_BeamClockFEE)
190  {
191  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
192  for (auto feeiter : bcliter.second)
193  {
194  std::cout << "FEM: " << feeiter << std::endl;
195  }
196  }
197  }
198  if (what == "ALL" || what == "FEEBCLK")
199  {
200  std::cout << "Printing last beamclock for every FEE" << std::endl;
201  for (auto bcliter : m_FEEBclkMap)
202  {
203  std::cout << "FEE" << bcliter.first << " bclk: 0x"
204  << std::hex << bcliter.second << std::dec << std::endl;
205  }
206  }
207  if (what == "ALL" || what == "STORAGE")
208  {
209  for (const auto &bcliter : m_InttRawHitMap)
210  {
211  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
212  for (auto feeiter : bcliter.second)
213  {
214  std::cout << "fee: " << feeiter->get_fee()
215  << " at " << std::hex << feeiter << std::dec << std::endl;
216  }
217  }
218  }
219  if (what == "ALL" || what == "STACK")
220  {
221  for (auto iter : m_BclkStack)
222  {
223  std::cout << "stacked bclk: 0x" << std::hex << iter << std::dec << std::endl;
224  }
225  }
226 }
227 
229 {
230  std::vector<uint64_t> toclearbclk;
231  for (const auto &iter : m_InttRawHitMap)
232  {
233  if (iter.first <= bclk)
234  {
235  for (auto pktiter : iter.second)
236  {
237  delete pktiter;
238  }
239  toclearbclk.push_back(iter.first);
240  }
241  else
242  {
243  break;
244  }
245  }
246  for (auto iter : toclearbclk)
247  {
248  m_BclkStack.erase(iter);
249  m_BeamClockFEE.erase(iter);
250  m_InttRawHitMap.erase(iter);
251  }
252 }
253 
254 bool SingleInttPoolInput::CheckPoolDepth(const uint64_t bclk)
255 {
256  for (auto iter : m_FEEBclkMap)
257  {
258  if (Verbosity() > 2)
259  {
260  std::cout << "my bclk 0x" << std::hex << iter.second
261  << " req: 0x" << bclk << std::dec << std::endl;
262  }
263  if (iter.second < bclk)
264  {
265  if (Verbosity() > 1)
266  {
267  std::cout << "FEE " << iter.first << " beamclock 0x" << std::hex << iter.second
268  << " smaller than req bclk: 0x" << bclk << std::dec << std::endl;
269  }
270  return false;
271  }
272  }
273  return true;
274 }
275 
277 {
278  // called interactively, to get rid of the current event
279  uint64_t currentbclk = *m_BclkStack.begin();
280  // std::cout << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
281  CleanupUsedPackets(currentbclk);
282  // m_BclkStack.erase(currentbclk);
283  // m_BeamClockFEE.erase(currentbclk);
284  return;
285 }
286 
287 bool SingleInttPoolInput::GetSomeMoreEvents(const uint64_t ibclk)
288 {
289  if (AllDone())
290  {
291  return false;
292  }
293  if (poolmap.empty())
294  {
295  return true;
296  }
297  uint64_t localbclk = ibclk;
298  if (ibclk == 0)
299  {
300  if (m_InttRawHitMap.empty())
301  {
302  return true;
303  }
304  localbclk = m_InttRawHitMap.begin()->first;
305  }
306 
307  for (auto bcliter : m_FEEBclkMap)
308  {
309  if (bcliter.second <= localbclk)
310  {
311  // std::cout << "FEE " << bcliter.first << " bclk: "
312  // << std::hex << bcliter.second << ", req: " << localbclk
313  // << std::dec << std::endl;
314  return true;
315  }
316  }
317  return false;
318 }
319 
321 {
322  PHNodeIterator iter(topNode);
323  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
324  if (!dstNode)
325  {
326  dstNode = new PHCompositeNode("DST");
327  topNode->addNode(dstNode);
328  }
329  PHNodeIterator iterDst(dstNode);
330  PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "INTT"));
331  if (!detNode)
332  {
333  detNode = new PHCompositeNode("INTT");
334  dstNode->addNode(detNode);
335  }
336  InttRawHitContainer *intthitcont = findNode::getClass<InttRawHitContainer>(detNode, "INTTRAWHIT");
337  if (!intthitcont)
338  {
339  intthitcont = new InttRawHitContainerv1();
340  PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(intthitcont, "INTTRAWHIT", "PHObject");
341  detNode->addNode(newNode);
342  }
343 }
344 //_______________________________________________________
345 
347 {
348  if (StreamingInputManager())
349  {
352  }
353 }