Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:45

0001 /****************************************************************************
0002  *
0003  * This is a part of HGCAL offline software.
0004  * Authors:
0005  *   Pedro Silva, CERN
0006  *   Laurent Forthomme, CERN
0007  *
0008  ****************************************************************************/
0009 
0010 #include "DataFormats/HGCalDigi/interface/HGCROCChannelDataFrame.h"
0011 #include "EventFilter/HGCalRawToDigi/interface/HGCalModuleTreeReader.h"
0012 #include "EventFilter/HGCalRawToDigi/interface/HGCalRawDataDefinitions.h"
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 #include "TChain.h"
0017 
0018 using namespace hgcal::econd;
0019 
0020 HGCalModuleTreeReader::HGCalModuleTreeReader(const EmulatorParameters& params,
0021                                              const std::string& tree_name,
0022                                              const std::vector<std::string>& filenames)
0023     : Emulator(params) {
0024   TChain chain(tree_name.data());
0025   for (const auto& filename : filenames)
0026     chain.Add(filename.c_str());
0027 
0028   HGCModuleTreeEvent event;
0029   chain.SetBranchAddress("event", &event.event);
0030   chain.SetBranchAddress("chip", &event.chip);
0031   chain.SetBranchAddress("half", &event.half);
0032   chain.SetBranchAddress("daqdata", &event.daqdata);
0033   chain.SetBranchAddress("bxcounter", &event.bxcounter);
0034   chain.SetBranchAddress("eventcounter", &event.eventcounter);
0035   chain.SetBranchAddress("orbitcounter", &event.orbitcounter);
0036   chain.SetBranchAddress("trigtime", &event.trigtime);
0037   chain.SetBranchAddress("trigwidth", &event.trigwidth);
0038 
0039   for (long long i = 0; i < chain.GetEntries(); ++i) {
0040     chain.GetEntry(i);
0041 
0042     // check if event already exists
0043     EventId key{(uint32_t)event.eventcounter, (uint32_t)event.bxcounter, (uint32_t)event.orbitcounter};
0044     if (data_.count(key) == 0)
0045       data_[key] = ERxInput{};
0046 
0047     // check if chip already exists
0048     ERxId_t erxKey{(uint8_t)event.chip, (uint8_t)event.half};
0049     if (data_[key].count(erxKey) == 0)
0050       data_[key][erxKey] = ERxData{};
0051 
0052     // daqdata: header, CM, 37 ch, CRC32, idle
0053     if (const auto nwords = event.daqdata->size(); nwords != 41)
0054       throw cms::Exception("HGCalModuleTreeReader")
0055           << "Invalid number of words retrieved for event {" << event.eventcounter << ":" << event.bxcounter << ":"
0056           << event.orbitcounter << "}: should be 41, got " << nwords << ".";
0057 
0058     // 1st word is the header: discard
0059     // 2nd word are the common mode words
0060 
0061     const uint32_t cmword(event.daqdata->at(1));
0062     if (((cmword >> 20) & 0xfff) != 0)
0063       throw cms::Exception("HGCalModuleTreeReader")
0064           << "Consistency check failed for common mode word: " << ((cmword >> 20) & 0xfff) << " != 0.";
0065 
0066     data_[key][erxKey].cm1 = cmword & 0x3ff;
0067     data_[key][erxKey].cm0 = (cmword >> 10) & 0x3ff;
0068 
0069     // next 37 words are channel data
0070     for (size_t i = 2; i < 2 + params_.num_channels_per_erx; i++) {
0071       HGCROCChannelDataFrame<uint32_t> frame(0, event.daqdata->at(i));
0072       const auto tctp = static_cast<ToTStatus>(frame.tctp());
0073       data_[key][erxKey].tctp.push_back(tctp);
0074       data_[key][erxKey].adcm.push_back(frame.adcm1());
0075       data_[key][erxKey].adc.push_back(tctp == ToTStatus::ZeroSuppressed ? frame.adc() : 0);
0076       data_[key][erxKey].tot.push_back(tctp == ToTStatus::ZeroSuppressed ? frame.rawtot() : 0);
0077       data_[key][erxKey].toa.push_back(frame.toa());
0078     }
0079 
0080     // copy CRC32
0081     data_[key][erxKey].crc32 = event.daqdata->at(39);
0082 
0083     // we could assert the idle word from #40 if needed
0084 
0085     // copy metadata
0086     data_[key][erxKey].meta.push_back(event.trigtime);
0087     data_[key][erxKey].meta.push_back(event.trigwidth);
0088   }
0089 
0090   edm::LogInfo("HGCalModuleTreeReader") << "read " << data_.size() << " events.";
0091 
0092   it_data_ = data_.begin();
0093 }
0094 
0095 //
0096 ECONDInput HGCalModuleTreeReader::next() {
0097   if (it_data_ == data_.end())
0098     throw cms::Exception("HGCalModuleTreeReader") << "Insufficient number of events were retrieved from input tree to "
0099                                                      "proceed with the generation of emulated events.";
0100 
0101   ++it_data_;
0102   return ECONDInput{it_data_->first, it_data_->second};
0103 }