CMS 3D CMS Logo

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