File indexing completed on 2025-08-06 08:17:40
0001 #include "InttCombinedRawDataDecoder.h"
0002
0003 #include <trackbase/InttDefs.h>
0004 #include <trackbase/InttEventInfov1.h>
0005 #include <trackbase/TrkrDefs.h> // for hitkey, hitsetkey
0006 #include <trackbase/TrkrHit.h>
0007 #include <trackbase/TrkrHitSet.h>
0008 #include <trackbase/TrkrHitSetContainer.h>
0009 #include <trackbase/TrkrHitSetContainerv1.h>
0010 #include <trackbase/TrkrHitv2.h>
0011
0012 #include <ffarawobjects/Gl1RawHit.h>
0013 #include <ffarawobjects/Gl1Packet.h>
0014 #include <ffarawobjects/InttRawHit.h>
0015 #include <ffarawobjects/InttRawHitContainer.h>
0016
0017 #include <fun4all/Fun4AllReturnCodes.h>
0018 #include <fun4all/Fun4AllServer.h>
0019 #include <phool/PHCompositeNode.h>
0020 #include <phool/PHIODataNode.h> // for PHIODataNode
0021 #include <phool/PHNodeIterator.h>
0022 #include <phool/getClass.h>
0023 #include <phool/phool.h> // for PHWHERE
0024
0025 #include <TSystem.h>
0026
0027 #include <cstdlib> // for exit
0028 #include <filesystem> // for filesystem::exist
0029 #include <iostream> // for operator<<, endl, bas...
0030 #include <map> // for _Rb_tree_iterator
0031
0032 InttCombinedRawDataDecoder::InttCombinedRawDataDecoder(std::string const& name)
0033 : SubsysReco(name)
0034 , m_calibinfoDAC({"INTT_DACMAP", CDB})
0035 , m_calibinfoBCO({"INTT_BCOMAP", CDB})
0036 {
0037
0038
0039 }
0040
0041 int InttCombinedRawDataDecoder::InitRun(PHCompositeNode* topNode)
0042 {
0043 if (!topNode)
0044 {
0045 std::cout
0046 << PHWHERE "\n"
0047 << "\tCould not retrieve topNode; doing nothing\n"
0048 << std::flush;
0049 exit(1);
0050 gSystem->Exit(1);
0051
0052 return 1;
0053 }
0054
0055 PHNodeIterator dst_itr(topNode);
0056 PHCompositeNode* dst_node = dynamic_cast<PHCompositeNode*>(dst_itr.findFirst("PHCompositeNode", "DST"));
0057 if (!dst_node)
0058 {
0059 std::cout
0060 << PHWHERE << "\n"
0061 << "\tCould not retrieve dst_node; doing nothing\n"
0062 << std::flush;
0063 exit(1);
0064 gSystem->Exit(1);
0065
0066 return 1;
0067 }
0068
0069 PHNodeIterator trkr_itr(dst_node);
0070 PHCompositeNode* trkr_node = dynamic_cast<PHCompositeNode*>(trkr_itr.findFirst("PHCompositeNode", "TRKR"));
0071 if (!trkr_node)
0072 {
0073 trkr_node = new PHCompositeNode("TRKR");
0074 dst_node->addNode(trkr_node);
0075 }
0076
0077 TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
0078 if (!trkr_hit_set_container)
0079 {
0080 if (Verbosity())
0081 {
0082 std::cout
0083 << PHWHERE << "\n"
0084 << "\tMaking TrkrHitSetContainer\n"
0085 << std::flush;
0086 }
0087
0088 trkr_hit_set_container = new TrkrHitSetContainerv1;
0089 PHIODataNode<PHObject>* new_node = new PHIODataNode<PHObject>(trkr_hit_set_container, "TRKR_HITSET", "PHObject");
0090 trkr_node->addNode(new_node);
0091 }
0092
0093
0094 if (m_writeInttEventHeader)
0095 {
0096 auto inttNode = dynamic_cast<PHCompositeNode*>(trkr_itr.findFirst("PHCompositeNode", "INTT"));
0097 if (!inttNode)
0098 {
0099 inttNode = new PHCompositeNode("INTT");
0100 dst_node->addNode(inttNode);
0101 }
0102
0103 intt_event_header = findNode::getClass<InttEventInfo>(inttNode, "INTTEVENTHEADER");
0104 if (!intt_event_header)
0105 {
0106 intt_event_header = new InttEventInfov1();
0107 auto newHeader = new PHIODataNode<PHObject>(intt_event_header, "INTTEVENTHEADER", "PHObject");
0108 inttNode->addNode(newHeader);
0109 }
0110 }
0111
0112 InttRawHitContainer* inttcont = findNode::getClass<InttRawHitContainer>(topNode, m_InttRawNodeName);
0113 if (!inttcont)
0114 {
0115 std::cout
0116 << PHWHERE << "\n"
0117 << "Could not get \"" << m_InttRawNodeName << "\" from Node Tree\n"
0118 << "removing module\n"
0119 << std::flush;
0120
0121 Fun4AllServer* se = Fun4AllServer::instance();
0122 se->unregisterSubsystem(this);
0123 return Fun4AllReturnCodes::EVENT_OK;
0124 }
0125
0126
0127 std::cout << "calibinfo DAC : " << m_calibinfoDAC.first << " " << (m_calibinfoDAC.second == CDB ? "CDB" : "FILE") << std::endl;
0128 m_dacmap.Verbosity(Verbosity());
0129 if (m_calibinfoDAC.second == CDB)
0130 {
0131 m_dacmap.LoadFromCDB(m_calibinfoDAC.first);
0132 }
0133 else
0134 {
0135 m_dacmap.LoadFromFile(m_calibinfoDAC.first);
0136 }
0137
0138
0139 std::cout << "calibinfo BCO : " << m_calibinfoBCO.first << " " << (m_calibinfoBCO.second == CDB ? "CDB" : "FILE") << std::endl;
0140 m_bcomap.Verbosity(Verbosity());
0141 int temp_offset = 0;
0142 if (m_calibinfoBCO.second == CDB)
0143 {
0144 temp_offset = m_bcomap.LoadFromCDB(m_calibinfoBCO.first);
0145 }
0146 else
0147 {
0148 temp_offset = m_bcomap.LoadFromFile(m_calibinfoBCO.first);
0149 }
0150 if(m_triggeredMode)
0151 {
0152 set_inttFeeOffset(temp_offset);
0153 }
0154
0155
0156 if (!m_badmap.OfflineLoaded() && !m_badmap.RawDataLoaded())
0157 {
0158 m_badmap.Load();
0159 }
0160 if (Verbosity())
0161 {
0162 std::cout << "InttBadChannelMap size: " << m_badmap.size() << std::endl;
0163 }
0164 if (1 < Verbosity())
0165 {
0166 m_badmap.Print();
0167 }
0168
0169 return Fun4AllReturnCodes::EVENT_OK;
0170 }
0171
0172 int InttCombinedRawDataDecoder::process_event(PHCompositeNode* topNode)
0173 {
0174 TrkrHitSetContainer* trkr_hit_set_container = findNode::getClass<TrkrHitSetContainer>(topNode, "TRKR_HITSET");
0175 if (!trkr_hit_set_container)
0176 {
0177 std::cout
0178 << PHWHERE << "\n"
0179 << "\tCould not get \"TRKR_HITSET\" from Node Tree\n"
0180 << "\tExiting\n"
0181 << std::flush;
0182 gSystem->Exit(1);
0183 exit(1);
0184
0185 return Fun4AllReturnCodes::DISCARDEVENT;
0186 }
0187
0188 InttRawHitContainer* inttcont = findNode::getClass<InttRawHitContainer>(topNode, m_InttRawNodeName);
0189 if (!inttcont)
0190 {
0191 std::cout << PHWHERE << std::endl;
0192 std::cout << "InttCombinedRawDataDecoder::process_event(PHCompositeNode* topNode)" << std::endl;
0193 std::cout << "Could not get \"" << m_InttRawNodeName << "\" from Node Tree" << std::endl;
0194 std::cout << "Exiting" << std::endl;
0195
0196 gSystem->Exit(1);
0197 exit(1);
0198 }
0199
0200 Gl1Packet* gl1 = nullptr;
0201 uint64_t gl1rawhitbco = 0;
0202 if (!m_runStandAlone)
0203 {
0204 gl1 = findNode::getClass<Gl1Packet>(topNode, "GL1RAWHIT");
0205 if (gl1)
0206 {
0207 gl1rawhitbco = gl1->lValue(0, "BCO");
0208 }
0209 else
0210 {
0211 auto oldgl1 = findNode::getClass<Gl1RawHit>(topNode, "GL1RAWHIT");
0212 if(!oldgl1)
0213 {
0214 std::cout << PHWHERE << " no gl1 container, exiting" << std::endl;
0215 return Fun4AllReturnCodes::ABORTEVENT;
0216 }
0217 gl1rawhitbco = oldgl1->get_bco();
0218 }
0219 }
0220
0221
0222
0223 auto lbshift = gl1rawhitbco << 24U;
0224 auto gl1bco = lbshift >> 24U;
0225
0226 if (m_writeInttEventHeader)
0227 {
0228 intt_event_header = findNode::getClass<InttEventInfo>(topNode, "INTTEVENTHEADER");
0229 assert(intt_event_header);
0230 if (inttcont->get_nhits() > 0)
0231 {
0232 intt_event_header->set_bco_full(inttcont->get_hit(0)->get_bco());
0233 }
0234 else
0235 {
0236 intt_event_header->set_bco_full(0);
0237 }
0238 }
0239
0240 TrkrDefs::hitsetkey hit_set_key = 0;
0241 TrkrDefs::hitkey hit_key = 0;
0242 TrkrHitSetContainer::Iterator hit_set_container_itr;
0243 TrkrHit* hit = nullptr;
0244
0245 for (unsigned int i = 0; i < inttcont->get_nhits(); i++)
0246 {
0247 InttRawHit* intthit = inttcont->get_hit(i);
0248 InttNameSpace::RawData_s raw = InttNameSpace::RawFromHit(intthit);
0249
0250 int adc = intthit->get_adc();
0251
0252 uint64_t bco_full = intthit->get_bco();
0253 int bco = intthit->get_FPHX_BCO();
0254
0255 InttNameSpace::Offline_s ofl = InttNameSpace::ToOffline(raw);
0256
0257
0258
0259 if (m_badmap.OfflineLoaded() && m_badmap.IsBad(ofl))
0260 {
0261 if (1 < Verbosity())
0262 {
0263 std::cout
0264 << PHWHERE << "\n"
0265 << "\tMasking channel:\n"
0266 << "\t" << ofl.layer << " " << ofl.ladder_phi << " " << ofl.ladder_z << " " << ofl.strip_y << " " << ofl.strip_x << "\n"
0267 << std::endl;
0268 }
0269 continue;
0270 }
0271
0272 if (m_badmap.RawDataLoaded() && m_badmap.IsBad(raw))
0273 {
0274 if (1 < Verbosity())
0275 {
0276 std::cout
0277 << PHWHERE << "\n"
0278 << "\tMasking (raw) channel:\n"
0279 << "\t" << raw.felix_server << " " << raw.felix_channel << " " << raw.chip << " " << raw.channel << "\n"
0280 << std::endl;
0281 }
0282 continue;
0283 }
0284
0285
0286
0287 if (m_bcomap.IsBad(raw, bco_full, bco) && m_bcoFilter)
0288 {
0289
0290 continue;
0291 }
0292
0293 hit_key = InttDefs::genHitKey(ofl.strip_y, ofl.strip_x);
0294 int time_bucket = 0;
0295 if(!m_runStandAlone)
0296 {
0297 if(m_triggeredMode)
0298 {
0299 time_bucket = (intthit->get_FPHX_BCO() - (intthit->get_bco() & 0x7fU) - m_inttFeeOffset + 128) % 128;
0300 }
0301 else
0302 {
0303
0304
0305
0306
0307
0308
0309 time_bucket = intthit->get_FPHX_BCO() + intthit->get_bco() - gl1bco - m_inttFeeOffset;
0310 }
0311 }
0312 hit_set_key = InttDefs::genHitSetKey(ofl.layer, ofl.ladder_z, ofl.ladder_phi, time_bucket);
0313 hit_set_container_itr = trkr_hit_set_container->findOrAddHitSet(hit_set_key);
0314 hit = hit_set_container_itr->second->getHit(hit_key);
0315
0316 if(m_outputBcoDiff)
0317 {
0318 int bco_diff = 0;
0319 if(m_triggeredMode)
0320 {
0321 bco_diff = (intthit->get_FPHX_BCO() - (intthit->get_bco() & 0x7fU) + 128) % 128;
0322 }
0323 else
0324 {
0325 bco_diff = intthit->get_FPHX_BCO() + intthit->get_bco() - gl1bco;
0326 }
0327
0328 std::cout << " bco: " << " fee " << intthit->get_fee()
0329 << " rawhitbco " << intthit->get_bco()
0330 << " gl1bco " << gl1bco
0331 << " intthit->get_FPHX_BCO() " << intthit->get_FPHX_BCO()
0332 << " bcodiff " << bco_diff
0333 << " time_bucket " << time_bucket
0334 << std::endl;
0335 }
0336
0337 if (hit)
0338 {
0339 continue;
0340 }
0341
0342
0343
0344 int dac = m_dacmap.GetDAC(raw, adc);
0345
0346 hit = new TrkrHitv2;
0347
0348 hit->setAdc(dac);
0349 hit_set_container_itr->second->addHitSpecificKey(hit_key, hit);
0350 }
0351
0352 return Fun4AllReturnCodes::EVENT_OK;
0353 }
0354