00001
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
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
00060
00061
00062 EcalTrigPrimESProducer::EcalTrigPrimESProducer(const edm::ParameterSet& iConfig) :
00063 dbFilename_(iConfig.getUntrackedParameter<std::string>("DatabaseFile",""))
00064 {
00065
00066
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
00080 }
00081
00082
00083 EcalTrigPrimESProducer::~EcalTrigPrimESProducer()
00084 {
00085 }
00086
00087
00088
00089
00090
00091
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 ;
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
00303 paramF.clear() ;
00304 for (int i=0 ; i <7 ; i++) {
00305 gis>>std::dec>>dataF ;
00306 paramF.push_back(dataF) ;
00307
00308 }
00309
00310 mapPhys_[id] = paramF ;
00311 }
00312
00313 if (dataCard == "CRYSTAL") {
00314 gis>>std::dec>>id ;
00315
00316 param.clear() ;
00317 for (int i=0 ; i <9 ; i++) {
00318 gis>>std::hex>>data ;
00319
00320 param.push_back(data) ;
00321 }
00322
00323 mapXtal_[id] = param ;
00324 }
00325
00326 if (dataCard == "STRIP_EB") {
00327 gis>>std::dec>>id ;
00328
00329 param.clear() ;
00330 for (int i=0 ; i <NBstripparams[0] ; i++) {
00331 gis>>std::hex>>data ;
00332
00333 param.push_back(data) ;
00334 }
00335
00336 mapStrip_[0][id] = param ;
00337 }
00338
00339 if (dataCard == "STRIP_EE") {
00340 gis>>std::dec>>id ;
00341
00342 param.clear() ;
00343 for (int i=0 ; i <NBstripparams[1] ; i++) {
00344 gis>>std::hex>>data ;
00345
00346 param.push_back(data) ;
00347 }
00348
00349 mapStrip_[1][id] = param ;
00350 }
00351
00352 if (dataCard == "TOWER_EB" || dataCard == "TOWER_EE") {
00353 gis>>std::dec>>id ;
00354
00355 param.clear() ;
00356 for (int i=0 ; i <2 ; i++) {
00357 gis>>std::hex>>data ;
00358
00359 param.push_back(data) ;
00360 }
00361
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
00369 param.clear() ;
00370 for (int i=0 ; i <5 ; i++) {
00371 gis>>std::hex>>data ;
00372
00373 param.push_back(data) ;
00374 }
00375
00376 mapWeight_[id] = param ;
00377 }
00378
00379 if (dataCard == "FG") {
00380 gis>>std::hex>>id ;
00381
00382 param.clear() ;
00383 for (int i=0 ; i <5 ; i++) {
00384 gis>>std::hex>>data ;
00385
00386 param.push_back(data) ;
00387 }
00388
00389 mapFg_[id] = param ;
00390 }
00391
00392 if (dataCard == "LUT") {
00393 gis>>std::hex>>id ;
00394
00395 param.clear() ;
00396 for (int i=0 ; i <1024 ; i++) {
00397 gis>>std::hex>>data ;
00398
00399 param.push_back(data) ;
00400 }
00401
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
00412 range.push_back(37) ;
00413 range.push_back(73) ;
00414 range.push_back(1) ;
00415 range.push_back(69) ;
00416 range.push_back(1) ;
00417 range.push_back(6) ;
00418 range.push_back(1) ;
00419 range.push_back(6) ;
00420 } else {
00421
00422 if (subdet >0 ) {
00423 range.push_back(73) ;
00424 range.push_back(109) ;
00425 } else {
00426 range.push_back(1) ;
00427 range.push_back(37) ;
00428 }
00429 range.push_back(1) ;
00430 range.push_back(29) ;
00431 range.push_back(1) ;
00432 range.push_back(6) ;
00433 range.push_back(1) ;
00434 range.push_back(6) ;
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
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492