Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SingleTpcPoolInput.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file SingleTpcPoolInput.cc
1 #include "SingleTpcPoolInput.h"
2 
4 
7 
8 #include <frog/FROG.h>
9 
10 #include <phool/PHCompositeNode.h>
11 #include <phool/PHNodeIterator.h> // for PHNodeIterator
12 #include <phool/getClass.h>
13 #include <phool/phool.h>
14 
15 #include <Event/Event.h>
16 #include <Event/EventTypes.h>
17 #include <Event/Eventiterator.h>
18 #include <Event/fileEventiterator.h>
19 
20 #include <memory>
21 #include <set>
22 
23 const int NPACKETS = 2;
24 
26  : SingleStreamingInput(name)
27 {
29  plist = new Packet *[NPACKETS];
30 }
31 
33 {
34  delete[] plist;
35 }
36 
37 void SingleTpcPoolInput::FillPool(const unsigned int /*nbclks*/)
38 {
39  if (AllDone()) // no more files and all events read
40  {
41  return;
42  }
43  while (GetEventiterator() == nullptr) // at startup this is a null pointer
44  {
45  if (!OpenNextFile())
46  {
47  AllDone(1);
48  return;
49  }
50  }
51 // std::set<uint64_t> saved_beamclocks;
52  while (GetSomeMoreEvents())
53  {
54  std::unique_ptr<Event> evt( GetEventiterator()->getNextEvent() );
55  while (!evt)
56  {
57  fileclose();
58  if (!OpenNextFile())
59  {
60  AllDone(1);
61  return;
62  }
63  evt.reset( GetEventiterator()->getNextEvent() );
64  }
65  if (Verbosity() > 2)
66  {
67  std::cout << "Fetching next Event" << evt->getEvtSequence() << std::endl;
68  }
69  RunNumber(evt->getRunNumber());
70  if (GetVerbosity() > 1)
71  {
72  evt->identify();
73  }
74  if (evt->getEvtType() != DATAEVENT)
75  {
77  continue;
78  }
79  int EventSequence = evt->getEvtSequence();
80  int npackets = evt->getPacketList(plist, NPACKETS);
81 
82  if (npackets > NPACKETS)
83  {
84  exit(1);
85  }
86  for (int i = 0; i < npackets; i++)
87  {
88 
89  // keep pointer to local packet
90  auto& packet = plist[i];
91 
92  // get packet id
93  const auto packet_id = packet->getIdentifier();
94 
95  if (Verbosity() > 1)
96  {
97  packet->identify();
98  }
99 
100  // by default use previous bco clock for gtm bco
101  auto& previous_bco = m_packet_bco[packet_id];
102  uint64_t gtm_bco = previous_bco;
103 
104  uint64_t m_nTaggerInFrame = packet->lValue(0, "N_TAGGER");
105  for (uint64_t t = 0; t < m_nTaggerInFrame; t++)
106  {
107  // only store gtm_bco for level1 type of taggers (not ENDDAT)
108  const auto is_lvl1 = static_cast<uint8_t>(packet->lValue(t, "IS_LEVEL1_TRIGGER"));
109  if( is_lvl1 )
110  {
111  gtm_bco = packet->lValue(t, "BCO");
112  if (Verbosity() > 0)
113  {
114  std::cout << "bco: 0x" << std::hex << gtm_bco << std::dec << std::endl;
115  }
116  // store
117  previous_bco = gtm_bco;
118  }
119  }
120 
121  int m_nWaveormInFrame = packet->iValue(0, "NR_WF");
122  for (int wf = 0; wf < m_nWaveormInFrame; wf++)
123  {
124  TpcRawHit *newhit = new TpcRawHitv1();
125  int FEE = packet->iValue(wf, "FEE");
126  newhit->set_bco(packet->iValue(wf, "BCO"));
127 
128  // store gtm bco in hit
129  newhit->set_gtm_bco(gtm_bco);
130 
131  newhit->set_packetid(packet->getIdentifier());
132  newhit->set_fee(FEE);
133  newhit->set_channel(packet->iValue(wf, "CHANNEL"));
134  newhit->set_sampaaddress(packet->iValue(wf, "SAMPAADDRESS"));
135  newhit->set_sampachannel(packet->iValue(wf, "CHANNEL"));
136 
137 // // checksum and checksum error
138 // newhit->set_checksum( packet->iValue(iwf, "CHECKSUM") );
139 // newhit->set_checksum_error( packet->iValue(iwf, "CHECKSUMERROR") );
140 
141  // samples
142  const uint16_t samples = packet->iValue(wf, "SAMPLES");
143  newhit->set_samples( samples );
144 
145  // adc values
146  for( uint16_t is =0; is < samples; ++is )
147  { newhit->set_adc( is, packet->iValue( wf, is ) ); }
148 
149  m_BeamClockFEE[gtm_bco].insert(FEE);
150  m_FEEBclkMap[FEE] = gtm_bco;
151  if (Verbosity() > 2)
152  {
153  std::cout << "evtno: " << EventSequence
154  << ", hits: " << wf
155  << ", num waveforms: " << m_nWaveormInFrame
156  << ", bco: 0x" << std::hex << gtm_bco << std::dec
157  << ", FEE: " << FEE << std::endl;
158  }
159 // packet->convert();
160  if (StreamingInputManager())
161  {
162  StreamingInputManager()->AddTpcRawHit(gtm_bco, newhit);
163  }
164  m_TpcRawHitMap[gtm_bco].push_back(newhit);
165  m_BclkStack.insert(gtm_bco);
166  }
167  delete packet;
168  }
169  }
170 // } while (m_TpcRawHitMap.size() < 10 || CheckPoolDepth(m_TpcRawHitMap.begin()->first));
171 }
172 
173 void SingleTpcPoolInput::Print(const std::string &what) const
174 {
175  if (what == "ALL" || what == "FEE")
176  {
177  for (const auto &bcliter : m_BeamClockFEE)
178  {
179  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
180  for (auto feeiter : bcliter.second)
181  {
182  std::cout << "FEM: " << feeiter << std::endl;
183  }
184  }
185  }
186  if (what == "ALL" || what == "FEEBCLK")
187  {
188  for (auto bcliter : m_FEEBclkMap)
189  {
190  std::cout << "FEE" << bcliter.first << " bclk: 0x"
191  << std::hex << bcliter.second << std::dec << std::endl;
192  }
193  }
194  if (what == "ALL" || what == "STORAGE")
195  {
196  for (const auto &bcliter : m_TpcRawHitMap)
197  {
198  std::cout << "Beam clock 0x" << std::hex << bcliter.first << std::dec << std::endl;
199  for (auto feeiter : bcliter.second)
200  {
201  std::cout << "fee: " << feeiter->get_fee()
202  << " at " << std::hex << feeiter << std::dec << std::endl;
203  }
204  }
205  }
206  if (what == "ALL" || what == "STACK")
207  {
208  for (auto iter : m_BclkStack)
209  {
210  std::cout << "stacked bclk: 0x" << std::hex << iter << std::dec << std::endl;
211  }
212  }
213 }
214 
215 void SingleTpcPoolInput::CleanupUsedPackets(const uint64_t bclk)
216 {
217  std::vector<uint64_t> toclearbclk;
218  for (const auto &iter : m_TpcRawHitMap)
219  {
220  if (iter.first <= bclk)
221  {
222  for (auto pktiter : iter.second)
223  {
224  delete pktiter;
225  }
226  toclearbclk.push_back(iter.first);
227  }
228  else
229  {
230  break;
231  }
232  }
233  // for (auto iter : m_BeamClockFEE)
234  // {
235  // iter.second.clear();
236  // }
237 
238  for (auto iter : toclearbclk)
239  {
240  m_BclkStack.erase(iter);
241  m_BeamClockFEE.erase(iter);
242  m_TpcRawHitMap.erase(iter);
243  }
244 }
245 
246 bool SingleTpcPoolInput::CheckPoolDepth(const uint64_t bclk)
247 {
248  // if (m_FEEBclkMap.size() < 10)
249  // {
250  // std::cout << "not all FEEs in map: " << m_FEEBclkMap.size() << std::endl;
251  // return true;
252  // }
253  for (auto iter : m_FEEBclkMap)
254  {
255  if (Verbosity() > 2)
256  {
257  std::cout << "my bclk 0x" << std::hex << iter.second
258  << " req: 0x" << bclk << std::dec << std::endl;
259  }
260  if (iter.second < bclk)
261  {
262  if (Verbosity() > 1)
263  {
264  std::cout << "FEE " << iter.first << " beamclock 0x" << std::hex << iter.second
265  << " smaller than req bclk: 0x" << bclk << std::dec << std::endl;
266  }
267  return false;
268  }
269  }
270  return true;
271 }
272 
274 {
275  // called interactively, to get rid of the current event
276  uint64_t currentbclk = *m_BclkStack.begin();
277 // std::cout << "clearing bclk 0x" << std::hex << currentbclk << std::dec << std::endl;
278  CleanupUsedPackets(currentbclk);
279  // m_BclkStack.erase(currentbclk);
280  // m_BeamClockFEE.erase(currentbclk);
281  return;
282 }
283 
285 {
286  if (AllDone())
287  {
288  return false;
289  }
290  if (m_TpcRawHitMap.empty())
291  {
292  return true;
293  }
294 
295  uint64_t lowest_bclk = m_TpcRawHitMap.begin()->first;
296  lowest_bclk += m_BcoRange;
297  for (auto bcliter : m_FEEBclkMap)
298  {
299  if (bcliter.second <= lowest_bclk)
300  {
301  // std::cout << "FEE " << bcliter.first << " bclk: "
302  // << std::hex << bcliter.second << ", req: " << localbclk
303  // << std::dec << std::endl;
304  return true;
305  }
306  }
307  return false;
308  // if (CheckPoolDepth(m_TpcRawHitMap.begin()->first))
309  // {
310  // if (m_TpcRawHitMap.size() >= 10)
311  // {
312  // return false;
313  // }
314  // }
315  // return true;
316 }
317 
319 {
320  PHNodeIterator iter(topNode);
321  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
322  if (! dstNode)
323  {
324  dstNode = new PHCompositeNode("DST");
325  topNode->addNode(dstNode);
326  }
327  PHNodeIterator iterDst(dstNode);
328 PHCompositeNode *detNode = dynamic_cast<PHCompositeNode *>(iterDst.findFirst("PHCompositeNode", "TPC"));
329 if (!detNode)
330 {
331  detNode = new PHCompositeNode("TPC");
332  dstNode->addNode(detNode);
333 }
334  TpcRawHitContainer *tpchitcont = findNode::getClass<TpcRawHitContainer>(detNode,"TPCRAWHIT");
335  if (!tpchitcont)
336  {
337  tpchitcont = new TpcRawHitContainerv1();
338  PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(tpchitcont, "TPCRAWHIT", "PHObject");
339  detNode->addNode(newNode);
340  }
341 }
342 
344 {
345  if (StreamingInputManager())
346  {
349  }
350  return;
351 }