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