CMS 3D CMS Logo

EcalTrigPrimESProducer.cc

Go to the documentation of this file.
00001 // user include files
00002 #include "EcalTrigPrimESProducer.h"
00003 
00004 #include <iostream>
00005 #include <fstream>
00006 #include <TMath.h>
00007 #include <sstream>
00008 
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 //
00012 // input stream from a gz file
00013 //
00014 
00015 struct GzInputStream
00016  {
00017   typedef voidp gzFile ;
00018   gzFile gzf ;
00019   char buffer[256] ;
00020   std::istringstream iss ;
00021   bool eof ;
00022   GzInputStream( const char * file )
00023    : eof(false)
00024    {
00025     gzf = gzopen(file,"rb") ;
00026     if (gzf==Z_NULL)
00027      {
00028       eof = true ;
00029       edm::LogWarning("EcalTPG") <<"Database file "<<file<<" not found!!!";
00030      }
00031     else readLine() ;
00032    }
00033   void readLine()
00034    {
00035     char * res = gzgets(gzf,buffer,256) ;
00036     eof = (res==Z_NULL) ;
00037     if (!eof)
00038      {
00039       iss.clear() ;
00040       iss.str(buffer) ;
00041      }
00042    }
00043   ~GzInputStream()
00044    { gzclose(gzf) ; }
00045   operator void*()
00046    { return ((eof==true)?((void*)0):iss) ; }
00047  } ;
00048 
00049 template <typename T>
00050 GzInputStream & operator>>( GzInputStream & gis, T & var )
00051  {
00052   while ((gis)&&(!(gis.iss>>var)))
00053    { gis.readLine() ; }
00054   return gis ;
00055  }
00056 
00057 
00058 //
00059 // constructors and destructor
00060 //
00061 
00062 EcalTrigPrimESProducer::EcalTrigPrimESProducer(const edm::ParameterSet& iConfig) :
00063   dbFilename_(iConfig.getUntrackedParameter<std::string>("DatabaseFile",""))
00064 {
00065   //the following line is needed to tell the framework what
00066   // data is being produced
00067   setWhatProduced(this, &EcalTrigPrimESProducer::producePedestals) ;
00068   setWhatProduced(this, &EcalTrigPrimESProducer::produceLinearizationConst) ;
00069   setWhatProduced(this, &EcalTrigPrimESProducer::produceSlidingWindow) ;
00070   setWhatProduced(this, &EcalTrigPrimESProducer::produceFineGrainEB) ;
00071   setWhatProduced(this, &EcalTrigPrimESProducer::produceFineGrainEEstrip) ;
00072   setWhatProduced(this, &EcalTrigPrimESProducer::produceFineGrainEEtower) ;
00073   setWhatProduced(this, &EcalTrigPrimESProducer::produceLUT) ;
00074   setWhatProduced(this, &EcalTrigPrimESProducer::produceWeight) ;
00075   setWhatProduced(this, &EcalTrigPrimESProducer::produceWeightGroup) ;
00076   setWhatProduced(this, &EcalTrigPrimESProducer::produceLutGroup) ;
00077   setWhatProduced(this, &EcalTrigPrimESProducer::produceFineGrainEBGroup) ;
00078   setWhatProduced(this, &EcalTrigPrimESProducer::producePhysicsConst) ;
00079   //now do what ever other initialization is needed
00080 }
00081 
00082 
00083 EcalTrigPrimESProducer::~EcalTrigPrimESProducer()
00084 { 
00085 }
00086 
00087 //
00088 // member functions
00089 //
00090 
00091 // ------------ method called to produce the data  ------------
00092 
00093 
00094 std::auto_ptr<EcalTPGPedestals> EcalTrigPrimESProducer::producePedestals(const EcalTPGPedestalsRcd & iRecord)
00095 {
00096   std::auto_ptr<EcalTPGPedestals> prod(new EcalTPGPedestals());
00097   parseTextFile() ;
00098   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00099   for (it = mapXtal_.begin() ; it != mapXtal_.end() ; it++) {
00100     EcalTPGPedestal item ;
00101     item.mean_x12 = (it->second)[0] ;
00102     item.mean_x6  = (it->second)[3] ;
00103     item.mean_x1  = (it->second)[6] ;
00104     prod->setValue(it->first,item) ;
00105   }
00106   return prod;
00107 }
00108 
00109 std::auto_ptr<EcalTPGLinearizationConst> EcalTrigPrimESProducer::produceLinearizationConst(const EcalTPGLinearizationConstRcd & iRecord)
00110 {
00111   std::auto_ptr<EcalTPGLinearizationConst> prod(new EcalTPGLinearizationConst());
00112   parseTextFile() ;
00113   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00114   for (it = mapXtal_.begin() ; it != mapXtal_.end() ; it++) {
00115     EcalTPGLinearizationConstant item ;
00116     item.mult_x12 = (it->second)[1] ;
00117     item.mult_x6  = (it->second)[4] ;
00118     item.mult_x1  = (it->second)[7] ;
00119     item.shift_x12 = (it->second)[2] ;
00120     item.shift_x6  = (it->second)[5] ;
00121     item.shift_x1  = (it->second)[8] ;
00122     prod->setValue(it->first,item) ;
00123   }
00124   return prod;
00125 }
00126 
00127 std::auto_ptr<EcalTPGSlidingWindow> EcalTrigPrimESProducer::produceSlidingWindow(const EcalTPGSlidingWindowRcd & iRecord)
00128 {
00129   std::auto_ptr<EcalTPGSlidingWindow> prod(new EcalTPGSlidingWindow());
00130   parseTextFile() ;
00131   for (int subdet=0 ; subdet<2 ; subdet++) {
00132     std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00133     for (it = mapStrip_[subdet].begin() ; it != mapStrip_[subdet].end() ; it++) {
00134       prod->setValue(it->first,(it->second)[0]) ;
00135     }
00136   }
00137   return prod;
00138 }
00139 
00140 std::auto_ptr<EcalTPGFineGrainEBIdMap> EcalTrigPrimESProducer::produceFineGrainEB(const EcalTPGFineGrainEBIdMapRcd & iRecord)
00141 {
00142   std::auto_ptr<EcalTPGFineGrainEBIdMap> prod(new EcalTPGFineGrainEBIdMap());
00143   parseTextFile() ;
00144   EcalTPGFineGrainConstEB fg ;
00145   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00146   for (it = mapFg_.begin() ; it != mapFg_.end() ; it++) {
00147     fg.setValues((it->second)[0], (it->second)[1], (it->second)[2], (it->second)[3], (it->second)[4]) ;
00148     prod->setValue(it->first,fg) ;
00149   }
00150   return prod;
00151 }
00152 
00153 std::auto_ptr<EcalTPGFineGrainStripEE> EcalTrigPrimESProducer::produceFineGrainEEstrip(const EcalTPGFineGrainStripEERcd & iRecord)
00154 {
00155   std::auto_ptr<EcalTPGFineGrainStripEE> prod(new EcalTPGFineGrainStripEE());
00156   parseTextFile() ;
00157   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00158   for (it = mapStrip_[1].begin() ; it != mapStrip_[1].end() ; it++) {
00159     EcalTPGFineGrainStripEE::Item item ;
00160     item.threshold = (it->second)[2] ;
00161     item.lut  = (it->second)[3] ;
00162     prod->setValue(it->first,item) ;
00163   }
00164   return prod;
00165 }
00166 
00167 std::auto_ptr<EcalTPGFineGrainTowerEE> EcalTrigPrimESProducer::produceFineGrainEEtower(const EcalTPGFineGrainTowerEERcd & iRecord)
00168 {
00169   std::auto_ptr<EcalTPGFineGrainTowerEE> prod(new EcalTPGFineGrainTowerEE());
00170   parseTextFile() ;
00171   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00172   for (it = mapTower_[1].begin() ; it != mapTower_[1].end() ; it++) {
00173     prod->setValue(it->first,(it->second)[1]) ;
00174   }
00175   return prod;
00176 }
00177 
00178 std::auto_ptr<EcalTPGLutIdMap> EcalTrigPrimESProducer::produceLUT(const EcalTPGLutIdMapRcd & iRecord)
00179 {
00180   std::auto_ptr<EcalTPGLutIdMap> prod(new EcalTPGLutIdMap());
00181   parseTextFile() ;
00182   EcalTPGLut lut ;
00183   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00184   for (it = mapLut_.begin() ; it != mapLut_.end() ; it++) {
00185     unsigned int lutArray[1024] ;
00186     for (int i=0 ; i <1024 ; i++) lutArray[i] = (it->second)[i] ;
00187     lut.setLut(lutArray) ;
00188     prod->setValue(it->first,lut) ;
00189   }
00190   return prod;
00191 }
00192 
00193 std::auto_ptr<EcalTPGWeightIdMap> EcalTrigPrimESProducer::produceWeight(const EcalTPGWeightIdMapRcd & iRecord)
00194 {
00195   std::auto_ptr<EcalTPGWeightIdMap> prod(new EcalTPGWeightIdMap());
00196   parseTextFile() ;
00197   EcalTPGWeights weights ;
00198   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00199   for (it = mapWeight_.begin() ; it != mapWeight_.end() ; it++) {
00200     weights.setValues((it->second)[0], 
00201                       (it->second)[1],
00202                       (it->second)[2], 
00203                       (it->second)[3],
00204                       (it->second)[4]) ;
00205     prod->setValue(it->first,weights) ;
00206   }
00207   return prod;
00208 }
00209 
00210 std::auto_ptr<EcalTPGWeightGroup> EcalTrigPrimESProducer::produceWeightGroup(const EcalTPGWeightGroupRcd & iRecord)
00211 {
00212   std::auto_ptr<EcalTPGWeightGroup> prod(new EcalTPGWeightGroup());
00213   parseTextFile() ;
00214   for (int subdet=0 ; subdet<2 ; subdet++) {
00215     std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00216     for (it = mapStrip_[subdet].begin() ; it != mapStrip_[subdet].end() ; it++) {
00217       prod->setValue(it->first,(it->second)[1]) ;
00218     }
00219   }
00220   return prod;
00221 }
00222 
00223 std::auto_ptr<EcalTPGLutGroup> EcalTrigPrimESProducer::produceLutGroup(const EcalTPGLutGroupRcd & iRecord)
00224 {
00225   std::auto_ptr<EcalTPGLutGroup> prod(new EcalTPGLutGroup());
00226   parseTextFile() ;
00227   for (int subdet=0 ; subdet<2 ; subdet++) {
00228     std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00229     for (it = mapTower_[subdet].begin() ; it != mapTower_[subdet].end() ; it++) {
00230       prod->setValue(it->first,(it->second)[0]) ;
00231     }
00232   }
00233   return prod;
00234 }
00235 
00236 std::auto_ptr<EcalTPGFineGrainEBGroup> EcalTrigPrimESProducer::produceFineGrainEBGroup(const EcalTPGFineGrainEBGroupRcd & iRecord)
00237 {
00238   std::auto_ptr<EcalTPGFineGrainEBGroup> prod(new EcalTPGFineGrainEBGroup());
00239   parseTextFile() ;
00240   std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00241   for (it = mapTower_[0].begin() ; it != mapTower_[0].end() ; it++) {
00242     prod->setValue(it->first,(it->second)[1]) ;
00243   }
00244   return prod;
00245 }
00246 
00247 std::auto_ptr<EcalTPGPhysicsConst> EcalTrigPrimESProducer::producePhysicsConst(const EcalTPGPhysicsConstRcd & iRecord)
00248 {
00249   std::auto_ptr<EcalTPGPhysicsConst> prod(new EcalTPGPhysicsConst());
00250   parseTextFile() ;
00251   std::map<uint32_t, std::vector<float> >::const_iterator it ;
00252   for (it = mapPhys_.begin() ; it != mapPhys_.end() ; it++) {
00253     EcalTPGPhysicsConst::Item item ;
00254     item.EtSat = (it->second)[0] ;
00255     item.ttf_threshold_Low = (it->second)[1] ;
00256     item.ttf_threshold_High = (it->second)[2] ;
00257     item.FG_lowThreshold = (it->second)[3] ;
00258     item.FG_highThreshold = (it->second)[4] ;
00259     item.FG_lowRatio = (it->second)[5] ;
00260     item.FG_highRatio = (it->second)[6] ;
00261     prod->setValue(it->first,item) ;
00262   }
00263   return prod;
00264 }
00265 
00266 
00267 void EcalTrigPrimESProducer::parseTextFile()
00268 {
00269   if (mapXtal_.size() != 0) return ; // just parse the file once!
00270 
00271   uint32_t id ;
00272   std::string dataCard ;
00273   std::string line;
00274   std::ifstream infile ; 
00275   std::vector<unsigned int> param ;
00276   std::vector<float> paramF ;
00277   int NBstripparams[2] = {2, 4} ;
00278   unsigned int data ;
00279   float dataF ;
00280 
00281   std::string bufString;
00282   std::string iString;
00283   std::string fString;
00284   std::string filename = "SimCalorimetry/EcalTrigPrimProducers/data/"+dbFilename_;
00285   std::string finalFileName ;
00286   size_t slash=dbFilename_.find("/");
00287   if (slash!=0) {
00288     edm::FileInPath fileInPath(filename);
00289     finalFileName = fileInPath.fullPath() ;
00290   }
00291   else {
00292     finalFileName = dbFilename_.c_str() ;
00293     edm::LogWarning("EcalTPG") <<"Couldnt find database file via fileinpath, trying with pathname directly!!";
00294   }
00295 
00296 
00297   GzInputStream gis(finalFileName.c_str()) ;
00298   while (gis>>dataCard) {
00299 
00300     if (dataCard == "PHYSICS_EB" || dataCard == "PHYSICS_EE") {
00301       gis>>std::dec>>id ;
00302       //std::cout<<dataCard<<" "<<std::dec<<id ;
00303       paramF.clear() ;
00304       for (int i=0 ; i <7 ; i++) {
00305         gis>>std::dec>>dataF ;
00306         paramF.push_back(dataF) ;
00307         //std::cout<<", "<<std::dec<<dataF ;
00308       }
00309       //std::cout<<std::endl ;
00310       mapPhys_[id] = paramF ;
00311     }
00312 
00313     if (dataCard == "CRYSTAL") {
00314       gis>>std::dec>>id ;
00315       //std::cout<<dataCard<<" "<<std::dec<<id ;
00316       param.clear() ;
00317       for (int i=0 ; i <9 ; i++) {
00318         gis>>std::hex>>data ;
00319         //std::cout<<", "<<std::hex<<data ;
00320         param.push_back(data) ;
00321       }
00322       //std::cout<<std::endl ;
00323       mapXtal_[id] = param ;
00324     }
00325 
00326     if (dataCard == "STRIP_EB") {
00327       gis>>std::dec>>id ;
00328       //std::cout<<dataCard<<" "<<std::dec<<id ;
00329       param.clear() ;
00330       for (int i=0 ; i <NBstripparams[0] ; i++) {
00331         gis>>std::hex>>data ;
00332         //std::cout<<", "<<std::hex<<data ;
00333         param.push_back(data) ;
00334       }
00335       //std::cout<<std::endl ;
00336       mapStrip_[0][id] = param ;
00337     }
00338 
00339     if (dataCard == "STRIP_EE") {
00340       gis>>std::dec>>id ;
00341       //std::cout<<dataCard<<" "<<std::dec<<id ;
00342       param.clear() ;
00343       for (int i=0 ; i <NBstripparams[1] ; i++) {
00344         gis>>std::hex>>data ;
00345         //std::cout<<", "<<std::hex<<data ;
00346         param.push_back(data) ;
00347       }
00348       //std::cout<<std::endl ;
00349       mapStrip_[1][id] = param ;
00350     }
00351 
00352     if (dataCard == "TOWER_EB" || dataCard == "TOWER_EE") {
00353       gis>>std::dec>>id ;
00354       //std::cout<<dataCard<<" "<<std::dec<<id ;
00355       param.clear() ;
00356       for (int i=0 ; i <2 ; i++) {
00357         gis>>std::hex>>data ;
00358         //std::cout<<", "<<std::hex<<data ;
00359         param.push_back(data) ;
00360       }
00361       //std::cout<<std::endl ;
00362       if (dataCard == "TOWER_EB") mapTower_[0][id] = param ;
00363       if (dataCard == "TOWER_EE") mapTower_[1][id] = param ;
00364     }
00365 
00366     if (dataCard == "WEIGHT") {
00367       gis>>std::hex>>id ;
00368       //std::cout<<dataCard<<" "<<std::dec<<id ;
00369       param.clear() ;
00370       for (int i=0 ; i <5 ; i++) {
00371         gis>>std::hex>>data ;
00372         //std::cout<<", "<<std::hex<<data ;
00373         param.push_back(data) ;
00374       }
00375       //std::cout<<std::endl ;
00376       mapWeight_[id] = param ;
00377     }
00378 
00379     if (dataCard == "FG") {
00380       gis>>std::hex>>id ;
00381       //std::cout<<dataCard<<" "<<std::dec<<id ;
00382       param.clear() ;
00383       for (int i=0 ; i <5 ; i++) {
00384         gis>>std::hex>>data ;
00385         //std::cout<<", "<<std::hex<<data ;
00386         param.push_back(data) ;
00387       }
00388       //std::cout<<std::endl ;
00389       mapFg_[id] = param ;
00390     }
00391  
00392     if (dataCard == "LUT") {
00393       gis>>std::hex>>id ;
00394       //std::cout<<dataCard<<" "<<std::dec<<id ;
00395       param.clear() ;
00396       for (int i=0 ; i <1024 ; i++) {
00397         gis>>std::hex>>data ;
00398         //std::cout<<", "<<std::hex<<data ;
00399         param.push_back(data) ;
00400       }
00401       //std::cout<<std::endl ;
00402       mapLut_[id] = param ;
00403     }
00404   }
00405 }
00406 
00407 std::vector<int> EcalTrigPrimESProducer::getRange(int subdet, int tccNb, int towerNbInTcc, int stripNbInTower, int xtalNbInStrip)
00408 {
00409   std::vector<int> range ;
00410   if (subdet == 0) { 
00411     // Barrel
00412     range.push_back(37)  ; // stccNbMin
00413     range.push_back(73) ; // tccNbMax
00414     range.push_back(1)  ; // towerNbMin
00415     range.push_back(69) ; // towerNbMax
00416     range.push_back(1)  ; // stripNbMin
00417     range.push_back(6)  ; // stripNbMax
00418     range.push_back(1)  ; // xtalNbMin
00419     range.push_back(6)  ; // xtalNbMax
00420   } else {
00421     // Endcap eta >0
00422     if (subdet >0 ) {
00423       range.push_back(73) ; // tccNbMin
00424       range.push_back(109) ; // tccNbMax
00425     } else { //endcap eta <0
00426       range.push_back(1) ; // tccNbMin
00427       range.push_back(37) ; // tccNbMax
00428     }
00429     range.push_back(1)  ; // towerNbMin
00430     range.push_back(29) ; // towerNbMax
00431     range.push_back(1)  ; // stripNbMin
00432     range.push_back(6)  ; // stripNbMax
00433     range.push_back(1)  ; // xtalNbMin
00434     range.push_back(6)  ; // xtalNbMax
00435   }
00436 
00437   if (tccNb>0) {
00438     range[0] = tccNb ; 
00439     range[1] = tccNb+1 ;
00440   }
00441   if (towerNbInTcc>0) {
00442     range[2] = towerNbInTcc ; 
00443     range[3] = towerNbInTcc+1 ;
00444   }
00445   if (stripNbInTower>0) {
00446     range[4] = stripNbInTower ; 
00447     range[5] = stripNbInTower+1 ;
00448   }
00449   if (xtalNbInStrip>0) {
00450     range[6] = xtalNbInStrip ; 
00451     range[7] = xtalNbInStrip+1 ;
00452   }
00453 
00454   return range ;
00455 }
00456 
00457 // bool EcalTrigPrimESProducer::getNextString(gzFile &gzf){
00458 //   size_t blank;
00459 //   if (bufpos_==0) {
00460 //     gzgets(gzf,buf_,80);
00461 //     if (gzeof(gzf)) return true;
00462 //     bufString_=std::string(buf_);
00463 //   }
00464 //   int  pos=0;
00465 //   pos =bufpos_;
00466 //   // look for next non-blank
00467 //   while (pos<bufString_.size()) {
00468 //     if (!bufString_.compare(pos,1," ")) pos++;
00469 //     else break;
00470 //   }
00471 //   blank=bufString_.find(" ",pos);
00472 //   size_t end = blank;
00473 //   if (blank==std::string::npos) end=bufString_.size();
00474 //   sub_=bufString_.substr(pos,end-pos);
00475 //   bufpos_= blank;
00476 //   if (blank==std::string::npos) bufpos_=0;
00477 //   return false;
00478 // }
00479 //  
00480 // int EcalTrigPrimESProducer::converthex() {
00481 //   // converts hex dec string sub to hexa
00482 //   //FIXME:: find something better (istrstream?)!!!!
00483 // 
00484 //   std::string chars("0123456789abcdef");
00485 //   int hex=0;
00486 //   for (size_t i=2;i<sub_.length();++i) {
00487 //     size_t f=chars.find(sub_[i]);
00488 //     if (f==std::string::npos) break;  //FIXME: length is 4 for 0x3!!
00489 //     hex=hex*16+chars.find(sub_[i]);
00490 //   }
00491 //   return hex;
00492 // }

Generated on Tue Jun 9 17:46:22 2009 for CMSSW by  doxygen 1.5.4