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 setWhatProduced(this, &EcalTrigPrimESProducer::produceBadX) ;
00080 setWhatProduced(this, &EcalTrigPrimESProducer::produceBadTT) ;
00081
00082 }
00083
00084
00085 EcalTrigPrimESProducer::~EcalTrigPrimESProducer()
00086 {
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096 std::auto_ptr<EcalTPGPedestals> EcalTrigPrimESProducer::producePedestals(const EcalTPGPedestalsRcd & iRecord)
00097 {
00098 std::auto_ptr<EcalTPGPedestals> prod(new EcalTPGPedestals());
00099 parseTextFile() ;
00100 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00101 for (it = mapXtal_.begin() ; it != mapXtal_.end() ; it++) {
00102 EcalTPGPedestal item ;
00103 item.mean_x12 = (it->second)[0] ;
00104 item.mean_x6 = (it->second)[3] ;
00105 item.mean_x1 = (it->second)[6] ;
00106 prod->setValue(it->first,item) ;
00107 }
00108 return prod;
00109 }
00110
00111 std::auto_ptr<EcalTPGLinearizationConst> EcalTrigPrimESProducer::produceLinearizationConst(const EcalTPGLinearizationConstRcd & iRecord)
00112 {
00113 std::auto_ptr<EcalTPGLinearizationConst> prod(new EcalTPGLinearizationConst());
00114 parseTextFile() ;
00115 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00116 for (it = mapXtal_.begin() ; it != mapXtal_.end() ; it++) {
00117 EcalTPGLinearizationConstant item ;
00118 item.mult_x12 = (it->second)[1] ;
00119 item.mult_x6 = (it->second)[4] ;
00120 item.mult_x1 = (it->second)[7] ;
00121 item.shift_x12 = (it->second)[2] ;
00122 item.shift_x6 = (it->second)[5] ;
00123 item.shift_x1 = (it->second)[8] ;
00124 prod->setValue(it->first,item) ;
00125 }
00126 return prod;
00127 }
00128
00129 std::auto_ptr<EcalTPGSlidingWindow> EcalTrigPrimESProducer::produceSlidingWindow(const EcalTPGSlidingWindowRcd & iRecord)
00130 {
00131 std::auto_ptr<EcalTPGSlidingWindow> prod(new EcalTPGSlidingWindow());
00132 parseTextFile() ;
00133 for (int subdet=0 ; subdet<2 ; subdet++) {
00134 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00135 for (it = mapStrip_[subdet].begin() ; it != mapStrip_[subdet].end() ; it++) {
00136 prod->setValue(it->first,(it->second)[0]) ;
00137 }
00138 }
00139 return prod;
00140 }
00141
00142 std::auto_ptr<EcalTPGFineGrainEBIdMap> EcalTrigPrimESProducer::produceFineGrainEB(const EcalTPGFineGrainEBIdMapRcd & iRecord)
00143 {
00144 std::auto_ptr<EcalTPGFineGrainEBIdMap> prod(new EcalTPGFineGrainEBIdMap());
00145 parseTextFile() ;
00146 EcalTPGFineGrainConstEB fg ;
00147 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00148 for (it = mapFg_.begin() ; it != mapFg_.end() ; it++) {
00149 fg.setValues((it->second)[0], (it->second)[1], (it->second)[2], (it->second)[3], (it->second)[4]) ;
00150 prod->setValue(it->first,fg) ;
00151 }
00152 return prod;
00153 }
00154
00155 std::auto_ptr<EcalTPGFineGrainStripEE> EcalTrigPrimESProducer::produceFineGrainEEstrip(const EcalTPGFineGrainStripEERcd & iRecord)
00156 {
00157 std::auto_ptr<EcalTPGFineGrainStripEE> prod(new EcalTPGFineGrainStripEE());
00158 parseTextFile() ;
00159 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00160 for (it = mapStrip_[1].begin() ; it != mapStrip_[1].end() ; it++) {
00161 EcalTPGFineGrainStripEE::Item item ;
00162 item.threshold = (it->second)[2] ;
00163 item.lut = (it->second)[3] ;
00164 prod->setValue(it->first,item) ;
00165 }
00166 return prod;
00167 }
00168
00169 std::auto_ptr<EcalTPGFineGrainTowerEE> EcalTrigPrimESProducer::produceFineGrainEEtower(const EcalTPGFineGrainTowerEERcd & iRecord)
00170 {
00171 std::auto_ptr<EcalTPGFineGrainTowerEE> prod(new EcalTPGFineGrainTowerEE());
00172 parseTextFile() ;
00173 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00174 for (it = mapTower_[1].begin() ; it != mapTower_[1].end() ; it++) {
00175 prod->setValue(it->first,(it->second)[1]) ;
00176 }
00177 return prod;
00178 }
00179
00180 std::auto_ptr<EcalTPGLutIdMap> EcalTrigPrimESProducer::produceLUT(const EcalTPGLutIdMapRcd & iRecord)
00181 {
00182 std::auto_ptr<EcalTPGLutIdMap> prod(new EcalTPGLutIdMap());
00183 parseTextFile() ;
00184 EcalTPGLut lut ;
00185 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00186 for (it = mapLut_.begin() ; it != mapLut_.end() ; it++) {
00187 unsigned int lutArray[1024] ;
00188 for (int i=0 ; i <1024 ; i++) lutArray[i] = (it->second)[i] ;
00189 lut.setLut(lutArray) ;
00190 prod->setValue(it->first,lut) ;
00191 }
00192 return prod;
00193 }
00194
00195 std::auto_ptr<EcalTPGWeightIdMap> EcalTrigPrimESProducer::produceWeight(const EcalTPGWeightIdMapRcd & iRecord)
00196 {
00197 std::auto_ptr<EcalTPGWeightIdMap> prod(new EcalTPGWeightIdMap());
00198 parseTextFile() ;
00199 EcalTPGWeights weights ;
00200 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00201 for (it = mapWeight_.begin() ; it != mapWeight_.end() ; it++) {
00202 weights.setValues((it->second)[0],
00203 (it->second)[1],
00204 (it->second)[2],
00205 (it->second)[3],
00206 (it->second)[4]) ;
00207 prod->setValue(it->first,weights) ;
00208 }
00209 return prod;
00210 }
00211
00212 std::auto_ptr<EcalTPGWeightGroup> EcalTrigPrimESProducer::produceWeightGroup(const EcalTPGWeightGroupRcd & iRecord)
00213 {
00214 std::auto_ptr<EcalTPGWeightGroup> prod(new EcalTPGWeightGroup());
00215 parseTextFile() ;
00216 for (int subdet=0 ; subdet<2 ; subdet++) {
00217 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00218 for (it = mapStrip_[subdet].begin() ; it != mapStrip_[subdet].end() ; it++) {
00219 prod->setValue(it->first,(it->second)[1]) ;
00220 }
00221 }
00222 return prod;
00223 }
00224
00225 std::auto_ptr<EcalTPGLutGroup> EcalTrigPrimESProducer::produceLutGroup(const EcalTPGLutGroupRcd & iRecord)
00226 {
00227 std::auto_ptr<EcalTPGLutGroup> prod(new EcalTPGLutGroup());
00228 parseTextFile() ;
00229 for (int subdet=0 ; subdet<2 ; subdet++) {
00230 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00231 for (it = mapTower_[subdet].begin() ; it != mapTower_[subdet].end() ; it++) {
00232 prod->setValue(it->first,(it->second)[0]) ;
00233 }
00234 }
00235 return prod;
00236 }
00237
00238 std::auto_ptr<EcalTPGFineGrainEBGroup> EcalTrigPrimESProducer::produceFineGrainEBGroup(const EcalTPGFineGrainEBGroupRcd & iRecord)
00239 {
00240 std::auto_ptr<EcalTPGFineGrainEBGroup> prod(new EcalTPGFineGrainEBGroup());
00241 parseTextFile() ;
00242 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00243 for (it = mapTower_[0].begin() ; it != mapTower_[0].end() ; it++) {
00244 prod->setValue(it->first,(it->second)[1]) ;
00245 }
00246 return prod;
00247 }
00248
00249 std::auto_ptr<EcalTPGPhysicsConst> EcalTrigPrimESProducer::producePhysicsConst(const EcalTPGPhysicsConstRcd & iRecord)
00250 {
00251 std::auto_ptr<EcalTPGPhysicsConst> prod(new EcalTPGPhysicsConst());
00252 parseTextFile() ;
00253 std::map<uint32_t, std::vector<float> >::const_iterator it ;
00254 for (it = mapPhys_.begin() ; it != mapPhys_.end() ; it++) {
00255 EcalTPGPhysicsConst::Item item ;
00256 item.EtSat = (it->second)[0] ;
00257 item.ttf_threshold_Low = (it->second)[1] ;
00258 item.ttf_threshold_High = (it->second)[2] ;
00259 item.FG_lowThreshold = (it->second)[3] ;
00260 item.FG_highThreshold = (it->second)[4] ;
00261 item.FG_lowRatio = (it->second)[5] ;
00262 item.FG_highRatio = (it->second)[6] ;
00263 prod->setValue(it->first,item) ;
00264 }
00265 return prod;
00266 }
00267
00268 std::auto_ptr<EcalTPGCrystalStatus> EcalTrigPrimESProducer::produceBadX(const EcalTPGCrystalStatusRcd & iRecord)
00269 {
00270 std::auto_ptr<EcalTPGCrystalStatus> prod(new EcalTPGCrystalStatus());
00271 parseTextFile() ;
00272 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00273 for (it = mapXtal_.begin() ; it != mapXtal_.end() ; it++) {
00274
00275 EcalTPGCrystalStatusCode badXValue;
00276 badXValue.setStatusCode(0);
00277 prod->setValue(it->first,badXValue) ;
00278 }
00279 return prod;
00280
00281 }
00282
00283 std::auto_ptr<EcalTPGTowerStatus> EcalTrigPrimESProducer::produceBadTT(const EcalTPGTowerStatusRcd & iRecord)
00284 {
00285 std::auto_ptr<EcalTPGTowerStatus> prod(new EcalTPGTowerStatus());
00286 parseTextFile() ;
00287 std::map<uint32_t, std::vector<uint32_t> >::const_iterator it ;
00288
00289 for (it = mapTower_[0].begin() ; it != mapTower_[0].end() ; it++) {
00290
00291 prod->setValue(it->first,0) ;
00292 }
00293
00294 for (it = mapTower_[1].begin() ; it != mapTower_[1].end() ; it++) {
00295
00296 prod->setValue(it->first,0) ;
00297 }
00298
00299 return prod;
00300 }
00301
00302 void EcalTrigPrimESProducer::parseTextFile()
00303 {
00304 if (mapXtal_.size() != 0) return ;
00305
00306 uint32_t id ;
00307 std::string dataCard ;
00308 std::string line;
00309 std::ifstream infile ;
00310 std::vector<unsigned int> param ;
00311 std::vector<float> paramF ;
00312 int NBstripparams[2] = {2, 4} ;
00313 unsigned int data ;
00314 float dataF ;
00315
00316 std::string bufString;
00317 std::string iString;
00318 std::string fString;
00319 std::string filename = "SimCalorimetry/EcalTrigPrimProducers/data/"+dbFilename_;
00320 std::string finalFileName ;
00321 size_t slash=dbFilename_.find("/");
00322 if (slash!=0) {
00323 edm::FileInPath fileInPath(filename);
00324 finalFileName = fileInPath.fullPath() ;
00325 }
00326 else {
00327 finalFileName = dbFilename_.c_str() ;
00328 edm::LogWarning("EcalTPG") <<"Couldnt find database file via fileinpath, trying with pathname directly!!";
00329 }
00330
00331
00332 GzInputStream gis(finalFileName.c_str()) ;
00333 while (gis>>dataCard) {
00334
00335 if (dataCard == "PHYSICS_EB" || dataCard == "PHYSICS_EE") {
00336 gis>>std::dec>>id ;
00337
00338 paramF.clear() ;
00339 for (int i=0 ; i <7 ; i++) {
00340 gis>>std::dec>>dataF ;
00341 paramF.push_back(dataF) ;
00342
00343 }
00344
00345 mapPhys_[id] = paramF ;
00346 }
00347
00348 if (dataCard == "CRYSTAL") {
00349 gis>>std::dec>>id ;
00350
00351 param.clear() ;
00352 for (int i=0 ; i <9 ; i++) {
00353 gis>>std::hex>>data ;
00354
00355 param.push_back(data) ;
00356 }
00357
00358 mapXtal_[id] = param ;
00359 }
00360
00361 if (dataCard == "STRIP_EB") {
00362 gis>>std::dec>>id ;
00363
00364 param.clear() ;
00365 for (int i=0 ; i <NBstripparams[0] ; i++) {
00366 gis>>std::hex>>data ;
00367
00368 param.push_back(data) ;
00369 }
00370
00371 mapStrip_[0][id] = param ;
00372 }
00373
00374 if (dataCard == "STRIP_EE") {
00375 gis>>std::dec>>id ;
00376
00377 param.clear() ;
00378 for (int i=0 ; i <NBstripparams[1] ; i++) {
00379 gis>>std::hex>>data ;
00380
00381 param.push_back(data) ;
00382 }
00383
00384 mapStrip_[1][id] = param ;
00385 }
00386
00387 if (dataCard == "TOWER_EB" || dataCard == "TOWER_EE") {
00388 gis>>std::dec>>id ;
00389
00390 param.clear() ;
00391 for (int i=0 ; i <2 ; i++) {
00392 gis>>std::hex>>data ;
00393
00394 param.push_back(data) ;
00395 }
00396
00397 if (dataCard == "TOWER_EB") mapTower_[0][id] = param ;
00398 if (dataCard == "TOWER_EE") mapTower_[1][id] = param ;
00399 }
00400
00401 if (dataCard == "WEIGHT") {
00402 gis>>std::hex>>id ;
00403
00404 param.clear() ;
00405 for (int i=0 ; i <5 ; i++) {
00406 gis>>std::hex>>data ;
00407
00408 param.push_back(data) ;
00409 }
00410
00411 mapWeight_[id] = param ;
00412 }
00413
00414 if (dataCard == "FG") {
00415 gis>>std::hex>>id ;
00416
00417 param.clear() ;
00418 for (int i=0 ; i <5 ; i++) {
00419 gis>>std::hex>>data ;
00420
00421 param.push_back(data) ;
00422 }
00423
00424 mapFg_[id] = param ;
00425 }
00426
00427 if (dataCard == "LUT") {
00428 gis>>std::hex>>id ;
00429
00430 param.clear() ;
00431 for (int i=0 ; i <1024 ; i++) {
00432 gis>>std::hex>>data ;
00433
00434 param.push_back(data) ;
00435 }
00436
00437 mapLut_[id] = param ;
00438 }
00439 }
00440 }
00441
00442 std::vector<int> EcalTrigPrimESProducer::getRange(int subdet, int tccNb, int towerNbInTcc, int stripNbInTower, int xtalNbInStrip)
00443 {
00444 std::vector<int> range ;
00445 if (subdet == 0) {
00446
00447 range.push_back(37) ;
00448 range.push_back(73) ;
00449 range.push_back(1) ;
00450 range.push_back(69) ;
00451 range.push_back(1) ;
00452 range.push_back(6) ;
00453 range.push_back(1) ;
00454 range.push_back(6) ;
00455 } else {
00456
00457 if (subdet >0 ) {
00458 range.push_back(73) ;
00459 range.push_back(109) ;
00460 } else {
00461 range.push_back(1) ;
00462 range.push_back(37) ;
00463 }
00464 range.push_back(1) ;
00465 range.push_back(29) ;
00466 range.push_back(1) ;
00467 range.push_back(6) ;
00468 range.push_back(1) ;
00469 range.push_back(6) ;
00470 }
00471
00472 if (tccNb>0) {
00473 range[0] = tccNb ;
00474 range[1] = tccNb+1 ;
00475 }
00476 if (towerNbInTcc>0) {
00477 range[2] = towerNbInTcc ;
00478 range[3] = towerNbInTcc+1 ;
00479 }
00480 if (stripNbInTower>0) {
00481 range[4] = stripNbInTower ;
00482 range[5] = stripNbInTower+1 ;
00483 }
00484 if (xtalNbInStrip>0) {
00485 range[6] = xtalNbInStrip ;
00486 range[7] = xtalNbInStrip+1 ;
00487 }
00488
00489 return range ;
00490 }
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527