Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleMvtxPoolInput.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SingleMvtxPoolInput.cc
1 #include "SingleMvtxPoolInput.h"
2 
4 
8 
9 #include <frog/FROG.h>
10 
11 #include <phool/PHCompositeNode.h>
12 #include <phool/PHNodeIterator.h> // for PHNodeIterator
13 #include <phool/getClass.h>
14 #include <phool/phool.h>
15 
16 #include <Event/Event.h>
17 #include <Event/EventTypes.h>
18 #include <Event/Eventiterator.h>
19 #include <Event/fileEventiterator.h>
20 
21 #include <cassert>
22 #include <memory>
23 #include <set>
24 
26  : SingleStreamingInput(name)
27 {
28  plist = new Packet *[2];
29 }
30 
32 {
33  delete[] plist;
34 }
35 
36 void SingleMvtxPoolInput::FillPool(const unsigned int /*nbclks*/)
37 {
38  if (AllDone()) // no more files and all events read
39  {
40  return;
41  }
42  while (GetEventiterator() == nullptr) // at startup this is a null pointer
43  {
44  if (!OpenNextFile())
45  {
46  AllDone(1);
47  return;
48  }
49  }
50  // std::set<uint64_t> saved_beamclocks;
51  while (GetSomeMoreEvents())
52  {
53  std::unique_ptr<Event> evt(GetEventiterator()->getNextEvent());
54  while (!evt)
55  {
56  fileclose();
57  if (!OpenNextFile())
58  {
59  AllDone(1);
60  return;
61  }
62  evt.reset(GetEventiterator()->getNextEvent());
63  }
64  if (Verbosity() > 2)
65  {
66  std::cout << "Fetching next Event" << evt->getEvtSequence() << std::endl;
67  }
68  RunNumber(evt->getRunNumber());
69  if (GetVerbosity() > 1)
70  {
71  evt->identify();
72  }
73  if (evt->getEvtType() != DATAEVENT)
74  {
76  continue;
77  }
78  int EventSequence = evt->getEvtSequence();
79  int npackets = evt->getPacketList(plist, 2);
80 
81  if (npackets > 2)
82  {
83  exit(1);
84  }
85  for (int i = 0; i < npackets; i++)
86  {
87  // Ignoring packet not from MVTX detector
88  if (Verbosity() > 1)
89  {
90  plist[i]->identify();
91  }
92  int num_feeId = plist[i]->iValue(-1, "NR_LINKS");
93  if (Verbosity() > 1)
94  {
95  std::cout << "Number of feeid in RCDAQ events: " << num_feeId << " for packet "
96  << plist[i]->getIdentifier() << std::endl;
97  }
98  if (num_feeId > 0)
99  {
100  for (int i_fee{0}; i_fee < num_feeId; ++i_fee)
101  {
102  auto feeId = plist[i]->iValue(i_fee, "FEEID");
103  auto link = DecodeFeeid(feeId);
104  // auto hbfSize = plist[i]->iValue(feeId, "NR_HBF");
105  auto num_strobes = plist[i]->iValue(feeId, "NR_STROBES");
106  auto num_L1Trgs = plist[i]->iValue(feeId, "NR_PHYS_TRG");
107  for (int iL1 = 0; iL1 < num_L1Trgs; ++iL1)
108  {
109  auto l1Trg_bco = plist[i]->lValue(feeId, iL1, "L1_IR_BCO");
110  // auto l1Trg_bc = plist[i]->iValue(feeId, iL1, "L1_IR_BC");
111  gtmL1BcoSet.emplace(l1Trg_bco);
112  }
113 
114  m_FeeStrobeMap[feeId] += num_strobes;
115  for (int i_strb{0}; i_strb < num_strobes; ++i_strb)
116  {
117  auto strb_bco = plist[i]->lValue(feeId, i_strb, "TRG_IR_BCO");
118  auto strb_bc = plist[i]->iValue(feeId, i_strb, "TRG_IR_BC");
119  auto num_hits = plist[i]->iValue(feeId, i_strb, "TRG_NR_HITS");
120  if (Verbosity() > 4)
121  {
122  std::cout << "evtno: " << EventSequence << ", Fee: " << feeId;
123  std::cout << " Layer: " << link.layer << " Stave: " << link.stave;
124  std::cout << " GBT: " << link.gbtid << ", bco: 0x" << std::hex << strb_bco << std::dec;
125  std::cout << ", n_hits: " << num_hits << std::endl;
126  }
127  for (int i_hit{0}; i_hit < num_hits; ++i_hit)
128  {
129  auto chip_bc = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_BC");
130  auto chip_id = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_CHIP_ID");
131  auto chip_row = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_ROW");
132  auto chip_col = plist[i]->iValue(feeId, i_strb, i_hit, "HIT_COL");
133  MvtxRawHit *newhit = new MvtxRawHitv1();
134  newhit->set_bco(strb_bco);
135  newhit->set_strobe_bc(strb_bc);
136  newhit->set_chip_bc(chip_bc);
137  newhit->set_layer_id(link.layer);
138  newhit->set_stave_id(link.stave);
139  newhit->set_chip_id(3 * link.gbtid + chip_id);
140  newhit->set_row(chip_row);
141  newhit->set_col(chip_col);
142  if (StreamingInputManager())
143  {
144  StreamingInputManager()->AddMvtxRawHit(strb_bco, newhit);
145  }
146  m_MvtxRawHitMap[strb_bco].push_back(newhit);
147  }
148  if (StreamingInputManager())
149  {
150  StreamingInputManager()->AddMvtxFeeId(strb_bco, feeId);
151  }
152  m_BeamClockFEE[strb_bco].insert(feeId);
153  m_BclkStack.insert(strb_bco);
154  m_FEEBclkMap[feeId] = strb_bco;
155  }
156  }
157  }
158  // plist[i]->convert();
159  delete plist[i];
160  }
161  // Assign L1 trg to Strobe windows data.
162  for (auto &lv1Bco : gtmL1BcoSet)
163  {
164  auto it = m_BclkStack.lower_bound(lv1Bco);
165  auto const strb_it = (it == m_BclkStack.begin()) ? (*it == lv1Bco ? it : m_BclkStack.cend()) : --it;
166  if (strb_it != m_BclkStack.cend())
167  {
168  if (StreamingInputManager())
169  {
170  StreamingInputManager()->AddMvtxL1TrgBco(*strb_it, lv1Bco);
171  }
172  }
173  else if (m_BclkStack.empty())
174  {
175  continue;
176  }
177  else
178  {
179  std::cout << "ERROR: lv1Bco: 0x" << std::hex << lv1Bco << std::dec
180  << " is less than minimun strobe bco 0x" << std::hex
181  << *m_BclkStack.begin() << std::dec << std::endl;
182  assert(0);
183  }
184  }
185  gtmL1BcoSet.clear();
186  }
187 }
188 
190 {
191  // TODO: adapt to MVTX case
192  if (what == "ALL" || what == "FEE")
193  {
194  for (const auto &bcliter : m_BeamClockFEE)
195  {
196  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
197  for (const auto feeiter : bcliter.second)
198  {
199  std::cout << "FEM: " << feeiter << std::endl;
200  }
201  }
202  }
203  if (what == "ALL" || what == "FEEBCLK")
204  {
205  for (auto bcliter : m_FEEBclkMap)
206  {
207  std::cout << "FEE" << bcliter.first << " bclk: 0x"
208  << std::hex << bcliter.second << std::dec << std::endl;
209  }
210  }
211  if (what == "ALL" || what == "STORAGE")
212  {
213  for (const auto &bcliter : m_MvtxRawHitMap)
214  {
215  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
216  for (const auto &feeiter : bcliter.second)
217  {
218  std::cout << "fee: " << feeiter->get_stave_id()
219  << " at " << std::hex << feeiter << std::dec << std::endl;
220  }
221  }
222  }
223  if (what == "ALL" || what == "STACK")
224  {
225  for (const auto &iter : m_BclkStack)
226  {
227  std::cout << "stacked bclk: 0x" << std::hex << iter << std::dec << std::endl;
228  }
229  }
230  if (what == "ALL" || what == "GET_NR_STROBES")
231  {
232  for (const auto &iter : m_FeeStrobeMap)
233  {
234  std::cout << "Total number of strobes for feeid: " << iter.first << ", " << iter.second << std::endl;
235  }
236  }
237 }
238 
240 {
241  std::vector<uint64_t> toclearbclk;
242  for (const auto &iter : m_MvtxRawHitMap)
243  {
244  if (iter.first <= bclk)
245  {
246  for (auto pktiter : iter.second)
247  {
248  delete pktiter;
249  }
250  toclearbclk.push_back(iter.first);
251  }
252  else
253  {
254  break;
255  }
256  }
257  // for (auto iter : m_BeamClockFEE)
258  // {
259  // iter.second.clear();
260  // }
261 
262  for (auto iter : toclearbclk)
263  {
264  m_BclkStack.erase(iter);
265  m_BeamClockFEE.erase(iter);
266  m_MvtxRawHitMap.erase(iter);
267  gtmL1BcoSet.erase(iter);
268  m_FeeStrobeMap.erase(iter);
269  }
270 }
271 
272 bool SingleMvtxPoolInput::CheckPoolDepth(const uint64_t bclk)
273 {
274  // if (m_FEEBclkMap.size() < 10)
275  // {
276  // std::cout << "not all FEEs in map: " << m_FEEBclkMap.size() << std::endl;
277  // return true;
278  // }
279  for (auto iter : m_FEEBclkMap)
280  {
281  if (Verbosity() > 2)
282  {
283  std::cout << iter.first << " my bclk 0x" << std::hex << iter.second
284  << " req: 0x" << bclk << std::dec << std::endl;
285  }
286  // equal case when we have more strobe with same bco
287  // due not synchronization
288  if (iter.second <= bclk)
289  {
290  if (Verbosity() > 1)
291  {
292  std::cout << "FEE " << iter.first << " beamclock 0x" << std::hex << iter.second
293  << " smaller than req bclk: 0x" << bclk << std::dec << std::endl;
294  }
295  return false;
296  }
297  }
298  return true;
299 }
300 
302 {
303  // called interactively, to get rid of the current event
304  uint64_t currentbclk = *m_BclkStack.begin();
305  // std::cout << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
306  CleanupUsedPackets(currentbclk);
307  // m_BclkStack.erase(currentbclk);
308  // m_BeamClockFEE.erase(currentbclk);
309  return;
310 }
311 
313 {
314  if (AllDone())
315  {
316  return false;
317  }
318  if (m_MvtxRawHitMap.empty())
319  {
320  return true;
321  }
322  uint64_t lowest_bclk = m_MvtxRawHitMap.begin()->first;
323  // lowest_bclk += m_BcoRange;
324  lowest_bclk += m_BcoRange;
325  for (auto bcliter : m_FEEBclkMap)
326  {
327  if (bcliter.second <= lowest_bclk)
328  {
329  // std::cout << "FEE " << bcliter.first << " bclk: "
330  // << std::hex << bcliter.second << ", req: " << localbclk
331  // << std::dec << std::endl;
332  return true;
333  }
334  }
335  return false;
336 
337  // if (CheckPoolDepth(m_MvtxRawHitMap.begin()->first))
338  // {
339  // if (m_MvtxRawHitMap.size() >= 200)
340  // {
341  // return false;
342  // }
343  // // }
344  // return true;
345 }
346 
348 {
349  PHNodeIterator iter(topNode);
350  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
351  if (!dstNode)
352  {
353  dstNode = new PHCompositeNode("DST");
354  topNode->addNode(dstNode);
355  }
356  PHNodeIterator iterDst(dstNode);
357  PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "MVTX"));
358  if (!detNode)
359  {
360  detNode = new PHCompositeNode("MVTX");
361  dstNode->addNode(detNode);
362  }
363 
364  MvtxRawEvtHeader* mvtxEH = findNode::getClass<MvtxRawEvtHeaderv1>(detNode,"MVTXRAWEVTHEADER");
365  if (! mvtxEH)
366  {
367  mvtxEH = new MvtxRawEvtHeaderv1();
368  PHIODataNode<PHObject>* newNode = new PHIODataNode<PHObject>(mvtxEH, "MVTXRAWEVTHEADER", "PHObject");
369  detNode->addNode(newNode);
370  }
371 
372  MvtxRawHitContainer *mvtxhitcont = findNode::getClass<MvtxRawHitContainer>(detNode, "MVTXRAWHIT");
373  if (!mvtxhitcont)
374  {
375  mvtxhitcont = new MvtxRawHitContainerv1();
376  PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(mvtxhitcont, "MVTXRAWHIT", "PHObject");
377  detNode->addNode(newNode);
378  }
379 }
380 
382 {
383  if (StreamingInputManager())
384  {
387  }
388  return;
389 }