Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MvtxPrototype2UnpackPRDF.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MvtxPrototype2UnpackPRDF.cc
2 
3 #include <Event/Event.h>
4 #include <Event/EventTypes.h>
5 #include <Event/packetConstants.h>
6 #include <Event/packet.h>
7 #include <Event/packet_hbd_fpgashort.h>
9 #include <phool/phool.h>
10 #include <phool/getClass.h>
12 #include <pdbcalbase/PdbParameterMap.h>
13 #include <phparameter/PHParameters.h>
14 
15 #include <trackbase/TrkrDefs.h>
17 #include <trackbase/TrkrHitSet.h>
18 //#include <trackbase/TrkrHitSetv1.h>
19 #include <mvtx/MvtxDefs.h>
20 #include <mvtx/MvtxHit.h>
21 //#include <mvtx/MvtxHitSetv1.h>
22 
23 #include <cassert>
24 #include <iostream>
25 #include <string>
26 
27 using namespace std;
28 
29 map< pair< int, int>, pair< int, int> > MvtxPrototype2UnpackPRDF::s_map_chips =
30 {
31  {{1,1}, {0,0}},
32  {{1,2}, {0,1}},
33  {{1,3}, {0,2}},
34  {{1,4}, {0,3}},
35  {{1,5}, {0,4}},
36  {{1,6}, {0,5}},
37  {{1,7}, {0,6}},
38  {{1,8}, {0,7}},
39  {{1,9}, {3,8}},
40  {{1,10}, {3,7}},
41  {{1,11}, {3,6}},
42  {{1,12}, {3,5}},
43  {{1,13}, {3,4}},
44  {{1,14}, {3,3}},
45  {{1,15}, {3,2}},
46  {{1,16}, {3,1}},
47  {{1,17}, {3,0}},
48  {{1,18}, {2,8}},
49  {{1,19}, {2,7}},
50  {{1,20}, {2,6}},
51  {{1,21}, {2,5}},
52  {{1,22}, {2,4}},
53  {{1,23}, {2,3}},
54  {{1,24}, {2,2}},
55  {{1,25}, {2,1}},
56  {{1,26}, {2,0}},
57  {{1,27}, {0,8}},
58  {{2,1}, {1,2}},
59  {{2,2}, {1,1}},
60  {{2,3}, {1,0}},
61  {{2,4}, {1,3}},
62  {{2,5}, {1,4}},
63  {{2,6}, {1,5}},
64  {{2,7}, {1,6}},
65  {{2,8}, {1,7}},
66  {{2,9}, {1,8}}
67  //order
68  //E103
69  //C105
70  //C104
71  //A105
72 }; //<ruid, ruch> to <stave, chipID>
73 
82 map<int, int> MvtxPrototype2UnpackPRDF::s_map_layers = { {0,0}, {1,1}, {2,2}, {3,3} };
83 
84 //____________________________________
86  SubsysReco("MvtxPrototype2UnpackPRDF"),
87  /*PHCompositeNode **/ dstNode(NULL),
88  /*Event**/_event(NULL),
89  /*Packet_hbd_fpgashort**/_packet(NULL),
90  /*int*/_nevents(0),
91  /*bool*/_first(true)
92 {
93  for (int istave=0; istave<NLAYER; istave++){
94  for (int ichip=0; ichip<NCHIP; ichip++){
95  _nevent_per_chip[istave][ichip] = 0;
96  _npixel_per_chip[istave][ichip] = 0;
97  }
98  }
99 }
100 
101 //____________________________________
102 int
104 {
105 
106  cout << "-----MvtxPrototype2UnpackPRDF::Init-----" << endl;
108 }
109 
110 //_____________________________________
111 int
113 {
114 
115  CreateNodeTree(topNode);
116 
117  cout << "-----MvtxPrototype2UnpackPRDF::Init-----" << endl;
119 }
120 
121 //____________________________________
122 int
124 {
125  _event = findNode::getClass<Event>(topNode, "PRDF");
126  if (_event==0)
127  {
128  cout << "MvtxPrototype2UnpackPRDF::Process_Event - Event not found" << endl;
130  }
131 
132  if ( _event->getEvtType() == BEGRUNEVENT )
133  {
134  cout << __FILE__ << " - run event header found. Aborting "<< endl;
135  _nevents = 0;
137  }
138 
139  if ( _event->getEvtType() != DATAEVENT )
140  {
141  cout << "MvtxPrototype2UnpackPRDF::Process_Event - non-data event type " \
142  << _event->getEvtType() << ". Aborting."<< endl;
144  }
145 
146  _nevents++;
147 
148  _hitsetcon = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
149  if ( _hitsetcon == 0 )
150  {
151  cout << "MvtxPrototype2UnpackPRDF::Process_Event - TRKR_HITSET not found" << endl;
153  }
154 
155  /*
156  else{
157 
158  MvtxDefUtil *mvtxdef = new MvtxDefUtil();
159 
160  for (int ichip=0; ichip<NCHIP; ichip++){
161  MvtxHitSetv1 *hitset = new MvtxHitSetv1();
162  hitset->SetHitSetKey(mvtxdef->GenHitSetKey(char(ichip),uint8_t(0),uint8_t(ichip)));
163  _hitsetcon->AddHitSetSpecifyKey(hitset->GetHitSetKey(),hitset);
164  if ( _verbosity>10 )
165  {
166  cout << "-----MvtxPrototype2UnpackPRDF::CreateNodeTree, Create hitset " << hitset->GetHitSetKey() << endl;
167  }
168  }
169 
170  if ( _verbosity>10 )
171  {
172  cout << "-----MvtxPrototype2UnpackPRDF::process_event Create HitSet" << endl;
173  _hitsetcon->identify();
174  }
175  delete mvtxdef;
176  }
177  */
178 
179  if ( Verbosity()>90 )
180  {
181  cout << "EVENT: " << _nevents << endl;
182  }
183 
184  MakeHits();
185  double significand = _nevents / pow(10, (int) (log10(_nevents)));
186  if ( Verbosity() >= VERBOSITY_MORE && fmod(significand,1.0) == 0 && significand <=10 )
187  {
188  cout << "-----MvtxPrototype2UnpackPRDF::process_event Check HitSetCon" << endl;
189  //_hitsetcon->identify();
190  for (int istave=0; istave<NLAYER; istave++)
191  {
192  for (int ichip=0; ichip<NCHIP; ichip++)
193  {
194  TrkrDefs::hitsetkey mvtx_hitsetkey =
195  MvtxDefs::genHitSetKey(uint8_t(istave), 0, uint8_t(ichip));
196  TrkrHitSet *hitset =
197  static_cast<TrkrHitSet*>(_hitsetcon->findHitSet(mvtx_hitsetkey));
198 
199  if ( hitset )
200  {
201  int nhits = hitset->size();
202  cout << "Stave: " << istave
203  << " Chip: " << ichip << " Nhits: " << nhits << endl;
204  }
205  }
206  }
207  }
208 
210 
211 }
212 
213 //_______________________________________
214 void
216 {
217 
218  PHNodeIterator nodeItr(topNode);
219  //DST node
220  dstNode = static_cast<PHCompositeNode *>(nodeItr.findFirst("PHCompositeNode",
221  "DST"));
222  if (!dstNode)
223  {
224  cout << "PHComposite node created: DST" << endl;
225  dstNode = new PHCompositeNode("DST");
226  topNode->addNode(dstNode);
227  }
228 
229  PHCompositeNode* trkrNode
230  = dynamic_cast<PHCompositeNode*>(nodeItr.findFirst("PHCompositeNode","TRKR"));
231  if (!trkrNode) {
232  trkrNode = new PHCompositeNode("TRKR");
233  dstNode->addNode(trkrNode);
234  }
235 
236  TrkrHitSetContainer *hitsetcon = findNode::getClass<TrkrHitSetContainer>(dstNode,"TRKR_HITSET");
237  if (!hitsetcon) {
238  hitsetcon = new TrkrHitSetContainer();
239  PHIODataNode<PHObject> *hitsetconNode =
240  new PHIODataNode<PHObject>(hitsetcon,"TRKR_HITSET","PHObject");
241  trkrNode->addNode(hitsetconNode);
242  }//
243 
244  //DATA nodes
245  /*
246  data_node = static_cast<PHCompositeNode *>(nodeItr.findFirst("PHCompositeNode",
247  "RAW_DATA"));
248  if (!data_node)
249  {
250  cout << "PHComposite node created: RAW_DATA" << endl;
251  data_node = new PHCompositeNode("RAW_DATA");
252  dst_node->addNode(data_node);
253  }
254  */
255 
256 }
257 
258 //_______________________________________
259 void
261 {
262 
263  //int nhits_for_this = 0;
264  Packet *p = _event->getPacket(2000);
265  if (p)
266  {
267  bool event_err = false;
268 
269  if ( !event_err )
270  {
271  for (int iru=0; iru<NMAXRU+1; iru++)
272  {
273 /*
274  if ( p->iValue(iru)==-1 )
275  {
276  cout << "No such RU: " << iru << endl;
277  continue;
278  }
279  if ( p->iValue(iru, "BAD_RUIDS") )
280  {
281  cout << "Bad RU: " << iru << endl;
282  continue;
283  }
284 */
285  if ( p->iValue(iru) != -1 )
286  {
287  for ( int ich = 0; ich < NMAXRUCHN+1; ich++)
288  {
289  if ( p->iValue(iru, ich) > 0 )
290  {
291 /*
292  //int chip_id = p->iValue(iru, ich, "CHIP_ID");
293  //skip masked or dead chip
294  if ( chip_id==-1 )
295  {
296  continue;
297  }
298 */
299  if ( s_map_chips.count({iru,ich}) != 1 )
300  {
301  cout << PHWHERE << "invalid: (iru " << iru << ", ich " \
302  << ich << ") " << endl;
303  }
304  else
305  {
306  std::pair<int,int> chip_pos = s_map_chips[{iru, ich}];
307  int stave_id_from_map = chip_pos.first;
308  int chip_id_from_map = chip_pos.second;
309 
310  //skip unused channel
311  if ( stave_id_from_map==-1 || chip_id_from_map==-1 )
312  {
313  continue;
314  }
315  /*
316  //skip inconsistent chip id
317  if ( chip_id!=chip_id_from_map )
318  {
319  cout << "WARNING!! Event: " << _nevents << " Inconsistent chip ID"
320  << " from packet: " << chip_id
321  << " from mapping: " << chip_id_from_map << endl;
322  }
323 
324  if ( Verbosity()>100 )
325  cout << "iru: " << iru << " ich: " << ich << " chip_id from packet: " \
326  << chip_id << " chip_id from map: " << chip_id_from_map << endl;
327 
328  int excess_data_bytes = p->iValue(iru, ich, "EXCESS_DATA_BYTES");
329  if ( excess_data_bytes>0 )
330  {
331  cout << "WARNING!! Event: " << _nevents << " Data found past chip trailer" \
332  << " EXCESS_DATA_BYTES: " << excess_data_bytes << endl;
333  }
334 
335  int bad_bytes = p->iValue(iru, ich, "BAD_BYTES");
336  if ( bad_bytes>0 )
337  {
338  cout << "WARNING!! Event: " << _nevents << " Bad data found" \
339  << " BAD_BYTES: " << bad_bytes << endl;
340  }
341 
342  int header_found = p->iValue(iru, ich, "HEADER_FOUND");
343  int trailer_found = p->iValue(iru, ich, "TRAILER_FOUND");
344  int bunchcounter = p->iValue(iru, ich, "BUNCHCOUNTER");
345 
346  if ( header_found==0 || trailer_found==0 )
347  {
348  if ( Verbosity()>50 )
349  {
350  cout << "WARNING!! Event: " << _nevents << " Missing RU " \
351  << iru << " CH: " << ich << " HEADER_FOUND: " << header_found \
352  << " TRAILER_FOUND: " << trailer_found << " BUNCHCOUNTER: " \
353  << bunchcounter << endl;
354  }
355  //continue;
356  }
357  */
358  int nhits_per_ch = p->iValue(iru, ich);
359  for (int ihit = 0; ihit < nhits_per_ch; ihit++)
360  {
361  int encoded_hit = p->iValue(iru, ich, ihit);
362  int row_hit = DecodeRow(encoded_hit);
363  int col_hit = DecodeCol(encoded_hit);
364 
365  if ( row_hit>=NROW || col_hit>=NCOL )
366  {
367  cout << PHWHERE << "WARNING!! Event: " << _nevents \
368  << " Hit out of window"
369  << " RU: " << iru
370  << " CH: " << ich
371  << " ROW: " << row_hit
372  << " COL: " << col_hit
373  << endl;
374  continue;
375  }
376 
377  _npixel_per_chip[stave_id_from_map][chip_id_from_map]++;
378 
379  auto lay = s_map_layers[stave_id_from_map];
381  MvtxDefs::genHitSetKey(lay, 0, chip_id_from_map);
382  TrkrHitSetContainer::Iterator hitsetit = \
383  _hitsetcon->findOrAddHitSet(hitsetkey);
384 
385  // generate the key for this hit
387  MvtxDefs::genHitKey(uint16_t(col_hit),uint16_t(row_hit));
388  // See if this hit already exists
389  TrkrHit *hit = hitsetit->second->getHit(hitkey);
390  if ( !hit )
391  {
392  hit = new MvtxHit();
393  hitsetit->second->addHitSpecificKey(hitkey, hit);
394  }
395  else
396  {
397  cout << PHWHERE << "WARNING!! Event: " << _nevents
398  << " duplicated Hit "
399  << " STAVE: " << stave_id_from_map
400  << " CHIP: " << chip_id_from_map
401  << " ROW: " << row_hit
402  << " COL: " << col_hit
403  << endl;
404  }
405 
406  }//ihit
407 
408  _nevent_per_chip[stave_id_from_map][chip_id_from_map]++;
409  } //if good ru,ch
410  } //if ru,ch have data
411  }//ich
412  }//if iru has data
413  }//iru
414  }// !err
415 
416  delete p;
417  }//p
418 
419 }
420 
421 //___________________________________
422 int
424 {
425  return (val >> 16);
426 }
427 
428 //___________________________________
429 int
431 {
432  return (val & 0xffff);
433 }
434 
435 //___________________________________
436 int
438 {
439 
440  if ( Verbosity() > VERBOSITY_SOME )
441  {
442  cout << "-----MvtxPrototype2UnpackPRDF::End::PrintSummary-----" << endl;
443  for (int istave=0; istave<NLAYER; istave++){
444  for (int ichip=0; ichip<NCHIP; ichip++){
445 
446  cout << "LAYER: " << istave << ", CHIP: " << ichip << ", "
447  << ", N EVENT: " << _nevent_per_chip[istave][ichip]
448  << ", N HITS: " << _npixel_per_chip[istave][ichip]
449  << endl;
450  }
451  }
452 
453  }
454 
456 }
457