File indexing completed on 2022-02-25 23:40:15
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 = 0;
0025
0026 unpackSetup(pset.getUntrackedParameter<std::vector<std::string> >("streams", std::vector<std::string>()));
0027 produces<FEDRawDataCollection>();
0028 }
0029
0030 void HcalTBSource::unpackSetup(const std::vector<std::string>& params) {
0031 for (std::vector<std::string>::const_iterator i = params.begin(); i != params.end(); i++) {
0032 unsigned long pos = i->find(':');
0033 std::string streamName = i->substr(0, pos);
0034 int remapTo = -1;
0035 if (pos != std::string::npos)
0036 remapTo = atoi(i->c_str() + pos + 1);
0037
0038 m_sourceIdRemap.insert(std::pair<std::string, int>(streamName, remapTo));
0039 if (remapTo != -1)
0040 edm::LogInfo("HCAL") << streamName << " --> " << remapTo << endl;
0041 else
0042 edm::LogInfo("HCAL") << streamName << " using fedid in file" << endl;
0043 }
0044 }
0045
0046 HcalTBSource::~HcalTBSource() {
0047 if (m_file != nullptr) {
0048 m_file->Close();
0049 m_file = nullptr;
0050 m_tree = nullptr;
0051 }
0052 }
0053
0054 void HcalTBSource::openFile(const std::string& filename) {
0055 if (m_file != nullptr) {
0056 m_file->Close();
0057 m_file = nullptr;
0058 m_tree = nullptr;
0059 }
0060
0061
0062 m_file = TFile::Open(filename.c_str());
0063 if (m_file == nullptr) {
0064 edm::LogError("HCAL") << "Unable to open " << filename << endl;
0065 m_tree = nullptr;
0066 return;
0067 }
0068
0069 m_tree = (TTree*)m_file->Get("CMSRAW");
0070
0071 if (m_tree == nullptr) {
0072 m_file->Close();
0073 m_file = nullptr;
0074 edm::LogError("HCAL") << "Unable to find CMSRAW tree" << endl;
0075 return;
0076 }
0077
0078 if (!m_quiet) {
0079 edm::LogInfo("HCAL") << "Opening '" << filename << "' with " << m_tree->GetEntries() << " events.\n";
0080 }
0081
0082 TObjArray* lb = m_tree->GetListOfBranches();
0083 n_chunks = 0;
0084 for (int i = 0; i < lb->GetSize(); i++) {
0085 TBranch* b = (TBranch*)lb->At(i);
0086 if (b == nullptr)
0087 continue;
0088 if (!strcmp(b->GetClassName(), "CDFEventInfo")) {
0089 m_eventInfo = nullptr;
0090 b->SetAddress(&m_eventInfo);
0091 } else {
0092 if (strcmp(b->GetClassName(), "CDFChunk"))
0093 continue;
0094 if (m_sourceIdRemap.find(b->GetName()) == m_sourceIdRemap.end()) {
0095 if (m_onlyRemapped)
0096 continue;
0097 m_sourceIdRemap.insert(std::pair<std::string, int>(b->GetName(), -1));
0098 if (!m_quiet)
0099 edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
0100 }
0101
0102 m_chunks[n_chunks] = nullptr;
0103 b->SetAddress(&(m_chunks[n_chunks]));
0104 m_chunkIds[n_chunks] = m_sourceIdRemap[b->GetName()];
0105 n_chunks++;
0106 }
0107 }
0108 m_i = 0;
0109 }
0110
0111 bool HcalTBSource::setRunAndEventInfo(EventID& id, TimeValue_t& time, edm::EventAuxiliary::ExperimentType&) {
0112 bool is_new = false;
0113
0114 while (m_tree == nullptr || m_i == m_tree->GetEntries()) {
0115 m_fileCounter++;
0116 if (m_file != nullptr) {
0117 m_file->Close();
0118 m_file = nullptr;
0119 m_tree = nullptr;
0120 }
0121 if (m_fileCounter >= int(fileNames(0).size()))
0122 return false;
0123 openFile(fileNames(0)[m_fileCounter]);
0124 is_new = true;
0125 }
0126
0127 if (m_tree == nullptr || m_i == m_tree->GetEntries())
0128 return false;
0129
0130 m_tree->GetEntry(m_i);
0131 m_i++;
0132
0133 if (m_eventInfo != nullptr) {
0134 if (is_new) {
0135 if (m_eventInfo->getEventNumber() == 0)
0136 m_eventNumberOffset = 1;
0137 else
0138 m_eventNumberOffset = 0;
0139 }
0140
0141 id = EventID((m_eventInfo->getRunNumber() == 0 ? 1 : m_eventInfo->getRunNumber()),
0142 id.luminosityBlock(),
0143 m_eventInfo->getEventNumber() + m_eventNumberOffset + m_skip);
0144 } else {
0145 id = EventID(m_fileCounter + 10, id.luminosityBlock(), m_i + 1);
0146 }
0147
0148 edm::TimeValue_t present_time = presentTime();
0149 unsigned long time_between_events = timeBetweenEvents();
0150
0151 time = present_time + time_between_events;
0152 return true;
0153 }
0154
0155 void HcalTBSource::produce(edm::Event& e) {
0156 auto bare_product = std::make_unique<FEDRawDataCollection>();
0157 for (int i = 0; i < n_chunks; i++) {
0158 const unsigned char* data = (const unsigned char*)m_chunks[i]->getData();
0159 int len = m_chunks[i]->getDataLength() * 8;
0160
0161 int natId = m_chunks[i]->getSourceId();
0162 int id = (m_chunkIds[i] > 0) ? (m_chunkIds[i]) : (natId);
0163
0164 FEDRawData& fed = bare_product->FEDData(id);
0165 fed.resize(len);
0166 memcpy(fed.data(), data, len);
0167
0168
0169 if (natId != id) {
0170 unsigned int* header = (unsigned int*)fed.data();
0171 header[0] = (header[0] & 0xFFF000FFu) | (id << 8);
0172
0173 }
0174 if (!m_quiet)
0175 edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
0176 }
0177
0178 e.put(std::move(bare_product));
0179 }
0180
0181 #include "FWCore/Framework/interface/InputSourceMacros.h"
0182
0183 DEFINE_FWK_INPUT_SOURCE(HcalTBSource);