CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalLuttoDB.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HcalLuttoDB
4 // Class: HcalLuttoDB
5 //
13 //
14 // Original Author: Michael Weinberger
15 // Created: Mon Mar 19 11:53:56 CDT 2007
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 
23 // user include files
29 
31 
40 
45 
46 using namespace edm;
47 using namespace std;
48 #include <iostream>
49 #include <fstream>
50 #include "md5.h"
51 
52 //
53 // class decleration
54 //
55 
56 class HcalLuttoDB : public edm::EDAnalyzer {
57 public:
58  explicit HcalLuttoDB(const edm::ParameterSet&);
59  ~HcalLuttoDB();
60 
61  virtual void beginJob() override ;
62  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
63  virtual void endJob() override ;
64 
65 private:
66  void writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector<unsigned short>& lut, std::ostream& os);
67  std::vector<unsigned char> extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan);
68  void writeoutlut2(HcalTrigTowerDetId id, HcalElectronicsId eid, const std::vector<unsigned char>& lut, std::ostream& os);
72  std::ostream* openPerCrate(int crate);
73  std::ostream* openPerLut1(HcalElectronicsId eid);
74  std::ostream* openPerLut2(HcalElectronicsId eid);
75  std::ostream* openChecksums();
76  std::ostream* oc_;
77 };
78  // ----------member data ---------------------------
79 
82 static const int formatRevision_=1;
83 
84 // constructors and destructor
85 //
87 {
88 
89  creationtag_ = iConfig.getParameter<std::string>("creationtag");
90  targetfirmware_ = iConfig.getParameter<std::string>("targetfirmware");
91  filePerCrate_ = iConfig.getUntrackedParameter<bool>("filePerCrate",true);
92  fileformat_=iConfig.getParameter<std::string>("filePrefix");
93 }
94 
96 {
97 }
98 
99 
100 //
101 // member functions
102 //
103 
104 std::ostream* HcalLuttoDB::openChecksums() {
105  char fname[1024];
106  snprintf(fname,1024,"%s_checksums.xml",fileformat_.c_str());
107  std::ostream* os=new std::ofstream(fname);
108  (*os) << "<?xml version=\"1.0\"?>\n<CFGBrick>\n";
109  return os;
110 }
111 
112 std::ostream* HcalLuttoDB::openPerCrate(int crate) {
113  char fname[1024];
114  snprintf(fname,1024,"%s_%d.xml",fileformat_.c_str(),crate);
115  std::ostream* os=new std::ofstream(fname);
116  (*os) << "<?xml version=\"1.0\"?>\n<CFGBrickSet>\n";
117  return os;
118 }
119 
121  char fname[1024];
122  snprintf(fname,1024,"%s_%d_%d%c_%d_%d_1.xml",fileformat_.c_str(),eid.readoutVMECrateId(),
123  eid.htrSlot(),((eid.htrTopBottom())?('t'):('b')),eid.fiberIndex(),eid.fiberChanId());
124  std::ostream* os=new std::ofstream(fname);
125  (*os) << "<?xml version=\"1.0\"?>\n";
126  return os;
127 }
128 
130  char fname[1024];
131  snprintf(fname,1024,"%s_%d_%d%c_%d_%d_2.xml",fileformat_.c_str(),eid.readoutVMECrateId(),
132  eid.htrSlot(),((eid.htrTopBottom())?('t'):('b')),eid.slbSiteNumber(),eid.slbChannelIndex());
133  std::ostream* os=new std::ofstream(fname);
134  (*os) << "<?xml version=\"1.0\"?>\n";
135  return os;
136 }
137 
138 
139 void
140 HcalLuttoDB::writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector<unsigned short>& lut, std::ostream& os) {
141 
142  os <<"<CFGBrick> "<<std::endl;
143  os <<" <Parameter name='IETA' type='int'>"<<id.ieta()<<"</Parameter>"<<std::endl;
144  os <<" <Parameter name='IPHI' type='int'>"<<id.iphi()<<"</Parameter>"<<std::endl;
145  os <<" <Parameter name='DEPTH' type='int'>"<<id.depth()<<"</Parameter>"<<std::endl;
146  os <<" <Parameter name='CRATE' type='int'>"<<eid.readoutVMECrateId()<<"</Parameter>"<<std::endl;
147  os <<" <Parameter name='SLOT' type='int'>"<<eid.htrSlot()<<"</Parameter>"<<std::endl;
148  os <<" <Parameter name='TOPBOTTOM' type='int'>"<<eid.htrTopBottom()<<"</Parameter>"<<std::endl;
149  os <<" <Parameter name='FIBER' type='int'>"<<eid.fiberIndex()<<"</Parameter>"<<std::endl;
150  os <<" <Parameter name='FIBERCHAN' type='int'>"<<eid.fiberChanId()<<"</Parameter>"<<std::endl;
151  os <<" <Parameter name='LUT_TYPE' type='int'>1</Parameter>"<<std::endl;
152  os <<" <Parameter name='CREATIONTAG' type='string'>"<<creationtag_<<"</Parameter>"<<std::endl;
153  os <<" <Parameter name='CREATIONSTAMP' type='string'>"<<creationstamp_<<"</Parameter>"<<std::endl;
154  os <<" <Parameter name='FORMATREVISION' type='string'>"<<formatRevision_<<"</Parameter>"<<std::endl;
155  os <<" <Parameter name='TARGETFIRMWARE' type='string'>"<<targetfirmware_<<"</Parameter>"<<std::endl;
156  int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
157  ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
158 
159  os <<" <Parameter name='GENERALIZEDINDEX' type='int'>"<<generalizedIndex<<"</Parameter>"<<std::endl;
160  // do checksum
161  md5_state_t md5er;
162  md5_byte_t digest[16];
163  unsigned char tool[2];
164  md5_init(&md5er);
165  for (int i=0; i<128; i++) {
166  tool[0]=lut[i]&0xFF;
167  tool[1]=(lut[i]>>8)&0xFF;
168  md5_append(&md5er,tool,2);
169  }
170  md5_finish(&md5er,digest);
171  os <<" <Parameter name='CHECKSUM' type='string'>";
172  for (int i=0; i<16; i++) os << std::hex << (((int)(digest[i]))&0xFF);
173  os << "</Parameter>\n";
174 
175  *oc_ << " <Data crate='" << eid.readoutVMECrateId()
176  << "' slot='" << eid.htrSlot()
177  << "' fpga='" << eid.htrTopBottom()
178  << "' fiber='" << eid.fiberIndex()
179  << "' fiberchan='" << eid.fiberChanId()
180  << "' luttype='1' elements='1' encoding='hex'>";
181  for (int i=0; i<16; i++) *oc_ << std::hex << (((int)(digest[i]))&0xFF);
182  *oc_ << "</Data>\n";
183 
184  os <<" <Data elements='128' encoding='hex'> "<<std::endl;
185  os << std::hex;
186  for(int initr2 = 0; initr2 < 128; initr2++){
187  os<<lut[initr2]<<" ";
188  }
189  os << std::dec;
190  os<<std::endl;
191  os <<" </Data> "<<std::endl;
192  os <<"</CFGBrick> "<<std::endl;
193 
194 }
195 
196 void
197 HcalLuttoDB::writeoutlut2(HcalTrigTowerDetId id, HcalElectronicsId eid, const std::vector<unsigned char>& lut, std::ostream& os) {
198 
199  os <<"<CFGBrick> "<<std::endl;
200  os <<" <Parameter name='IETA' type='int'>"<<id.ieta()<<"</Parameter>"<<std::endl;
201  os <<" <Parameter name='IPHI' type='int'>"<<id.iphi()<<"</Parameter>"<<std::endl;
202  os <<" <Parameter name='CRATE' type='int'>"<<eid.readoutVMECrateId()<<"</Parameter>"<<std::endl;
203  os <<" <Parameter name='SLOT' type='int'>"<<eid.htrSlot()<<"</Parameter>"<<std::endl;
204  os <<" <Parameter name='TOPBOTTOM' type='int'>"<<eid.htrTopBottom()<<"</Parameter>"<<std::endl;
205  os <<" <Parameter name='SLB' type='int'>"<<eid.slbSiteNumber()<<"</Parameter>"<<std::endl;
206  os <<" <Parameter name='SLBCHAN' type='int'>"<<eid.slbChannelIndex()<<"</Parameter>"<<std::endl;
207  os <<" <Parameter name='LUT_TYPE' type='int'>2</Parameter>"<<std::endl;
208  os <<" <Parameter name='CREATIONTAG' type='string'>"<<creationtag_<<"</Parameter>"<<std::endl;
209  os <<" <Parameter name='CREATIONSTAMP' type='string'>"<<creationstamp_<<"</Parameter>"<<std::endl;
210  os <<" <Parameter name='FORMATREVISION' type='string'>"<<formatRevision_<<"</Parameter>"<<std::endl;
211  os <<" <Parameter name='TARGETFIRMWARE' type='string'>"<<targetfirmware_<<"</Parameter>"<<std::endl;
212  int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
213  ((id.ieta()<0)?(0):(100));
214 
215 
216  os <<" <Parameter name='GENERALIZEDINDEX' type='int'>"<<generalizedIndex<<"</Parameter>"<<std::endl;
217 
218 
219 // do checksum
220  md5_state_t md5er;
221  md5_byte_t digest[16];
222  md5_init(&md5er);
223  md5_append(&md5er,&(lut[0]),1024);
224  md5_finish(&md5er,digest);
225  os <<" <Parameter name='CHECKSUM' type='string'>";
226  for (int i=0; i<16; i++) os << std::hex << (((int)(digest[i]))&0xFF);
227  os << "</Parameter>\n";
228 
229  *oc_ << " <Data crate='" << eid.readoutVMECrateId()
230  << "' slot='" << eid.htrSlot()
231  << "' fpga='" << eid.htrTopBottom()
232  << "' slb='" << eid.slbSiteNumber()
233  << "' slbchan='" << eid.slbChannelIndex()
234  << "' luttype='2' elements='1' encoding='hex'>";
235  for (int i=0; i<16; i++) *oc_ << std::hex << (((int)(digest[i]))&0xFF);
236  *oc_ << "</Data>\n";
237 
238  os <<" <Data elements='1024' encoding='hex'> "<<std::endl;
239  os << std::hex;
240  for(int initr2 = 0; initr2 < 1024; initr2++){
241  os<< (int(lut[initr2])&0xFF)<<" ";
242  }
243  os << std::dec;
244  os<<std::endl;
245  os <<" </Data> "<<std::endl;
246  os <<"</CFGBrick> "<<std::endl;
247 
248 }
249 
250 std::vector<unsigned char> HcalLuttoDB::extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan) {
251  std::vector<unsigned char> lut;
252  for (int i=0; i<1024; i++) {
253  HcalTriggerPrimitiveSample s=coder.hcalCompress(chan,i,false);
254  lut.push_back(s.compressedEt());
255  }
256  return lut;
257 }
258 
259 // ------------ method called to produce the data ------------
260 void
262 {
263  //using namespace edm;
264  //using namespace std;
265 
266  edm::LogInfo("Hcal") << "Beginning dump of Hcal TPG LUTS (this may take a minute or two)";
267 
268  const HcalElectronicsMap* Map_;
270  iSetup.get<HcalDbRecord>().get( pSetup );
271  Map_=pSetup->getHcalMapping();
272  // get the conditions, for the decoding
273  edm::ESHandle<HcalTPGCoder> inputCoder;
274  iSetup.get<HcalTPGRecord>().get(inputCoder);
275  edm::ESHandle<CaloTPGTranscoder> outTranscoder;
276  iSetup.get<CaloTPGRecord>().get(outTranscoder);
277 
278  std::vector<HcalElectronicsId> allEID = Map_->allElectronicsId();
279  std::vector<HcalElectronicsId>::iterator itreid;
280 
281  std::ostream* pfile=0;
282  oc_=openChecksums();
283 
284  for (int crate=0; crate<20; crate++) {
285  edm::LogInfo("Hcal") << "Beginning crate " << crate;
286  for(itreid = allEID.begin(); itreid != allEID.end(); ++itreid)
287  {
288  if (itreid->readoutVMECrateId()!=crate) continue;
289  if (itreid->isTriggerChainId()) { // lut2
290  HcalTrigTowerDetId tid=Map_->lookupTrigger(*itreid);
291  if (tid.null()) continue;
292 
293  if (filePerCrate_ && pfile==0) pfile=openPerCrate(crate);
294  else if (pfile==0) pfile=openPerLut2(*itreid);
295 
296  std::vector<unsigned char> lut=extractOutputLut(*outTranscoder,tid);
297  writeoutlut2(tid,*itreid,lut,*pfile);
298  if (!filePerCrate_) { delete pfile; pfile=0; }
299  } else { // lut1
300  HcalGenericDetId gid=Map_->lookup(*itreid);
302 
303  if (filePerCrate_ && pfile==0) pfile=openPerCrate(crate);
304  else if (pfile==0) pfile=openPerLut1(*itreid);
305 
306  std::vector<unsigned short> lut=inputCoder->getLinearizationLUT(HcalDetId(gid));
307  writeoutlut1(HcalDetId(gid),*itreid,lut,*pfile);
308  if (!filePerCrate_) { delete pfile; pfile=0; }
309  }
310  }
311  if (pfile!=0) {
312  if (filePerCrate_) *pfile << "</CFGBrickSet>\n";
313  delete pfile;
314  pfile=0;
315  }
316  }
317  *oc_ << "</CFGBrick>\n";
318  delete oc_;
319 
320 }
321 
322 
323 
324 // ------------ method called once each job just before starting event loop ------------
325 void
327 {
328  char buffer[120];
329  time_t now=time(0);
330  struct tm* tm=localtime(&now);
331  strftime(buffer,120,"%F %T",tm);
332  creationstamp_ = buffer;
333 }
334 
335 // ------------ method called once each job just after ending the event loop ------------
336 void
338 }
339 
340 //define this as a plug-in
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
virtual HcalTriggerPrimitiveSample hcalCompress(const HcalTrigTowerDetId &id, unsigned int sample, int fineGrain) const =0
Compression from linear samples+fine grain in the HTR.
int fiberIndex() const
get the fiber index. For VME 1-8 (which of eight fibers carried by a spigot), for uTCA fibers are zer...
virtual void endJob() override
Definition: HcalLuttoDB.cc:337
std::ostream * openPerLut2(HcalElectronicsId eid)
Definition: HcalLuttoDB.cc:129
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
tuple chan
lumi = TPaveText(lowX+0.38, lowY+0.061, lowX+0.45, lowY+0.161, &quot;NDC&quot;) lumi.SetBorderSize( 0 ) lumi...
bool filePerCrate_
Definition: HcalLuttoDB.cc:69
int htrSlot() const
get the htr slot
HcalLuttoDB(const edm::ParameterSet &)
Definition: HcalLuttoDB.cc:86
std::ostream * openChecksums()
Definition: HcalLuttoDB.cc:104
void writeoutlut2(HcalTrigTowerDetId id, HcalElectronicsId eid, const std::vector< unsigned char > &lut, std::ostream &os)
Definition: HcalLuttoDB.cc:197
int readoutVMECrateId() const
get the readout VME crate number
void beginJob()
Definition: Breakpoints.cc:15
int iEvent
Definition: GenABIO.cc:230
std::string targetfirmware_
Definition: HcalLuttoDB.cc:81
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: HcalLuttoDB.cc:261
int htrTopBottom() const
get the htr top/bottom (1=top/0=bottom), valid for VME
std::ostream * oc_
Definition: HcalLuttoDB.cc:76
tuple lut
Definition: lumiPlot.py:244
int fiberChanId() const
get the fiber channel id (which of channels on a fiber)
std::ostream * openPerCrate(int crate)
Definition: HcalLuttoDB.cc:112
int slbChannelIndex() const
get the SLB channel index (valid only for VME trigger-chain ids)
std::string fileformat_
Definition: HcalLuttoDB.cc:71
virtual void beginJob() override
Definition: HcalLuttoDB.cc:326
std::vector< HcalElectronicsId > allElectronicsId() const
const T & get() const
Definition: EventSetup.h:56
bool null() const
is this a null id ?
Definition: DetId.h:45
std::ostream * openPerLut1(HcalElectronicsId eid)
Definition: HcalLuttoDB.cc:120
string fname
main script
std::string creationstamp_
Definition: HcalLuttoDB.cc:70
int slbSiteNumber() const
get the SLB site number (valid only for VME trigger-chain ids)
int compressedEt() const
get the encoded/compressed Et
const DetId lookupTrigger(HcalElectronicsId fId) const
brief lookup the trigger logical detid associated with the given electronics id
void writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector< unsigned short > &lut, std::ostream &os)
Definition: HcalLuttoDB.cc:140
std::string creationtag_
Definition: HcalLuttoDB.cc:80
HcalGenericSubdetector genericSubdet() const
std::vector< unsigned char > extractOutputLut(const CaloTPGTranscoder &coder, HcalTrigTowerDetId chan)
Definition: HcalLuttoDB.cc:250
Readout chain identification for Hcal.
const DetId lookup(HcalElectronicsId fId) const
lookup the logical detid associated with the given electronics id
static const int formatRevision_
Definition: HcalLuttoDB.cc:82