Back to home page

sPhenix code displayed by LXR

 
 

    


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 //#include <trackbase/TrkrHitSetv1.h>
0019 #include <mvtx/MvtxDefs.h>
0020 #include <mvtx/MvtxHit.h>
0021 //#include <mvtx/MvtxHitSetv1.h>
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   //order
0068   //E103
0069   //C105
0070   //C104
0071   //A105
0072 }; //<ruid, ruch> to <stave, chipID>
0073 
0074 /**
0075  * Layers order in geom
0076  * A105 -> layer 0, stave 0
0077  * C104 -> layer 1, stave 0
0078  * C105 -> layer 2, stave 0
0079  * E103 -> layer 3, stave 0
0080  * <{stave_from_map_chips, layers_in_geom}
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     /*PHCompositeNode **/ dstNode(NULL),
0088     /*Event**/_event(NULL),
0089     /*Packet_hbd_fpgashort**/_packet(NULL),
0090     /*int*/_nevents(0),
0091     /*bool*/_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   else{
0157 
0158     MvtxDefUtil *mvtxdef = new MvtxDefUtil();
0159 
0160     for (int ichip=0; ichip<NCHIP; ichip++){
0161       MvtxHitSetv1 *hitset = new MvtxHitSetv1();
0162       hitset->SetHitSetKey(mvtxdef->GenHitSetKey(char(ichip),uint8_t(0),uint8_t(ichip)));
0163       _hitsetcon->AddHitSetSpecifyKey(hitset->GetHitSetKey(),hitset);
0164       if ( _verbosity>10 )
0165       {
0166         cout << "-----MvtxPrototype2UnpackPRDF::CreateNodeTree, Create hitset " << hitset->GetHitSetKey() << endl;
0167       }
0168     }
0169 
0170     if ( _verbosity>10 )
0171     {
0172       cout << "-----MvtxPrototype2UnpackPRDF::process_event Create HitSet" << endl;
0173       _hitsetcon->identify();
0174     }
0175     delete mvtxdef;
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     //_hitsetcon->identify();
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   //DST node
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   //DATA nodes
0245   /*
0246   data_node = static_cast<PHCompositeNode *>(nodeItr.findFirst("PHCompositeNode",
0247                                                                "RAW_DATA"));
0248   if (!data_node)
0249   {
0250     cout << "PHComposite node created: RAW_DATA" << endl;
0251     data_node = new PHCompositeNode("RAW_DATA");
0252     dst_node->addNode(data_node);
0253   }
0254   */
0255 
0256 }
0257 
0258 //_______________________________________
0259 void
0260 MvtxPrototype2UnpackPRDF::MakeHits()
0261 {
0262 
0263   //int nhits_for_this = 0;
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       if ( p->iValue(iru)==-1 )
0275       {
0276         cout << "No such RU: " << iru << endl;
0277         continue;
0278       }
0279       if ( p->iValue(iru, "BAD_RUIDS") )
0280       {
0281         cout << "Bad RU: " << iru << endl;
0282         continue;
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               //int chip_id = p->iValue(iru, ich, "CHIP_ID");
0293               //skip masked or dead chip
0294               if ( chip_id==-1 )
0295               {
0296                 continue;
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                 //skip unused channel
0311                 if ( stave_id_from_map==-1 || chip_id_from_map==-1 )
0312                 {
0313                   continue;
0314                 }
0315   /*
0316                 //skip inconsistent chip id
0317                 if ( chip_id!=chip_id_from_map )
0318                 {
0319                   cout << "WARNING!! Event: " << _nevents << " Inconsistent chip ID"
0320                   << " from packet: " << chip_id
0321                   << " from mapping: " << chip_id_from_map << endl;
0322                 }
0323     
0324                 if ( Verbosity()>100 )
0325                   cout << "iru: " << iru << " ich: " << ich << " chip_id from packet: " \
0326                        << chip_id << " chip_id from map: " << chip_id_from_map << endl;
0327     
0328                 int excess_data_bytes = p->iValue(iru, ich, "EXCESS_DATA_BYTES");
0329                 if ( excess_data_bytes>0 )
0330                 {
0331                   cout << "WARNING!! Event: " << _nevents << " Data found past chip trailer" \
0332                        << " EXCESS_DATA_BYTES: " << excess_data_bytes << endl;
0333                 }
0334   
0335                 int bad_bytes = p->iValue(iru, ich, "BAD_BYTES");
0336                 if ( bad_bytes>0 )
0337                 {
0338                   cout << "WARNING!! Event: " << _nevents << " Bad data found" \
0339                        << " BAD_BYTES: " << bad_bytes << endl;
0340                 }
0341     
0342                 int header_found = p->iValue(iru, ich, "HEADER_FOUND");
0343                 int trailer_found = p->iValue(iru, ich, "TRAILER_FOUND");
0344                 int bunchcounter = p->iValue(iru, ich, "BUNCHCOUNTER");
0345     
0346                 if ( header_found==0 || trailer_found==0 )
0347                 {
0348                   if ( Verbosity()>50 )
0349                   {
0350                     cout << "WARNING!! Event: " << _nevents << " Missing RU "          \
0351                          << iru << " CH: " << ich << " HEADER_FOUND: " << header_found \
0352                          << " TRAILER_FOUND: " << trailer_found << " BUNCHCOUNTER: "   \
0353                          << bunchcounter << endl;
0354                   }
0355                   //continue;
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                   // generate the key for this hit
0386                   TrkrDefs::hitkey hitkey = \
0387                     MvtxDefs::genHitKey(uint16_t(col_hit),uint16_t(row_hit));
0388                   // See if this hit already exists
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                 }//ihit
0407 
0408                 _nevent_per_chip[stave_id_from_map][chip_id_from_map]++;
0409               } //if good ru,ch
0410             } //if ru,ch have data
0411           }//ich
0412         }//if iru has data
0413       }//iru
0414     }// !err
0415 
0416     delete p;
0417   }//p
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