CMS 3D CMS Logo

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