CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/CalibCalorimetry/HcalTPGIO/src/HcalLuttoDB.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    HcalLuttoDB
00004 // Class:      HcalLuttoDB
00005 // 
00013 //
00014 // Original Author:  Michael Weinberger
00015 //         Created:  Mon Mar 19 11:53:56 CDT 2007
00016 // $Id: HcalLuttoDB.cc,v 1.6 2009/12/17 21:20:49 wmtan Exp $
00017 //
00018 //
00019 
00020 
00021 // system include files
00022 #include <memory>
00023 
00024 // user include files
00025 #include "FWCore/Framework/interface/EDAnalyzer.h"
00026 #include "FWCore/Framework/interface/EventSetup.h"
00027 #include "FWCore/Framework/interface/MakerMacros.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00029 #include "FWCore/Framework/interface/ESHandle.h"
00030 
00031 #include "CalibFormats/HcalObjects/interface/HcalDbService.h"
00032 
00033 #include "DataFormats/DetId/interface/DetId.h"
00034 #include "DataFormats/HcalDetId/interface/HcalSubdetector.h"
00035 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00036 #include "DataFormats/HcalDetId/interface/HcalGenericDetId.h"
00037 #include "DataFormats/HcalDetId/interface/HcalTrigTowerDetId.h"
00038 #include "DataFormats/HcalDetId/interface/HcalElectronicsId.h"
00039 #include "CondFormats/HcalObjects/interface/HcalElectronicsMap.h"
00040 #include "CalibFormats/HcalObjects/interface/HcalDbRecord.h"
00041 
00042 #include "CalibFormats/HcalObjects/interface/HcalTPGRecord.h"
00043 #include "CalibFormats/HcalObjects/interface/HcalTPGCoder.h"
00044 #include "CalibFormats/CaloTPG/interface/CaloTPGRecord.h"
00045 #include "CalibFormats/CaloTPG/interface/CaloTPGTranscoder.h"
00046 
00047 using namespace edm;
00048 using namespace std;
00049 #include <iostream>
00050 #include <fstream>
00051 #include "FWCore/Utilities/interface/md5.h"
00052 
00053 //
00054 // class decleration
00055 //
00056 
00057 class HcalLuttoDB : public edm::EDAnalyzer {
00058 public:
00059   explicit HcalLuttoDB(const edm::ParameterSet&);
00060   ~HcalLuttoDB();
00061   
00062   virtual void beginJob() ;
00063   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00064   virtual void endJob() ;
00065 
00066 private:
00067   void writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector<unsigned short>& lut, std::ostream& os);
00068   std::vector<unsigned char> extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan);
00069   void writeoutlut2(HcalTrigTowerDetId id, HcalElectronicsId eid, const std::vector<unsigned char>& lut, std::ostream& os);
00070   bool filePerCrate_;
00071   std::string creationstamp_;
00072   std::string fileformat_;
00073   std::ostream* openPerCrate(int crate);
00074   std::ostream* openPerLut1(HcalElectronicsId eid);
00075   std::ostream* openPerLut2(HcalElectronicsId eid);
00076   std::ostream* openChecksums();
00077   std::ostream* oc_;
00078 };      
00079       // ----------member data ---------------------------
00080  
00081 std::string creationtag_;
00082 std::string targetfirmware_;
00083 static const int formatRevision_=1;
00084 
00085 // constructors and destructor
00086 //
00087 HcalLuttoDB::HcalLuttoDB(const edm::ParameterSet& iConfig)
00088 {
00089   
00090   creationtag_       = iConfig.getParameter<std::string>("creationtag");
00091   targetfirmware_    = iConfig.getParameter<std::string>("targetfirmware");
00092   filePerCrate_ = iConfig.getUntrackedParameter<bool>("filePerCrate",true);
00093   fileformat_=iConfig.getParameter<std::string>("filePrefix");
00094 }
00095 
00096 HcalLuttoDB::~HcalLuttoDB()
00097 {
00098 }
00099 
00100 
00101 //
00102 // member functions
00103 //
00104 
00105 std::ostream* HcalLuttoDB::openChecksums() {
00106   char fname[1024];
00107   snprintf(fname,1024,"%s_checksums.xml",fileformat_.c_str());
00108   std::ostream* os=new std::ofstream(fname);
00109   (*os) << "<?xml version=\"1.0\"?>\n<CFGBrick>\n";
00110   return os;
00111 }
00112 
00113 std::ostream* HcalLuttoDB::openPerCrate(int crate) {
00114   char fname[1024];
00115   snprintf(fname,1024,"%s_%d.xml",fileformat_.c_str(),crate);
00116   std::ostream* os=new std::ofstream(fname);
00117   (*os) << "<?xml version=\"1.0\"?>\n<CFGBrickSet>\n";
00118   return os;
00119 }
00120 
00121 std::ostream* HcalLuttoDB::openPerLut1(HcalElectronicsId eid) {
00122   char fname[1024];
00123   snprintf(fname,1024,"%s_%d_%d%c_%d_%d_1.xml",fileformat_.c_str(),eid.readoutVMECrateId(),
00124            eid.htrSlot(),((eid.htrTopBottom())?('t'):('b')),eid.fiberIndex(),eid.fiberChanId());
00125   std::ostream* os=new std::ofstream(fname);
00126   (*os) << "<?xml version=\"1.0\"?>\n";
00127   return os;
00128 }
00129 
00130 std::ostream* HcalLuttoDB::openPerLut2(HcalElectronicsId eid) {
00131   char fname[1024];
00132   snprintf(fname,1024,"%s_%d_%d%c_%d_%d_2.xml",fileformat_.c_str(),eid.readoutVMECrateId(),
00133            eid.htrSlot(),((eid.htrTopBottom())?('t'):('b')),eid.slbSiteNumber(),eid.slbChannelIndex());
00134   std::ostream* os=new std::ofstream(fname);
00135   (*os) << "<?xml version=\"1.0\"?>\n";
00136   return os;
00137 }
00138 
00139 
00140 void
00141 HcalLuttoDB::writeoutlut1(HcalDetId id, HcalElectronicsId eid, const std::vector<unsigned short>& lut, std::ostream& os) {
00142 
00143   os <<"<CFGBrick> "<<std::endl;
00144   os <<" <Parameter name='IETA' type='int'>"<<id.ieta()<<"</Parameter>"<<std::endl;
00145   os <<" <Parameter name='IPHI' type='int'>"<<id.iphi()<<"</Parameter>"<<std::endl;
00146   os <<" <Parameter name='DEPTH' type='int'>"<<id.depth()<<"</Parameter>"<<std::endl;
00147   os <<" <Parameter name='CRATE' type='int'>"<<eid.readoutVMECrateId()<<"</Parameter>"<<std::endl;
00148   os <<" <Parameter name='SLOT' type='int'>"<<eid.htrSlot()<<"</Parameter>"<<std::endl;
00149   os <<" <Parameter name='TOPBOTTOM' type='int'>"<<eid.htrTopBottom()<<"</Parameter>"<<std::endl;
00150   os <<" <Parameter name='FIBER' type='int'>"<<eid.fiberIndex()<<"</Parameter>"<<std::endl;
00151   os <<" <Parameter name='FIBERCHAN' type='int'>"<<eid.fiberChanId()<<"</Parameter>"<<std::endl;
00152   os <<" <Parameter name='LUT_TYPE' type='int'>1</Parameter>"<<std::endl;
00153   os <<" <Parameter name='CREATIONTAG' type='string'>"<<creationtag_<<"</Parameter>"<<std::endl;
00154   os <<" <Parameter name='CREATIONSTAMP' type='string'>"<<creationstamp_<<"</Parameter>"<<std::endl;
00155   os <<" <Parameter name='FORMATREVISION' type='string'>"<<formatRevision_<<"</Parameter>"<<std::endl;
00156   os <<" <Parameter name='TARGETFIRMWARE' type='string'>"<<targetfirmware_<<"</Parameter>"<<std::endl;
00157   int generalizedIndex=id.ietaAbs()+1000*id.depth()+10000*id.iphi()+
00158     ((id.ieta()<0)?(0):(100))+((id.subdet()==HcalForward && id.ietaAbs()==29)?(4*10000):(0));
00159 
00160   os <<" <Parameter name='GENERALIZEDINDEX' type='int'>"<<generalizedIndex<<"</Parameter>"<<std::endl;
00161   // do checksum
00162   md5_state_t md5er;
00163   md5_byte_t digest[16];
00164   unsigned char tool[2];
00165   md5_init(&md5er);
00166   for (int i=0; i<128; i++) {
00167     tool[0]=lut[i]&0xFF;
00168     tool[1]=(lut[i]>>8)&0xFF;
00169     md5_append(&md5er,tool,2);
00170   }
00171   md5_finish(&md5er,digest);
00172   os <<" <Parameter name='CHECKSUM' type='string'>";
00173   for (int i=0; i<16; i++) os << std::hex << (((int)(digest[i]))&0xFF);
00174   os << "</Parameter>\n";
00175 
00176   *oc_ << "  <Data crate='" << eid.readoutVMECrateId()
00177        << "' slot='" << eid.htrSlot()
00178        << "' fpga='" << eid.htrTopBottom()
00179        << "' fiber='" << eid.fiberIndex()
00180        << "' fiberchan='" << eid.fiberChanId()
00181        << "' luttype='1' elements='1' encoding='hex'>";
00182   for (int i=0; i<16; i++) *oc_ << std::hex << (((int)(digest[i]))&0xFF);    
00183   *oc_ << "</Data>\n";
00184 
00185   os <<" <Data elements='128' encoding='hex'> "<<std::endl;
00186   os << std::hex;
00187   for(int initr2 = 0; initr2 < 128; initr2++){
00188     os<<lut[initr2]<<" ";
00189   } 
00190   os << std::dec;
00191   os<<std::endl;
00192   os <<" </Data> "<<std::endl;
00193   os <<"</CFGBrick> "<<std::endl;
00194        
00195 }
00196 
00197 void
00198 HcalLuttoDB::writeoutlut2(HcalTrigTowerDetId id, HcalElectronicsId eid, const std::vector<unsigned char>& lut, std::ostream& os) {
00199 
00200   os <<"<CFGBrick> "<<std::endl;
00201   os <<" <Parameter name='IETA' type='int'>"<<id.ieta()<<"</Parameter>"<<std::endl;
00202   os <<" <Parameter name='IPHI' type='int'>"<<id.iphi()<<"</Parameter>"<<std::endl;
00203   os <<" <Parameter name='CRATE' type='int'>"<<eid.readoutVMECrateId()<<"</Parameter>"<<std::endl;
00204   os <<" <Parameter name='SLOT' type='int'>"<<eid.htrSlot()<<"</Parameter>"<<std::endl;
00205   os <<" <Parameter name='TOPBOTTOM' type='int'>"<<eid.htrTopBottom()<<"</Parameter>"<<std::endl;
00206   os <<" <Parameter name='SLB' type='int'>"<<eid.slbSiteNumber()<<"</Parameter>"<<std::endl;
00207   os <<" <Parameter name='SLBCHAN' type='int'>"<<eid.slbChannelIndex()<<"</Parameter>"<<std::endl;
00208   os <<" <Parameter name='LUT_TYPE' type='int'>2</Parameter>"<<std::endl;
00209   os <<" <Parameter name='CREATIONTAG' type='string'>"<<creationtag_<<"</Parameter>"<<std::endl;
00210   os <<" <Parameter name='CREATIONSTAMP' type='string'>"<<creationstamp_<<"</Parameter>"<<std::endl;
00211   os <<" <Parameter name='FORMATREVISION' type='string'>"<<formatRevision_<<"</Parameter>"<<std::endl;
00212   os <<" <Parameter name='TARGETFIRMWARE' type='string'>"<<targetfirmware_<<"</Parameter>"<<std::endl;
00213   int generalizedIndex=id.ietaAbs()+10000*id.iphi()+
00214     ((id.ieta()<0)?(0):(100));
00215 
00216 
00217   os <<" <Parameter name='GENERALIZEDINDEX' type='int'>"<<generalizedIndex<<"</Parameter>"<<std::endl;
00218 
00219 
00220 // do checksum
00221   md5_state_t md5er;
00222   md5_byte_t digest[16];
00223   md5_init(&md5er);
00224   md5_append(&md5er,&(lut[0]),1024);
00225   md5_finish(&md5er,digest);
00226   os <<" <Parameter name='CHECKSUM' type='string'>";
00227   for (int i=0; i<16; i++) os << std::hex << (((int)(digest[i]))&0xFF);
00228   os << "</Parameter>\n";
00229 
00230   *oc_ << "  <Data crate='" << eid.readoutVMECrateId()
00231        << "' slot='" << eid.htrSlot()
00232        << "' fpga='" << eid.htrTopBottom()
00233        << "' slb='" << eid.slbSiteNumber()
00234        << "' slbchan='" << eid.slbChannelIndex()
00235        << "' luttype='2' elements='1' encoding='hex'>";
00236   for (int i=0; i<16; i++) *oc_ << std::hex << (((int)(digest[i]))&0xFF);    
00237   *oc_ << "</Data>\n";
00238 
00239   os <<" <Data elements='1024' encoding='hex'> "<<std::endl;
00240   os << std::hex;
00241   for(int initr2 = 0; initr2 < 1024; initr2++){
00242     os<< (int(lut[initr2])&0xFF)<<" ";
00243   } 
00244   os << std::dec;
00245   os<<std::endl;
00246   os <<" </Data> "<<std::endl;
00247   os <<"</CFGBrick> "<<std::endl;
00248        
00249 }
00250 
00251 std::vector<unsigned char> HcalLuttoDB::extractOutputLut(const CaloTPGTranscoder& coder, HcalTrigTowerDetId chan) {
00252   std::vector<unsigned char> lut;
00253   for (int i=0; i<1024; i++) {
00254     HcalTriggerPrimitiveSample s=coder.hcalCompress(chan,i,false);
00255     lut.push_back(s.compressedEt());
00256   }
00257   return lut;
00258 }
00259 
00260 // ------------ method called to produce the data  ------------
00261 void
00262 HcalLuttoDB::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00263 {
00264   //using namespace edm;
00265   //using namespace std;
00266   
00267   edm::LogInfo("Hcal") << "Beginning dump of Hcal TPG LUTS (this may take a minute or two)";
00268 
00269   const HcalElectronicsMap* Map_;
00270   ESHandle<HcalDbService> pSetup;
00271   iSetup.get<HcalDbRecord>().get( pSetup );
00272   Map_=pSetup->getHcalMapping();
00273   // get the conditions, for the decoding
00274   edm::ESHandle<HcalTPGCoder> inputCoder;
00275   iSetup.get<HcalTPGRecord>().get(inputCoder);
00276   edm::ESHandle<CaloTPGTranscoder> outTranscoder;
00277   iSetup.get<CaloTPGRecord>().get(outTranscoder);
00278   outTranscoder->setup(iSetup,CaloTPGTranscoder::HcalTPG);
00279 
00280   std::vector<HcalElectronicsId> allEID = Map_->allElectronicsId();
00281   std::vector<HcalElectronicsId>::iterator itreid;
00282 
00283   std::ostream* pfile=0;
00284   oc_=openChecksums();
00285   
00286   for (int crate=0; crate<20; crate++) {
00287     edm::LogInfo("Hcal") << "Beginning crate " << crate;
00288     for(itreid  = allEID.begin(); itreid != allEID.end(); itreid++)
00289       {
00290         if (itreid->readoutVMECrateId()!=crate) continue;
00291         if (itreid->isTriggerChainId()) { // lut2
00292           HcalTrigTowerDetId tid=Map_->lookupTrigger(*itreid);
00293           if (tid.null()) continue;
00294 
00295           if (filePerCrate_ && pfile==0) pfile=openPerCrate(crate);
00296           else if (pfile==0) pfile=openPerLut2(*itreid);
00297 
00298           std::vector<unsigned char> lut=extractOutputLut(*outTranscoder,tid);
00299           writeoutlut2(tid,*itreid,lut,*pfile);
00300           if (!filePerCrate_) { delete pfile; pfile=0; }          
00301         } else { // lut1
00302           HcalGenericDetId gid=Map_->lookup(*itreid);
00303           if (gid.null() || !(gid.genericSubdet()==HcalGenericDetId::HcalGenBarrel || gid.genericSubdet()==HcalGenericDetId::HcalGenEndcap || gid.genericSubdet()==HcalGenericDetId::HcalGenForward)) continue;
00304           
00305           if (filePerCrate_ && pfile==0) pfile=openPerCrate(crate);
00306           else if (pfile==0) pfile=openPerLut1(*itreid);
00307 
00308           std::vector<unsigned short> lut=inputCoder->getLinearizationLUT(HcalDetId(gid));
00309           writeoutlut1(HcalDetId(gid),*itreid,lut,*pfile);
00310           if (!filePerCrate_) { delete pfile; pfile=0; }
00311         }
00312       }
00313     if (pfile!=0) {
00314       if (filePerCrate_) *pfile << "</CFGBrickSet>\n";
00315       delete pfile;
00316       pfile=0;
00317     }
00318   }
00319   *oc_ << "</CFGBrick>\n";
00320   delete oc_;
00321 
00322   outTranscoder->releaseSetup();
00323 }
00324 
00325 
00326 
00327 // ------------ method called once each job just before starting event loop  ------------
00328 void 
00329 HcalLuttoDB::beginJob()
00330 {
00331   char buffer[120];
00332   time_t now=time(0);
00333   struct tm* tm=localtime(&now);
00334   strftime(buffer,120,"%F %T",tm);
00335   creationstamp_     = buffer;
00336 }
00337 
00338 // ------------ method called once each job just after ending the event loop  ------------
00339 void 
00340 HcalLuttoDB::endJob() {
00341 }
00342 
00343 //define this as a plug-in
00344 DEFINE_FWK_MODULE(HcalLuttoDB);