Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:10:32

0001 #include "TFile.h"
0002 #include "TTree.h"
0003 #include "IORawData/HcalTBInputService/plugins/HcalTBSource.h"
0004 #include "IORawData/HcalTBInputService/src/CDFChunk.h"
0005 #include "IORawData/HcalTBInputService/src/CDFEventInfo.h"
0006 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include <iostream>
0009 
0010 ClassImp(CDFChunk);
0011 ClassImp(CDFEventInfo);
0012 
0013 using namespace edm;
0014 using namespace std;
0015 
0016 HcalTBSource::HcalTBSource(const edm::ParameterSet& pset, edm::InputSourceDescription const& desc)
0017     : edm::ProducerSourceFromFiles(pset, desc, true),
0018       m_quiet(pset.getUntrackedParameter<bool>("quiet", true)),
0019       m_onlyRemapped(pset.getUntrackedParameter<bool>("onlyRemapped", false)),
0020       m_skip(pset.getUntrackedParameter<uint32_t>("skipEvents", 0)) {
0021   m_tree = nullptr;
0022   m_fileCounter = -1;
0023   m_file = nullptr;
0024   m_i = m_skip;
0025 
0026   if (m_skip != 0)
0027     edm::LogWarning("HcalTBSource") << "skipEvents != 0 works only for the first input file";
0028 
0029   unpackSetup(pset.getUntrackedParameter<std::vector<std::string> >("streams", std::vector<std::string>()));
0030   produces<FEDRawDataCollection>();
0031 }
0032 
0033 void HcalTBSource::unpackSetup(const std::vector<std::string>& params) {
0034   for (std::vector<std::string>::const_iterator i = params.begin(); i != params.end(); i++) {
0035     unsigned long pos = i->find(':');
0036     std::string streamName = i->substr(0, pos);
0037     int remapTo = -1;
0038     if (pos != std::string::npos)
0039       remapTo = atoi(i->c_str() + pos + 1);
0040 
0041     m_sourceIdRemap.insert(std::pair<std::string, int>(streamName, remapTo));
0042     if (remapTo != -1)
0043       edm::LogInfo("HCAL") << streamName << " --> " << remapTo << endl;
0044     else
0045       edm::LogInfo("HCAL") << streamName << " using fedid in file" << endl;
0046   }
0047 }
0048 
0049 HcalTBSource::~HcalTBSource() {
0050   if (m_file != nullptr) {
0051     m_file->Close();
0052     m_file = nullptr;
0053     m_tree = nullptr;
0054   }
0055 }
0056 
0057 void HcalTBSource::openFile(const std::string& filename) {
0058   if (m_file != nullptr) {
0059     m_file->Close();
0060     m_file = nullptr;
0061     m_tree = nullptr;
0062   }
0063 
0064   //  try {
0065   m_file = TFile::Open(filename.c_str());
0066   if (m_file == nullptr) {
0067     edm::LogError("HCAL") << "Unable to open " << filename << endl;
0068     m_tree = nullptr;
0069     return;
0070   }
0071 
0072   m_tree = (TTree*)m_file->Get("CMSRAW");
0073 
0074   if (m_tree == nullptr) {
0075     m_file->Close();
0076     m_file = nullptr;
0077     edm::LogError("HCAL") << "Unable to find CMSRAW tree" << endl;
0078     return;
0079   }
0080 
0081   if (!m_quiet) {
0082     edm::LogInfo("HCAL") << "Opening '" << filename << "' with " << m_tree->GetEntries() << " events.\n";
0083   }
0084 
0085   TObjArray* lb = m_tree->GetListOfBranches();
0086   n_chunks = 0;
0087   for (int i = 0; i < lb->GetSize(); i++) {
0088     TBranch* b = (TBranch*)lb->At(i);
0089     if (b == nullptr)
0090       continue;
0091     if (!strcmp(b->GetClassName(), "CDFEventInfo")) {
0092       m_eventInfo = nullptr;
0093       b->SetAddress(&m_eventInfo);
0094     } else {
0095       if (strcmp(b->GetClassName(), "CDFChunk"))
0096         continue;
0097       if (m_sourceIdRemap.find(b->GetName()) == m_sourceIdRemap.end()) {
0098         if (m_onlyRemapped)
0099           continue;
0100         m_sourceIdRemap.insert(std::pair<std::string, int>(b->GetName(), -1));
0101         if (!m_quiet)
0102           edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
0103       }
0104 
0105       m_chunks[n_chunks] = nullptr;  // allow ROOT to allocate
0106       b->SetAddress(&(m_chunks[n_chunks]));
0107       m_chunkIds[n_chunks] = m_sourceIdRemap[b->GetName()];
0108       n_chunks++;
0109     }
0110   }
0111   if (!((m_skip != 0) & (m_i == m_skip)))
0112     m_i = 0;
0113 }
0114 
0115 bool HcalTBSource::setRunAndEventInfo(EventID& id, TimeValue_t& time, edm::EventAuxiliary::ExperimentType&) {
0116   bool is_new = false;
0117 
0118   while (m_tree == nullptr || m_i == m_tree->GetEntries()) {
0119     m_fileCounter++;
0120     if (m_file != nullptr) {
0121       m_file->Close();
0122       m_file = nullptr;
0123       m_tree = nullptr;
0124     }
0125     if (m_fileCounter >= int(fileNames(0).size()))
0126       return false;  // nothing good
0127     openFile(fileNames(0)[m_fileCounter]);
0128     is_new = true;
0129   }
0130 
0131   if (m_tree == nullptr || m_i == m_tree->GetEntries())
0132     return false;  //nothing good
0133 
0134   m_tree->GetEntry(m_i);
0135   m_i++;
0136 
0137   if (m_eventInfo != nullptr) {
0138     if (is_new) {
0139       if (m_eventInfo->getEventNumber() == 0)
0140         m_eventNumberOffset = 1;
0141       else
0142         m_eventNumberOffset = 0;
0143     }
0144     // ZERO is unacceptable for a run number from a technical point of view
0145     id = EventID((m_eventInfo->getRunNumber() == 0 ? 1 : m_eventInfo->getRunNumber()),
0146                  id.luminosityBlock(),
0147                  m_eventInfo->getEventNumber() + m_eventNumberOffset);
0148   } else {
0149     id = EventID(m_fileCounter + 10, id.luminosityBlock(), m_i + 1);
0150   }
0151   // time is a hack
0152   edm::TimeValue_t present_time = presentTime();
0153   unsigned long time_between_events = timeBetweenEvents();
0154 
0155   time = present_time + time_between_events;
0156   return true;
0157 }
0158 
0159 void HcalTBSource::produce(edm::Event& e) {
0160   auto bare_product = std::make_unique<FEDRawDataCollection>();
0161   for (int i = 0; i < n_chunks; i++) {
0162     const unsigned char* data = (const unsigned char*)m_chunks[i]->getData();
0163     int len = m_chunks[i]->getDataLength() * 8;
0164 
0165     int natId = m_chunks[i]->getSourceId();
0166     int id = (m_chunkIds[i] > 0) ? (m_chunkIds[i]) : (natId);
0167 
0168     FEDRawData& fed = bare_product->FEDData(id);
0169     fed.resize(len);
0170     memcpy(fed.data(), data, len);
0171 
0172     // patch the SourceId...
0173     if (natId != id) {
0174       unsigned int* header = (unsigned int*)fed.data();
0175       header[0] = (header[0] & 0xFFF000FFu) | (id << 8);
0176       // TODO: patch CRC after this change!
0177     }
0178     if (!m_quiet)
0179       edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
0180   }
0181 
0182   e.put(std::move(bare_product));
0183 }
0184 
0185 #include "FWCore/Framework/interface/InputSourceMacros.h"
0186 
0187 DEFINE_FWK_INPUT_SOURCE(HcalTBSource);