File indexing completed on 2025-12-19 09:24:40
0001 #include "MvtxPrototype2UnpackPRDF.h"
0002
0003 #include <Event/Event.h>
0004 #include <Event/EventTypes.h>
0005 #include <Event/packetConstants.h>
0006 #include <Event/packet.h>
0007 #include <Event/packet_hbd_fpgashort.h>
0008 #include <phool/PHCompositeNode.h>
0009 #include <phool/phool.h>
0010 #include <phool/getClass.h>
0011 #include <fun4all/Fun4AllReturnCodes.h>
0012 #include <pdbcalbase/PdbParameterMap.h>
0013 #include <phparameter/PHParameters.h>
0014
0015 #include <trackbase/TrkrDefs.h>
0016 #include <trackbase/TrkrHitSetContainer.h>
0017 #include <trackbase/TrkrHitSet.h>
0018
0019 #include <mvtx/MvtxDefs.h>
0020 #include <mvtx/MvtxHit.h>
0021
0022
0023 #include <cassert>
0024 #include <iostream>
0025 #include <string>
0026
0027 using namespace std;
0028
0029 map< pair< int, int>, pair< int, int> > MvtxPrototype2UnpackPRDF::s_map_chips =
0030 {
0031 {{1,1}, {0,0}},
0032 {{1,2}, {0,1}},
0033 {{1,3}, {0,2}},
0034 {{1,4}, {0,3}},
0035 {{1,5}, {0,4}},
0036 {{1,6}, {0,5}},
0037 {{1,7}, {0,6}},
0038 {{1,8}, {0,7}},
0039 {{1,9}, {3,8}},
0040 {{1,10}, {3,7}},
0041 {{1,11}, {3,6}},
0042 {{1,12}, {3,5}},
0043 {{1,13}, {3,4}},
0044 {{1,14}, {3,3}},
0045 {{1,15}, {3,2}},
0046 {{1,16}, {3,1}},
0047 {{1,17}, {3,0}},
0048 {{1,18}, {2,8}},
0049 {{1,19}, {2,7}},
0050 {{1,20}, {2,6}},
0051 {{1,21}, {2,5}},
0052 {{1,22}, {2,4}},
0053 {{1,23}, {2,3}},
0054 {{1,24}, {2,2}},
0055 {{1,25}, {2,1}},
0056 {{1,26}, {2,0}},
0057 {{1,27}, {0,8}},
0058 {{2,1}, {1,2}},
0059 {{2,2}, {1,1}},
0060 {{2,3}, {1,0}},
0061 {{2,4}, {1,3}},
0062 {{2,5}, {1,4}},
0063 {{2,6}, {1,5}},
0064 {{2,7}, {1,6}},
0065 {{2,8}, {1,7}},
0066 {{2,9}, {1,8}}
0067
0068
0069
0070
0071
0072 };
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 map<int, int> MvtxPrototype2UnpackPRDF::s_map_layers = { {0,0}, {1,1}, {2,2}, {3,3} };
0083
0084
0085 MvtxPrototype2UnpackPRDF::MvtxPrototype2UnpackPRDF() :
0086 SubsysReco("MvtxPrototype2UnpackPRDF"),
0087 dstNode(NULL),
0088 _event(NULL),
0089 _packet(NULL),
0090 _nevents(0),
0091 _first(true)
0092 {
0093 for (int istave=0; istave<NLAYER; istave++){
0094 for (int ichip=0; ichip<NCHIP; ichip++){
0095 _nevent_per_chip[istave][ichip] = 0;
0096 _npixel_per_chip[istave][ichip] = 0;
0097 }
0098 }
0099 }
0100
0101
0102 int
0103 MvtxPrototype2UnpackPRDF::Init(PHCompositeNode *topNode)
0104 {
0105
0106 cout << "-----MvtxPrototype2UnpackPRDF::Init-----" << endl;
0107 return Fun4AllReturnCodes::EVENT_OK;
0108 }
0109
0110
0111 int
0112 MvtxPrototype2UnpackPRDF::InitRun(PHCompositeNode *topNode)
0113 {
0114
0115 CreateNodeTree(topNode);
0116
0117 cout << "-----MvtxPrototype2UnpackPRDF::Init-----" << endl;
0118 return Fun4AllReturnCodes::EVENT_OK;
0119 }
0120
0121
0122 int
0123 MvtxPrototype2UnpackPRDF::process_event(PHCompositeNode *topNode)
0124 {
0125 _event = findNode::getClass<Event>(topNode, "PRDF");
0126 if (_event==0)
0127 {
0128 cout << "MvtxPrototype2UnpackPRDF::Process_Event - Event not found" << endl;
0129 return Fun4AllReturnCodes::ABORTEVENT;
0130 }
0131
0132 if ( _event->getEvtType() == BEGRUNEVENT )
0133 {
0134 cout << __FILE__ << " - run event header found. Aborting "<< endl;
0135 _nevents = 0;
0136 return Fun4AllReturnCodes::ABORTEVENT;
0137 }
0138
0139 if ( _event->getEvtType() != DATAEVENT )
0140 {
0141 cout << "MvtxPrototype2UnpackPRDF::Process_Event - non-data event type " \
0142 << _event->getEvtType() << ". Aborting."<< endl;
0143 return Fun4AllReturnCodes::ABORTEVENT;
0144 }
0145
0146 _nevents++;
0147
0148 _hitsetcon = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
0149 if ( _hitsetcon == 0 )
0150 {
0151 cout << "MvtxPrototype2UnpackPRDF::Process_Event - TRKR_HITSET not found" << endl;
0152 return Fun4AllReturnCodes::ABORTEVENT;
0153 }
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 if ( Verbosity()>90 )
0180 {
0181 cout << "EVENT: " << _nevents << endl;
0182 }
0183
0184 MakeHits();
0185 double significand = _nevents / pow(10, (int) (log10(_nevents)));
0186 if ( Verbosity() >= VERBOSITY_MORE && fmod(significand,1.0) == 0 && significand <=10 )
0187 {
0188 cout << "-----MvtxPrototype2UnpackPRDF::process_event Check HitSetCon" << endl;
0189
0190 for (int istave=0; istave<NLAYER; istave++)
0191 {
0192 for (int ichip=0; ichip<NCHIP; ichip++)
0193 {
0194 TrkrDefs::hitsetkey mvtx_hitsetkey =
0195 MvtxDefs::genHitSetKey(uint8_t(istave), 0, uint8_t(ichip));
0196 TrkrHitSet *hitset =
0197 static_cast<TrkrHitSet*>(_hitsetcon->findHitSet(mvtx_hitsetkey));
0198
0199 if ( hitset )
0200 {
0201 int nhits = hitset->size();
0202 cout << "Stave: " << istave
0203 << " Chip: " << ichip << " Nhits: " << nhits << endl;
0204 }
0205 }
0206 }
0207 }
0208
0209 return Fun4AllReturnCodes::EVENT_OK;
0210
0211 }
0212
0213
0214 void
0215 MvtxPrototype2UnpackPRDF::CreateNodeTree(PHCompositeNode *topNode)
0216 {
0217
0218 PHNodeIterator nodeItr(topNode);
0219
0220 dstNode = static_cast<PHCompositeNode *>(nodeItr.findFirst("PHCompositeNode",
0221 "DST"));
0222 if (!dstNode)
0223 {
0224 cout << "PHComposite node created: DST" << endl;
0225 dstNode = new PHCompositeNode("DST");
0226 topNode->addNode(dstNode);
0227 }
0228
0229 PHCompositeNode* trkrNode
0230 = dynamic_cast<PHCompositeNode*>(nodeItr.findFirst("PHCompositeNode","TRKR"));
0231 if (!trkrNode) {
0232 trkrNode = new PHCompositeNode("TRKR");
0233 dstNode->addNode(trkrNode);
0234 }
0235
0236 TrkrHitSetContainer *hitsetcon = findNode::getClass<TrkrHitSetContainer>(dstNode,"TRKR_HITSET");
0237 if (!hitsetcon) {
0238 hitsetcon = new TrkrHitSetContainer();
0239 PHIODataNode<PHObject> *hitsetconNode =
0240 new PHIODataNode<PHObject>(hitsetcon,"TRKR_HITSET","PHObject");
0241 trkrNode->addNode(hitsetconNode);
0242 }
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256 }
0257
0258
0259 void
0260 MvtxPrototype2UnpackPRDF::MakeHits()
0261 {
0262
0263
0264 Packet *p = _event->getPacket(2000);
0265 if (p)
0266 {
0267 bool event_err = false;
0268
0269 if ( !event_err )
0270 {
0271 for (int iru=0; iru<NMAXRU+1; iru++)
0272 {
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284
0285 if ( p->iValue(iru) != -1 )
0286 {
0287 for ( int ich = 0; ich < NMAXRUCHN+1; ich++)
0288 {
0289 if ( p->iValue(iru, ich) > 0 )
0290 {
0291
0292
0293
0294
0295
0296
0297
0298
0299 if ( s_map_chips.count({iru,ich}) != 1 )
0300 {
0301 cout << PHWHERE << "invalid: (iru " << iru << ", ich " \
0302 << ich << ") " << endl;
0303 }
0304 else
0305 {
0306 std::pair<int,int> chip_pos = s_map_chips[{iru, ich}];
0307 int stave_id_from_map = chip_pos.first;
0308 int chip_id_from_map = chip_pos.second;
0309
0310
0311 if ( stave_id_from_map==-1 || chip_id_from_map==-1 )
0312 {
0313 continue;
0314 }
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358 int nhits_per_ch = p->iValue(iru, ich);
0359 for (int ihit = 0; ihit < nhits_per_ch; ihit++)
0360 {
0361 int encoded_hit = p->iValue(iru, ich, ihit);
0362 int row_hit = DecodeRow(encoded_hit);
0363 int col_hit = DecodeCol(encoded_hit);
0364
0365 if ( row_hit>=NROW || col_hit>=NCOL )
0366 {
0367 cout << PHWHERE << "WARNING!! Event: " << _nevents \
0368 << " Hit out of window"
0369 << " RU: " << iru
0370 << " CH: " << ich
0371 << " ROW: " << row_hit
0372 << " COL: " << col_hit
0373 << endl;
0374 continue;
0375 }
0376
0377 _npixel_per_chip[stave_id_from_map][chip_id_from_map]++;
0378
0379 auto lay = s_map_layers[stave_id_from_map];
0380 TrkrDefs::hitsetkey hitsetkey = \
0381 MvtxDefs::genHitSetKey(lay, 0, chip_id_from_map);
0382 TrkrHitSetContainer::Iterator hitsetit = \
0383 _hitsetcon->findOrAddHitSet(hitsetkey);
0384
0385
0386 TrkrDefs::hitkey hitkey = \
0387 MvtxDefs::genHitKey(uint16_t(col_hit),uint16_t(row_hit));
0388
0389 TrkrHit *hit = hitsetit->second->getHit(hitkey);
0390 if ( !hit )
0391 {
0392 hit = new MvtxHit();
0393 hitsetit->second->addHitSpecificKey(hitkey, hit);
0394 }
0395 else
0396 {
0397 cout << PHWHERE << "WARNING!! Event: " << _nevents
0398 << " duplicated Hit "
0399 << " STAVE: " << stave_id_from_map
0400 << " CHIP: " << chip_id_from_map
0401 << " ROW: " << row_hit
0402 << " COL: " << col_hit
0403 << endl;
0404 }
0405
0406 }
0407
0408 _nevent_per_chip[stave_id_from_map][chip_id_from_map]++;
0409 }
0410 }
0411 }
0412 }
0413 }
0414 }
0415
0416 delete p;
0417 }
0418
0419 }
0420
0421
0422 int
0423 MvtxPrototype2UnpackPRDF::DecodeRow(int val) const
0424 {
0425 return (val >> 16);
0426 }
0427
0428
0429 int
0430 MvtxPrototype2UnpackPRDF::DecodeCol(int val) const
0431 {
0432 return (val & 0xffff);
0433 }
0434
0435
0436 int
0437 MvtxPrototype2UnpackPRDF::End(PHCompositeNode *topNode)
0438 {
0439
0440 if ( Verbosity() > VERBOSITY_SOME )
0441 {
0442 cout << "-----MvtxPrototype2UnpackPRDF::End::PrintSummary-----" << endl;
0443 for (int istave=0; istave<NLAYER; istave++){
0444 for (int ichip=0; ichip<NCHIP; ichip++){
0445
0446 cout << "LAYER: " << istave << ", CHIP: " << ichip << ", "
0447 << ", N EVENT: " << _nevent_per_chip[istave][ichip]
0448 << ", N HITS: " << _npixel_per_chip[istave][ichip]
0449 << endl;
0450 }
0451 }
0452
0453 }
0454
0455 return Fun4AllReturnCodes::EVENT_OK;
0456 }
0457