CMS 3D CMS Logo

HcalTBSource.cc
Go to the documentation of this file.
1 #include "TFile.h"
2 #include "TTree.h"
8 #include <iostream>
9 
12 
13 using namespace edm;
14 using namespace std;
15 
18  m_quiet(pset.getUntrackedParameter<bool>("quiet", true)),
19  m_onlyRemapped(pset.getUntrackedParameter<bool>("onlyRemapped", false)),
20  m_skip(pset.getUntrackedParameter<uint32_t>("skipEvents", 0)) {
21  m_tree = nullptr;
22  m_fileCounter = -1;
23  m_file = nullptr;
24  m_i = m_skip;
25 
26  if (m_skip != 0)
27  edm::LogWarning("HcalTBSource") << "skipEvents != 0 works only for the first input file";
28 
29  unpackSetup(pset.getUntrackedParameter<std::vector<std::string> >("streams", std::vector<std::string>()));
30  produces<FEDRawDataCollection>();
31 }
32 
33 void HcalTBSource::unpackSetup(const std::vector<std::string>& params) {
34  for (std::vector<std::string>::const_iterator i = params.begin(); i != params.end(); i++) {
35  unsigned long pos = i->find(':');
36  std::string streamName = i->substr(0, pos);
37  int remapTo = -1;
38  if (pos != std::string::npos)
39  remapTo = atoi(i->c_str() + pos + 1);
40 
41  m_sourceIdRemap.insert(std::pair<std::string, int>(streamName, remapTo));
42  if (remapTo != -1)
43  edm::LogInfo("HCAL") << streamName << " --> " << remapTo << endl;
44  else
45  edm::LogInfo("HCAL") << streamName << " using fedid in file" << endl;
46  }
47 }
48 
50  if (m_file != nullptr) {
51  m_file->Close();
52  m_file = nullptr;
53  m_tree = nullptr;
54  }
55 }
56 
58  if (m_file != nullptr) {
59  m_file->Close();
60  m_file = nullptr;
61  m_tree = nullptr;
62  }
63 
64  // try {
65  m_file = TFile::Open(filename.c_str());
66  if (m_file == nullptr) {
67  edm::LogError("HCAL") << "Unable to open " << filename << endl;
68  m_tree = nullptr;
69  return;
70  }
71 
72  m_tree = (TTree*)m_file->Get("CMSRAW");
73 
74  if (m_tree == nullptr) {
75  m_file->Close();
76  m_file = nullptr;
77  edm::LogError("HCAL") << "Unable to find CMSRAW tree" << endl;
78  return;
79  }
80 
81  if (!m_quiet) {
82  edm::LogInfo("HCAL") << "Opening '" << filename << "' with " << m_tree->GetEntries() << " events.\n";
83  }
84 
85  TObjArray* lb = m_tree->GetListOfBranches();
86  n_chunks = 0;
87  for (int i = 0; i < lb->GetSize(); i++) {
88  TBranch* b = (TBranch*)lb->At(i);
89  if (b == nullptr)
90  continue;
91  if (!strcmp(b->GetClassName(), "CDFEventInfo")) {
92  m_eventInfo = nullptr;
93  b->SetAddress(&m_eventInfo);
94  } else {
95  if (strcmp(b->GetClassName(), "CDFChunk"))
96  continue;
97  if (m_sourceIdRemap.find(b->GetName()) == m_sourceIdRemap.end()) {
98  if (m_onlyRemapped)
99  continue;
100  m_sourceIdRemap.insert(std::pair<std::string, int>(b->GetName(), -1));
101  if (!m_quiet)
102  edm::LogInfo("HCAL") << "Also reading branch " << b->GetName();
103  }
104 
105  m_chunks[n_chunks] = nullptr; // allow ROOT to allocate
106  b->SetAddress(&(m_chunks[n_chunks]));
107  m_chunkIds[n_chunks] = m_sourceIdRemap[b->GetName()];
108  n_chunks++;
109  }
110  }
111  if (!((m_skip != 0) & (m_i == m_skip)))
112  m_i = 0;
113 }
114 
116  bool is_new = false;
117 
118  while (m_tree == nullptr || m_i == m_tree->GetEntries()) {
119  m_fileCounter++;
120  if (m_file != nullptr) {
121  m_file->Close();
122  m_file = nullptr;
123  m_tree = nullptr;
124  }
125  if (m_fileCounter >= int(fileNames(0).size()))
126  return false; // nothing good
128  is_new = true;
129  }
130 
131  if (m_tree == nullptr || m_i == m_tree->GetEntries())
132  return false; //nothing good
133 
134  m_tree->GetEntry(m_i);
135  m_i++;
136 
137  if (m_eventInfo != nullptr) {
138  if (is_new) {
139  if (m_eventInfo->getEventNumber() == 0)
141  else
143  }
144  // ZERO is unacceptable for a run number from a technical point of view
145  id = EventID((m_eventInfo->getRunNumber() == 0 ? 1 : m_eventInfo->getRunNumber()),
146  id.luminosityBlock(),
148  } else {
149  id = EventID(m_fileCounter + 10, id.luminosityBlock(), m_i + 1);
150  }
151  // time is a hack
152  edm::TimeValue_t present_time = presentTime();
153  unsigned long time_between_events = timeBetweenEvents();
154 
155  time = present_time + time_between_events;
156  return true;
157 }
158 
160  auto bare_product = std::make_unique<FEDRawDataCollection>();
161  for (int i = 0; i < n_chunks; i++) {
162  const unsigned char* data = (const unsigned char*)m_chunks[i]->getData();
163  int len = m_chunks[i]->getDataLength() * 8;
164 
165  int natId = m_chunks[i]->getSourceId();
166  int id = (m_chunkIds[i] > 0) ? (m_chunkIds[i]) : (natId);
167 
168  FEDRawData& fed = bare_product->FEDData(id);
169  fed.resize(len);
170  memcpy(fed.data(), data, len);
171 
172  // patch the SourceId...
173  if (natId != id) {
174  unsigned int* header = (unsigned int*)fed.data();
175  header[0] = (header[0] & 0xFFF000FFu) | (id << 8);
176  // TODO: patch CRC after this change!
177  }
178  if (!m_quiet)
179  edm::LogInfo("HCAL") << "Reading " << len << " bytes for FED " << id << std::endl;
180  }
181 
182  e.put(std::move(bare_product));
183 }
184 
186 
size
Write out results.
TFile * m_file
Definition: HcalTBSource.h:32
UInt_t getRunNumber() const
get the run number
Definition: CDFEventInfo.h:12
int m_eventNumberOffset
Definition: HcalTBSource.h:41
ULong64_t getEventNumber() const
get the event number
Definition: CDFEventInfo.h:16
Log< level::Error, false > LogError
TTree * m_tree
Definition: HcalTBSource.h:31
void openFile(const std::string &filename)
Definition: HcalTBSource.cc:57
#define DEFINE_FWK_INPUT_SOURCE(type)
void resize(size_t newsize)
Definition: FEDRawData.cc:28
int getSourceId() const
Definition: CDFChunk.h:24
Int_t getDataLength() const
Definition: CDFChunk.h:23
Global information about an event such as event number and run number.
Definition: CDFEventInfo.h:8
unsigned long long TimeValue_t
Definition: Timestamp.h:21
bool m_onlyRemapped
Definition: HcalTBSource.h:34
CDFChunk * m_chunks[CHUNK_COUNT]
Definition: HcalTBSource.h:37
Log< level::Info, false > LogInfo
int m_chunkIds[CHUNK_COUNT]
Definition: HcalTBSource.h:38
HcalTBSource(const edm::ParameterSet &pset, edm::InputSourceDescription const &desc)
Definition: HcalTBSource.cc:16
void unpackSetup(const std::vector< std::string > &params)
Definition: HcalTBSource.cc:33
ClassImp(CDFChunk)
double b
Definition: hdecay.h:120
HLT enums.
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:80
std::vector< std::string > fileNames(unsigned iCatalog) const
Definition: FromFiles.h:22
std::map< std::string, int > m_sourceIdRemap
Definition: HcalTBSource.h:39
const unsigned char * data() const
Return a const pointer to the beginning of the data buffer.
Definition: FEDRawData.cc:24
CDFEventInfo * m_eventInfo
Definition: HcalTBSource.h:40
bool setRunAndEventInfo(edm::EventID &id, edm::TimeValue_t &time, edm::EventAuxiliary::ExperimentType &) override
~HcalTBSource() override
Definition: HcalTBSource.cc:49
Log< level::Warning, false > LogWarning
def move(src, dest)
Definition: eostools.py:511
void produce(edm::Event &e) override