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