CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/SimCalorimetry/EcalSelectiveReadoutProducers/src/EcalSRCondTools.cc

Go to the documentation of this file.
00001 //emacs settings:-*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
00002 /*
00003  * $Id: EcalSRCondTools.cc,v 1.3 2010/09/13 20:04:28 pgras Exp $
00004  *
00005  * author: Ph Gras. June, 2010
00006  */
00007 
00008 #include "SimCalorimetry/EcalSelectiveReadoutProducers/interface/EcalSRCondTools.h"
00009 
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 #include "FWCore/Framework/interface/Event.h"
00012 #include "FWCore/Framework/interface/EventSetup.h"
00013 #include "FWCore/ServiceRegistry/interface/Service.h"
00014 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
00015 
00016 #include "CondFormats/EcalObjects/interface/EcalSRSettings.h"
00017 #include "CondFormats/DataRecord/interface/EcalSRSettingsRcd.h"
00018 #include "CondFormats/EcalObjects/interface/EcalTPGPhysicsConst.h"
00019 #include "CondFormats/DataRecord/interface/EcalTPGPhysicsConstRcd.h"
00020 
00021 #include <string>
00022 #include <fstream>
00023 #include <algorithm>
00024 
00025 static int tccNum[12][12] = {
00026   /* EE- */
00027   {  36, 19, 20, 21, 22, 23, 18,  1,  2,  3,  4,  5}, //SRP 1
00028   {  24, 25, 26, 27, 28, 29,  6,  7,  8,  9, 10, 11}, //SRP 2
00029   {  30, 31, 32, 33, 34, 35, 12, 13, 14, 15, 16, 17}, //SRP 3
00030   /* EB- */
00031   { 54, 37, 38, 39, 40, 41, -1, -1, -1, -1, -1, -1},  //SRP 4
00032   { 42, 43, 44, 45, 46, 47, -1, -1, -1, -1, -1, -1},  //SRP 5
00033   { 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1, -1},  //SRP 6
00034   /* EB+ */
00035   { 72, 55, 56, 57, 58, 59, -1, -1, -1, -1, -1, -1},  //SRP 7
00036   { 60, 61, 62, 63, 64, 65, -1, -1, -1, -1, -1, -1},  //SRP 8
00037   { 66, 67, 68, 69, 70, 71, -1, -1, -1, -1, -1, -1},  //SRP 9
00038   /* EE+ */
00039   {  90, 73, 74, 75, 76, 77,108, 91, 92, 93, 94, 95}, //SRP 10
00040   {  78, 79, 80, 81, 82, 83, 96, 97, 98, 99,100,101}, //SRP 11
00041   {  84, 85, 86, 87, 88, 89,102,103,104,105,106,107}   //SRP 12
00042 };
00043 
00044 static int dccNum[12][12] = {
00045   {  1,  2,  3, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 1 
00046   {  4,  5,  6, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 2 
00047   {  7,  8,  9, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 3 
00048   { 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1},  //SRP 4 
00049   { 16, 17, 18, 19, 20, 21, -1, -1, -1, -1, -1, -1},  //SRP 5 
00050   { 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1},  //SRP 6 
00051   { 28, 29, 30, 31, 32, 33, -1, -1, -1, -1, -1, -1},  //SRP 7 
00052   { 34, 35, 36, 37, 38, 39, -1, -1, -1, -1, -1, -1},  //SRP 8 
00053   { 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, -1, -1},  //SRP 9 
00054   { 46, 47, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 10
00055   { 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 11
00056   { 52, 53, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1}  //SRP 12
00057 };
00058 
00059 
00060 using namespace std;
00061 
00062 EcalSRCondTools::EcalSRCondTools(const edm::ParameterSet& ps):ps_(ps){
00063 }
00064 
00065 
00066 EcalSRCondTools::~EcalSRCondTools(){
00067 }
00068 
00069 void
00070 EcalSRCondTools::analyze(const edm::Event& event, const edm::EventSetup& es){
00071   static bool done = false;
00072   if(done) return;
00073   EcalSRSettings* sr = new EcalSRSettings;
00074 
00075   string mode = ps_.getParameter<string>("mode");
00076   bool iomode;
00077   const bool iomode_read = false;
00078   const bool iomode_write = true;
00079 
00080   if(mode == "online_config" || mode == "combine_config"){
00081     iomode =iomode_write;
00082     string fname = ps_.getParameter<string>("onlineSrpConfigFile");
00083     ifstream f(fname.c_str());
00084     if(!f.good()){
00085       throw cms::Exception("EcalSRCondTools") << "Failed to open file " << fname;
00086     }
00087     importSrpConfigFile(*sr, f, true);
00088   }
00089 
00090   if(mode=="python_config" || mode=="combine_config"){
00091     iomode = iomode_write;
00092     importParameterSet(*sr, ps_);
00093   }
00094 
00095   if(mode=="read"){
00096     iomode = iomode_read;
00097   }
00098 
00099   if(!(mode=="python_config" || mode == "online_config" || mode == "combine_config" || (mode=="read"))){
00100     throw cms::Exception("Config") << "Invalid value," << mode << ",  for parameter mode. "
00101                                    << "Valid values: online_config, python_config, combine_config, read";
00102   }
00103 
00104   if(iomode==iomode_write){
00105     sr->bxGlobalOffset_ = ps_.getParameter<int>("bxGlobalOffset");
00106     sr->automaticSrpSelect_ = ps_.getParameter<int>("automaticSrpSelect");
00107     sr->automaticMasks_ = ps_.getParameter<int>("automaticMasks");
00108     
00109     edm::Service<cond::service::PoolDBOutputService> db;
00110     if( !db.isAvailable() ){
00111       throw cms::Exception("CondDBAccess") << "Failed to connect to PoolDBOutputService\n";
00112     }
00113     //fillup DB
00114     //create new infinite IOV
00115     cond::Time_t firstSinceTime = db->beginOfTime();
00116     db->writeOne(sr,firstSinceTime,"EcalSRSettingsRcd");
00117     done = true;
00118   } else {//read mode
00119     edm::ESHandle<EcalSRSettings> hSr;
00120     es.get<EcalSRSettingsRcd>().get(hSr);
00121     if(!hSr.isValid()){
00122       cout << "EcalSRSettings record not found. Check the Cond DB Global tag.\n";
00123     } else{
00124       const EcalSRSettings* sr = hSr.product();
00125       cout << "ECAL Seletive readout settings:\n";
00126       cout << *sr << "\n" << endl;
00127     }
00128     
00129     //trigger tower thresholds (from FENIX configuration):
00130     edm::ESHandle<EcalTPGPhysicsConst> hTp;
00131     es.get<EcalTPGPhysicsConstRcd>().get(hTp);
00132     if(!hTp.isValid()){
00133       cout << "EcalTPGPhysicsConst record not found. Check the Cond DB Global tag.\n";
00134     } else{
00135       const EcalTPGPhysicsConst * tp = hTp.product();
00136       const EcalTPGPhysicsConstMap mymap = tp->getMap();
00137       if(mymap.size()!=2){
00138         cout << "Error: TPG physics record is of unexpected size: "
00139              << mymap.size()  << " elements instead of two (one for EB, one for EE)\n";
00140       } else{
00141         EcalTPGPhysicsConstMap::const_iterator it=mymap.begin();
00142         cout << "----------------------------------------------------------------------\n"
00143           "Trigger tower Et thresholds extracted from TPG configuration \n"
00144           "(EcalSRCondTools modules supports only read mode for these parameters):\n\n";
00145         cout<< "EB: " << "LT = " << it->second.ttf_threshold_Low << " GeV "
00146             << "HT = "<< it->second.ttf_threshold_High << " GeV\n";
00147         ++it;
00148         cout<< "EE: " << "LT = " << it->second.ttf_threshold_Low << " GeV "
00149             << "HT = "<< it->second.ttf_threshold_High << " GeV\n";
00150         
00151       }
00152     }
00153   }
00154 }
00155 
00156 void EcalSRCondTools::importParameterSet(EcalSRSettings& sr, const edm::ParameterSet& ps){
00157   sr.deltaPhi_.resize(1);
00158   sr.deltaPhi_[0] = ps.getParameter<int >("deltaPhi");
00159   sr.deltaEta_.resize(1);
00160   sr.deltaEta_[0] = ps.getParameter<int >("deltaEta");
00161   sr.ecalDccZs1stSample_.resize(1);
00162   sr.ecalDccZs1stSample_[0] = ps.getParameter<int >("ecalDccZs1stSample");
00163   sr.ebDccAdcToGeV_ = ps.getParameter<double >("ebDccAdcToGeV");
00164   sr.eeDccAdcToGeV_ = ps.getParameter<double >("eeDccAdcToGeV");
00165   sr.dccNormalizedWeights_.resize(1);
00166   const std::vector<double>& w = ps.getParameter<std::vector<double>  >("dccNormalizedWeights");
00167   sr.dccNormalizedWeights_[0].resize(w.size());
00168   std::copy(w.begin(), w.end(), sr.dccNormalizedWeights_[0].begin());
00169   sr.symetricZS_.resize(1);
00170   sr.symetricZS_[0] = ps.getParameter<bool >("symetricZS");
00171   sr.srpLowInterestChannelZS_.resize(2);
00172   const int eb = 0;
00173   const int ee = 1;
00174   sr.srpLowInterestChannelZS_[eb] = ps.getParameter<double >("srpBarrelLowInterestChannelZS");
00175   sr.srpLowInterestChannelZS_[ee] = ps.getParameter<double >("srpEndcapLowInterestChannelZS");
00176   sr.srpHighInterestChannelZS_.resize(2);
00177   sr.srpHighInterestChannelZS_[eb] = ps.getParameter<double >("srpBarrelHighInterestChannelZS");
00178   sr.srpHighInterestChannelZS_[ee] = ps.getParameter<double >("srpEndcapHighInterestChannelZS");
00179   //sr.trigPrimBypass_.resize(1);
00180   //sr.trigPrimBypass_[0] = ps.getParameter<bool >("trigPrimBypass");
00181   //sr.trigPrimBypassMode_.resize(1);
00182   //sr.trigPrimBypassMode_[0] = ps.getParameter<int >("trigPrimBypassMode");
00183   //sr.trigPrimBypassLTH_.resize(1);
00184   //sr.trigPrimBypassLTH_[0] = ps.getParameter<double >("trigPrimBypassLTH");
00185   //sr.trigPrimBypassHTH_.resize(1);
00186   //sr.trigPrimBypassHTH_[0] = ps.getParameter<double >("trigPrimBypassHTH");
00187   //sr.trigPrimBypassWithPeakFinder_.resize(1);
00188   //sr.trigPrimBypassWithPeakFinder_[0] = ps.getParameter<bool >("trigPrimBypassWithPeakFinder");
00189   //sr.defaultTtf_.resize(1);
00190   //sr.defaultTtf_[0] = ps.getParameter<int >("defaultTtf");
00191   sr.actions_ = ps.getParameter<std::vector<int> >("actions");
00192 }
00193 
00194 void EcalSRCondTools::importSrpConfigFile(EcalSRSettings& sr, std::istream& f, bool d){
00195   //initialize vectors:
00196   sr.deltaEta_ = vector<int>(1,0);
00197   sr.deltaPhi_ = vector<int>(1,0);
00198   sr.actions_ = vector<int>(4, 0);
00199   sr.tccMasksFromConfig_ = vector<short>(EcalSRSettings::nTccs_, 0);
00200   sr.srpMasksFromConfig_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(8, 0));
00201   sr.dccMasks_ = vector<short>(EcalSRSettings::nDccs_);
00202   sr.srfMasks_ = vector<short>(EcalSRSettings::nDccs_);
00203   sr.substitutionSrfs_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(68,0));
00204   sr.testerTccEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00205   sr.testerSrpEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00206   sr.testerDccTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00207   sr.testerSrpTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00208   sr.bxOffsets_ = vector<short>(EcalSRSettings::nSrps_, 0);
00209   sr.automaticMasks_ = 0;
00210   sr.automaticSrpSelect_ = 0;
00211   
00212   //string line;
00213   int iLine = 0;
00214   int iValueSet = -1;
00215   const int nValueSets = 6*EcalSRSettings::nSrps_+9;
00216   string line;
00217   stringstream sErr("");
00218   while(!f.eof() && sErr.str().empty()){
00219     getline(f, line);
00220     ++iLine;
00221     line = trim(line);
00222     if(line[0] == '#' || line.empty()){//comment line and empty line to ignore
00223       continue;
00224     } else{
00225       ++iValueSet;
00226     }
00227     if(iValueSet>=nValueSets) break;
00228     uint32_t value;
00229     string sValue;
00230     int pos = 0;
00231     int iCh = 0;
00232     int nChs[nValueSets] = {
00233       //TCC masks: 0-11
00234       12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00235       //SRP masks: 12-23
00236       8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
00237       //DCC masks: 24-35
00238       12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00239       //SRF Masks: 36-47
00240       6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,  6,
00241       //substitution SRFs: 48-59
00242       68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68,
00243       //Tester card to emulate or test: 60-71
00244       4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
00245       //Bx offsets: 72
00246       12,
00247       //algo type: 73
00248       1,
00249       //action flags: 74
00250       4,
00251       //pattern file directory: 75
00252       1,
00253       //VME slots: 76
00254       12,
00255       //card types: 77
00256       12,
00257       //config Mode
00258       1,
00259       //VME Interface card
00260       1,
00261       //Spy Mode
00262       12,
00263     };
00264 
00265     while(((sValue = tokenize(line, " \t", pos))!=string(""))
00266           && (iCh<nChs[iValueSet]) && sErr.str().empty()){
00267       value = strtoul(sValue.c_str(), 0, 0);
00268       const int iSrp = iValueSet%EcalSRSettings::nSrps_;
00269       if(iValueSet<12){//TCC
00270         assert((unsigned)iSrp < sizeof(tccNum) / sizeof(tccNum[0]));
00271         assert((unsigned)iCh < sizeof(tccNum[0]) / sizeof(tccNum[0][0]));
00272         int tcc = tccNum[iSrp][iCh];
00273         if(tcc>=0) {
00274           if(d) cout << "tccMasksFromConfig_[" << tcc << "] <- "
00275                      << value << "\n";
00276           sr.tccMasksFromConfig_[tcc-1] = value;
00277         }
00278       } else if(iValueSet<24){//SRP-SRP
00279         if(d) cout << "srpMasks_[" << iSrp << "][" << iCh << "] <- "
00280                    << value << "\n";
00281         sr.srpMasksFromConfig_[iSrp][iCh] = value;
00282       } else if(iValueSet<36) {//DCC output
00283         assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
00284         assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
00285         int dcc = dccNum[iSrp][iCh];
00286         if(dcc > 0){
00287           assert((unsigned)(dcc-1) < sr.dccMasks_.size());
00288           if(d) cout << "dccMasks_[" << (dcc-1) << "] <- "
00289                      << value << "\n";
00290           sr.dccMasks_[dcc-1] = value;
00291         }
00292       } else if(iValueSet<48){//SRF masks
00293         assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
00294         assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
00295         int dcc = dccNum[iSrp][iCh];
00296         if(dcc > 0){
00297           if(d) cout << "srfMasks_[" << (dcc-1) << "] <- "
00298                      << value << "\n";
00299           assert((unsigned)(dcc-1) < sr.srfMasks_.size());
00300           sr.srfMasks_[dcc-1] = value;
00301         }
00302       } else if(iValueSet<60){//substiution SRFs
00303         assert((unsigned)iSrp < sr.substitutionSrfs_.size());
00304         assert((unsigned)iCh <  sr.substitutionSrfs_[0].size());
00305         if(d) cout << "substitutionMasks_[" << iSrp << "][" << iCh << "] <- "
00306                    << value << "\n";
00307         sr.substitutionSrfs_[iSrp][iCh] = value;
00308       } else if(iValueSet<72){//Tester card config
00309         switch(iCh){
00310         case 0:
00311           assert((unsigned)iSrp < sr.testerTccEmuSrpIds_.size());
00312           if(d) cout << "testerTccEmuSrpIds_[" << iSrp << "] <- "
00313                      << value << "\n";
00314           sr.testerTccEmuSrpIds_[iSrp] = value;
00315           break;
00316         case 1:
00317           assert((unsigned)iSrp < sr.testerSrpEmuSrpIds_.size());
00318           if(d) cout << "testerSrpEmuSrpIds_[" << iSrp << "] <- "
00319                      << value << "\n";
00320           sr.testerSrpEmuSrpIds_[iSrp] = value;
00321           break;
00322         case 2:
00323           assert((unsigned)iSrp < sr.testerDccTestSrpIds_.size());
00324           if(d) cout << "testerDccTestSrpIds_[" << iSrp << "] <- "
00325                      << value << "\n";
00326           sr.testerDccTestSrpIds_[iSrp] = value;
00327           break;
00328         case 3:
00329           assert((unsigned)iSrp < sr.testerSrpTestSrpIds_.size());
00330           if(d) cout << "testerSrpTestSrpIds_[" << iSrp << "] <- "
00331                      << value << "\n";
00332           sr.testerSrpTestSrpIds_[iSrp] = value;
00333           break;
00334         default:
00335           sErr << "Syntax error in SRP system configuration "
00336                << " line " << iLine << ".";
00337         }
00338       } else if(iValueSet<73){//bx offsets
00339         assert((unsigned)iCh < sr.bxOffsets_.size());
00340         if(d) cout << "bxOffset_[" << iCh << "] <- "
00341                    << value << "\n";
00342         sr.bxOffsets_[iCh] = value;
00343       } else if(iValueSet<74){//algo type
00344         int algo = value;
00345         switch(algo){
00346         case 0:
00347           sr.deltaEta_[0] = sr.deltaPhi_[0] = 1;
00348           break;
00349         case 1:
00350           sr.deltaEta_[0] = sr.deltaPhi_[0]  = 2;
00351           break;
00352         default:
00353           throw cms::Exception("OutOfRange") << "Value of parameter algo ," << algo
00354                                              << ", is invalid. Valid values are 0 and 1.";
00355         }
00356         if(d) cout << "deltaEta_[0] <- " << sr.deltaEta_[0] << "\t"
00357                    << "deltaPhi_[0] <- " << sr.deltaPhi_[0] << "\n";
00358       } else if(iValueSet<75){//action flags
00359         assert((unsigned)iCh < sr.actions_.size());
00360         if(d) cout << "actions_[" << iCh << "] <- "
00361                    << value << "\n";
00362         sr.actions_[iCh] = value;
00363       } else if(iValueSet<76){//pattern file directory
00364 //      emuDir_ = sValue;
00365 //      if(d) cout << "emuDir_ <= "
00366 //                 << value << "\n";
00367       } else if(iValueSet<77){//VME slots
00368         //      slotIds_[iCh] = value;
00369   //    if(d) cout << "slotIds_[" << iCh << "] <= "
00370         //                 << value << "\n";
00371       } else if(iValueSet<78){//card types
00372         //      cardTypes_[iCh] = sValue[0];
00373         //      if(d) cout << "cardTypes_[" << iCh << "] <= "
00374         //                 << value << "\n";
00375       } else if (iValueSet<79){//config mode
00376         //TODO validity check on value
00377         //      configMode_ = (ConfigMode)value;
00378         //      if(d) cout << "config mode <= " << value << "\n";
00379       } else if (iValueSet<80){//VME I/F
00380         //TODO validity check on value
00381         //     vmeInterface_ = (Vme::type_t)value;
00382         //if(d) cout << "Vme Interface code <= " << value << "\n";
00383       } else if (iValueSet<81){//Spy Mode
00384         //TODO validity check on value
00385         //      spyMode_[iCh] = value & 0x3;
00386         //      if(d) cout << "Spy mode <= " << value << "\n";
00387       } else{//should never be reached!
00388         assert(false);
00389       }
00390       ++iCh;
00391     }
00392     if(iCh!=nChs[iValueSet]){//error
00393       sErr << "Syntax error in imported SRP system configuration file "
00394         /*<< filename <<*/ " line " << iLine << ".";
00395     }
00396   }
00397   if(sErr.str().empty() && iValueSet!=(nValueSets-1)){//error
00398     sErr << "Syntax Error in imported SRP system configuration file "
00399       /*<< filename <<*/ " line " << iLine << ".";
00400   }
00401   if(sErr.str().size()!=0) throw cms::Exception("SyntaxError") << sErr.str();
00402 }
00403 
00404 double EcalSRCondTools::normalizeWeights(int hwWeight){
00405   //Fix sign bit in case only the 12 least significant bits of hwWeight were set
00406   //(hardware reprensentation uses only 12 bits)
00407   if(hwWeight & (1<<11)) hwWeight |= ~0xEFF;
00408   return hwWeight/1024.;
00409 }
00410 
00411 string EcalSRCondTools::tokenize(const string& s, const string& delim, int& pos){
00412   if(pos<0) return "";
00413   int pos0 = pos;
00414   int len = s.size();
00415   //eats delimeters at beginning of the string
00416   while(pos0<len && find(delim.begin(), delim.end(), s[pos0])!=delim.end()){
00417     ++pos0;
00418   }
00419   if(pos0==len) return "";
00420   pos = s.find_first_of(delim, pos0);
00421   return s.substr(pos0, (pos>0?pos:len)-pos0);
00422 }
00423 
00424 std::string EcalSRCondTools::trim(std::string s){
00425   std::string::size_type pos0 = s.find_first_not_of(" \t");
00426   if(pos0==string::npos){
00427     pos0=0;
00428   }
00429   string::size_type pos1 = s.find_last_not_of(" \t") + 1;
00430   if(pos1==string::npos){
00431     pos1 = pos0;
00432   }
00433   return s.substr(pos0, pos1-pos0);
00434 }