Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-18 00:40:18

0001 //emacs settings:-*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil -*-
0002 /*
0003  *
0004  * author: Ph Gras. June, 2010
0005  */
0006 
0007 #include "SimCalorimetry/EcalSelectiveReadoutProducers/interface/EcalSRCondTools.h"
0008 
0009 #include "FWCore/Framework/interface/ESHandle.h"
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/EventSetup.h"
0012 #include "FWCore/ServiceRegistry/interface/Service.h"
0013 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0014 
0015 #include <fstream>
0016 #include <iostream>
0017 #include <algorithm>
0018 
0019 constexpr int tccNum[12][12] = {
0020     /* EE- */
0021     {36, 19, 20, 21, 22, 23, 18, 1, 2, 3, 4, 5},       //SRP 1
0022     {24, 25, 26, 27, 28, 29, 6, 7, 8, 9, 10, 11},      //SRP 2
0023     {30, 31, 32, 33, 34, 35, 12, 13, 14, 15, 16, 17},  //SRP 3
0024     /* EB- */
0025     {54, 37, 38, 39, 40, 41, -1, -1, -1, -1, -1, -1},  //SRP 4
0026     {42, 43, 44, 45, 46, 47, -1, -1, -1, -1, -1, -1},  //SRP 5
0027     {48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1, -1},  //SRP 6
0028     /* EB+ */
0029     {72, 55, 56, 57, 58, 59, -1, -1, -1, -1, -1, -1},  //SRP 7
0030     {60, 61, 62, 63, 64, 65, -1, -1, -1, -1, -1, -1},  //SRP 8
0031     {66, 67, 68, 69, 70, 71, -1, -1, -1, -1, -1, -1},  //SRP 9
0032     /* EE+ */
0033     {90, 73, 74, 75, 76, 77, 108, 91, 92, 93, 94, 95},      //SRP 10
0034     {78, 79, 80, 81, 82, 83, 96, 97, 98, 99, 100, 101},     //SRP 11
0035     {84, 85, 86, 87, 88, 89, 102, 103, 104, 105, 106, 107}  //SRP 12
0036 };
0037 
0038 constexpr int dccNum[12][12] = {
0039     {1, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1},     //SRP 1
0040     {4, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1},     //SRP 2
0041     {7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1},     //SRP 3
0042     {10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1},  //SRP 4
0043     {16, 17, 18, 19, 20, 21, -1, -1, -1, -1, -1, -1},  //SRP 5
0044     {22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1},  //SRP 6
0045     {28, 29, 30, 31, 32, 33, -1, -1, -1, -1, -1, -1},  //SRP 7
0046     {34, 35, 36, 37, 38, 39, -1, -1, -1, -1, -1, -1},  //SRP 8
0047     {40, 41, 42, 43, 44, 45, -1, -1, -1, -1, -1, -1},  //SRP 9
0048     {46, 47, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 10
0049     {49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1},  //SRP 11
0050     {52, 53, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1}   //SRP 12
0051 };
0052 
0053 using namespace std;
0054 
0055 EcalSRCondTools::EcalSRCondTools(const edm::ParameterSet& ps)
0056     : ps_(ps), mode_(ps.getParameter<string>("mode")), iomode_write_(true), done_(false) {
0057   if (mode_ == "read") {
0058     iomode_write_ = false;
0059     hSrToken_ = esConsumes();
0060     tpgPhysicsConstToken_ = esConsumes();
0061   }
0062 }
0063 
0064 EcalSRCondTools::~EcalSRCondTools() {}
0065 
0066 void EcalSRCondTools::analyze(const edm::Event& event, const edm::EventSetup& es) {
0067   if (done_)
0068     return;
0069   EcalSRSettings sr;
0070 
0071   if (mode_ == "online_config" || mode_ == "combine_config") {
0072     string fname = ps_.getParameter<string>("onlineSrpConfigFile");
0073     ifstream f(fname.c_str());
0074     if (!f.good()) {
0075       throw cms::Exception("EcalSRCondTools") << "Failed to open file " << fname;
0076     }
0077     importSrpConfigFile(sr, f, true);
0078   }
0079 
0080   if (mode_ == "python_config" || mode_ == "combine_config") {
0081     importParameterSet(sr, ps_);
0082   }
0083 
0084   if (!(mode_ == "python_config" || mode_ == "online_config" || mode_ == "combine_config" || (mode_ == "read"))) {
0085     throw cms::Exception("Config") << "Invalid value," << mode_ << ",  for parameter mode. "
0086                                    << "Valid values: online_config, python_config, combine_config, read";
0087   }
0088 
0089   if (iomode_write_) {
0090     sr.bxGlobalOffset_ = ps_.getParameter<int>("bxGlobalOffset");
0091     sr.automaticSrpSelect_ = ps_.getParameter<int>("automaticSrpSelect");
0092     sr.automaticMasks_ = ps_.getParameter<int>("automaticMasks");
0093 
0094     edm::Service<cond::service::PoolDBOutputService> db;
0095     if (!db.isAvailable()) {
0096       throw cms::Exception("CondDBAccess") << "Failed to connect to PoolDBOutputService\n";
0097     }
0098     //fillup DB
0099     //create new infinite IOV
0100     cond::Time_t firstSinceTime = db->beginOfTime();
0101     db->writeOneIOV(sr, firstSinceTime, "EcalSRSettingsRcd");
0102     done_ = true;
0103   } else {  //read mode
0104     const edm::ESHandle<EcalSRSettings> hSr = es.getHandle(hSrToken_);
0105     if (!hSr.isValid()) {
0106       cout << "EcalSRSettings record not found. Check the Cond DB Global tag.\n";
0107     } else {
0108       const EcalSRSettings* ssr = hSr.product();
0109       cout << "ECAL Seletive readout settings:\n";
0110       cout << *ssr << "\n" << endl;
0111     }
0112 
0113     //trigger tower thresholds (from FENIX configuration):
0114     const edm::ESHandle<EcalTPGPhysicsConst> hTp = es.getHandle(tpgPhysicsConstToken_);
0115     if (!hTp.isValid()) {
0116       cout << "EcalTPGPhysicsConst record not found. Check the Cond DB Global tag.\n";
0117     } else {
0118       const EcalTPGPhysicsConst* tp = hTp.product();
0119       const EcalTPGPhysicsConstMap& mymap = tp->getMap();
0120       if (mymap.size() != 2) {
0121         cout << "Error: TPG physics record is of unexpected size: " << mymap.size()
0122              << " elements instead of two (one for EB, one for EE)\n";
0123       } else {
0124         EcalTPGPhysicsConstMap::const_iterator it = mymap.begin();
0125         cout << "----------------------------------------------------------------------\n"
0126                 "Trigger tower Et thresholds extracted from TPG configuration \n"
0127                 "(EcalSRCondTools modules supports only read mode for these parameters):\n\n";
0128         cout << "EB: "
0129              << "LT = " << it->second.ttf_threshold_Low << " GeV "
0130              << "HT = " << it->second.ttf_threshold_High << " GeV\n";
0131         ++it;
0132         cout << "EE: "
0133              << "LT = " << it->second.ttf_threshold_Low << " GeV "
0134              << "HT = " << it->second.ttf_threshold_High << " GeV\n";
0135       }
0136     }
0137   }
0138 }
0139 
0140 void EcalSRCondTools::importParameterSet(EcalSRSettings& sr, const edm::ParameterSet& ps) {
0141   sr.deltaPhi_.resize(1);
0142   sr.deltaPhi_[0] = ps.getParameter<int>("deltaPhi");
0143   sr.deltaEta_.resize(1);
0144   sr.deltaEta_[0] = ps.getParameter<int>("deltaEta");
0145   sr.ecalDccZs1stSample_.resize(1);
0146   sr.ecalDccZs1stSample_[0] = ps.getParameter<int>("ecalDccZs1stSample");
0147   sr.ebDccAdcToGeV_ = ps.getParameter<double>("ebDccAdcToGeV");
0148   sr.eeDccAdcToGeV_ = ps.getParameter<double>("eeDccAdcToGeV");
0149   sr.dccNormalizedWeights_.resize(1);
0150   const std::vector<double>& w = ps.getParameter<std::vector<double> >("dccNormalizedWeights");
0151   sr.dccNormalizedWeights_[0].resize(w.size());
0152   std::copy(w.begin(), w.end(), sr.dccNormalizedWeights_[0].begin());
0153   sr.symetricZS_.resize(1);
0154   sr.symetricZS_[0] = ps.getParameter<bool>("symetricZS");
0155   sr.srpLowInterestChannelZS_.resize(2);
0156   const int eb = 0;
0157   const int ee = 1;
0158   sr.srpLowInterestChannelZS_[eb] = ps.getParameter<double>("srpBarrelLowInterestChannelZS");
0159   sr.srpLowInterestChannelZS_[ee] = ps.getParameter<double>("srpEndcapLowInterestChannelZS");
0160   sr.srpHighInterestChannelZS_.resize(2);
0161   sr.srpHighInterestChannelZS_[eb] = ps.getParameter<double>("srpBarrelHighInterestChannelZS");
0162   sr.srpHighInterestChannelZS_[ee] = ps.getParameter<double>("srpEndcapHighInterestChannelZS");
0163   //sr.trigPrimBypass_.resize(1);
0164   //sr.trigPrimBypass_[0] = ps.getParameter<bool >("trigPrimBypass");
0165   //sr.trigPrimBypassMode_.resize(1);
0166   //sr.trigPrimBypassMode_[0] = ps.getParameter<int >("trigPrimBypassMode");
0167   //sr.trigPrimBypassLTH_.resize(1);
0168   //sr.trigPrimBypassLTH_[0] = ps.getParameter<double >("trigPrimBypassLTH");
0169   //sr.trigPrimBypassHTH_.resize(1);
0170   //sr.trigPrimBypassHTH_[0] = ps.getParameter<double >("trigPrimBypassHTH");
0171   //sr.trigPrimBypassWithPeakFinder_.resize(1);
0172   //sr.trigPrimBypassWithPeakFinder_[0] = ps.getParameter<bool >("trigPrimBypassWithPeakFinder");
0173   //sr.defaultTtf_.resize(1);
0174   //sr.defaultTtf_[0] = ps.getParameter<int >("defaultTtf");
0175   sr.actions_ = ps.getParameter<std::vector<int> >("actions");
0176 }
0177 
0178 void EcalSRCondTools::importSrpConfigFile(EcalSRSettings& sr, std::istream& f, bool d) {
0179   //initialize vectors:
0180   sr.deltaEta_ = vector<int>(1, 0);
0181   sr.deltaPhi_ = vector<int>(1, 0);
0182   sr.actions_ = vector<int>(4, 0);
0183   sr.tccMasksFromConfig_ = vector<short>(EcalSRSettings::nTccs_, 0);
0184   sr.srpMasksFromConfig_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(8, 0));
0185   sr.dccMasks_ = vector<short>(EcalSRSettings::nDccs_);
0186   sr.srfMasks_ = vector<short>(EcalSRSettings::nDccs_);
0187   sr.substitutionSrfs_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(68, 0));
0188   sr.testerTccEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
0189   sr.testerSrpEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
0190   sr.testerDccTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
0191   sr.testerSrpTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
0192   sr.bxOffsets_ = vector<short>(EcalSRSettings::nSrps_, 0);
0193   sr.automaticMasks_ = 0;
0194   sr.automaticSrpSelect_ = 0;
0195 
0196   //string line;
0197   int iLine = 0;
0198   int iValueSet = -1;
0199   const int nValueSets = 6 * EcalSRSettings::nSrps_ + 9;
0200   string line;
0201   stringstream sErr("");
0202   while (!f.eof() && sErr.str().empty()) {
0203     getline(f, line);
0204     ++iLine;
0205     line = trim(line);
0206     if (line[0] == '#' || line.empty()) {  //comment line and empty line to ignore
0207       continue;
0208     } else {
0209       ++iValueSet;
0210     }
0211     if (iValueSet >= nValueSets)
0212       break;
0213     uint32_t value;
0214     string sValue;
0215     int pos = 0;
0216     int iCh = 0;
0217     int nChs[nValueSets] = {
0218         //TCC masks: 0-11
0219         12,
0220         12,
0221         12,
0222         12,
0223         12,
0224         12,
0225         12,
0226         12,
0227         12,
0228         12,
0229         12,
0230         12,
0231         //SRP masks: 12-23
0232         8,
0233         8,
0234         8,
0235         8,
0236         8,
0237         8,
0238         8,
0239         8,
0240         8,
0241         8,
0242         8,
0243         8,
0244         //DCC masks: 24-35
0245         12,
0246         12,
0247         12,
0248         12,
0249         12,
0250         12,
0251         12,
0252         12,
0253         12,
0254         12,
0255         12,
0256         12,
0257         //SRF Masks: 36-47
0258         6,
0259         6,
0260         6,
0261         6,
0262         6,
0263         6,
0264         6,
0265         6,
0266         6,
0267         6,
0268         6,
0269         6,
0270         //substitution SRFs: 48-59
0271         68,
0272         68,
0273         68,
0274         68,
0275         68,
0276         68,
0277         68,
0278         68,
0279         68,
0280         68,
0281         68,
0282         68,
0283         //Tester card to emulate or test: 60-71
0284         4,
0285         4,
0286         4,
0287         4,
0288         4,
0289         4,
0290         4,
0291         4,
0292         4,
0293         4,
0294         4,
0295         4,
0296         //Bx offsets: 72
0297         12,
0298         //algo type: 73
0299         1,
0300         //action flags: 74
0301         4,
0302         //pattern file directory: 75
0303         1,
0304         //VME slots: 76
0305         12,
0306         //card types: 77
0307         12,
0308         //config Mode
0309         1,
0310         //VME Interface card
0311         1,
0312         //Spy Mode
0313         12,
0314     };
0315 
0316     while (((sValue = tokenize(line, " \t", pos)) != string("")) && (iCh < nChs[iValueSet]) && sErr.str().empty()) {
0317       value = strtoul(sValue.c_str(), nullptr, 0);
0318       const int iSrp = iValueSet % EcalSRSettings::nSrps_;
0319       if (iValueSet < 12) {  //TCC
0320         assert((unsigned)iSrp < sizeof(tccNum) / sizeof(tccNum[0]));
0321         assert((unsigned)iCh < sizeof(tccNum[0]) / sizeof(tccNum[0][0]));
0322         int tcc = tccNum[iSrp][iCh];
0323         if (tcc >= 0) {
0324           if (d)
0325             cout << "tccMasksFromConfig_[" << tcc << "] <- " << value << "\n";
0326           sr.tccMasksFromConfig_[tcc - 1] = value;
0327         }
0328       } else if (iValueSet < 24) {  //SRP-SRP
0329         if (d)
0330           cout << "srpMasks_[" << iSrp << "][" << iCh << "] <- " << value << "\n";
0331         sr.srpMasksFromConfig_[iSrp][iCh] = value;
0332       } else if (iValueSet < 36) {  //DCC output
0333         assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
0334         assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
0335         int dcc = dccNum[iSrp][iCh];
0336         if (dcc > 0) {
0337           assert((unsigned)(dcc - 1) < sr.dccMasks_.size());
0338           if (d)
0339             cout << "dccMasks_[" << (dcc - 1) << "] <- " << value << "\n";
0340           sr.dccMasks_[dcc - 1] = value;
0341         }
0342       } else if (iValueSet < 48) {  //SRF masks
0343         assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
0344         assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
0345         int dcc = dccNum[iSrp][iCh];
0346         if (dcc > 0) {
0347           if (d)
0348             cout << "srfMasks_[" << (dcc - 1) << "] <- " << value << "\n";
0349           assert((unsigned)(dcc - 1) < sr.srfMasks_.size());
0350           sr.srfMasks_[dcc - 1] = value;
0351         }
0352       } else if (iValueSet < 60) {  //substiution SRFs
0353         assert((unsigned)iSrp < sr.substitutionSrfs_.size());
0354         assert((unsigned)iCh < sr.substitutionSrfs_[0].size());
0355         if (d)
0356           cout << "substitutionMasks_[" << iSrp << "][" << iCh << "] <- " << value << "\n";
0357         sr.substitutionSrfs_[iSrp][iCh] = value;
0358       } else if (iValueSet < 72) {  //Tester card config
0359         switch (iCh) {
0360           case 0:
0361             assert((unsigned)iSrp < sr.testerTccEmuSrpIds_.size());
0362             if (d)
0363               cout << "testerTccEmuSrpIds_[" << iSrp << "] <- " << value << "\n";
0364             sr.testerTccEmuSrpIds_[iSrp] = value;
0365             break;
0366           case 1:
0367             assert((unsigned)iSrp < sr.testerSrpEmuSrpIds_.size());
0368             if (d)
0369               cout << "testerSrpEmuSrpIds_[" << iSrp << "] <- " << value << "\n";
0370             sr.testerSrpEmuSrpIds_[iSrp] = value;
0371             break;
0372           case 2:
0373             assert((unsigned)iSrp < sr.testerDccTestSrpIds_.size());
0374             if (d)
0375               cout << "testerDccTestSrpIds_[" << iSrp << "] <- " << value << "\n";
0376             sr.testerDccTestSrpIds_[iSrp] = value;
0377             break;
0378           case 3:
0379             assert((unsigned)iSrp < sr.testerSrpTestSrpIds_.size());
0380             if (d)
0381               cout << "testerSrpTestSrpIds_[" << iSrp << "] <- " << value << "\n";
0382             sr.testerSrpTestSrpIds_[iSrp] = value;
0383             break;
0384           default:
0385             sErr << "Syntax error in SRP system configuration "
0386                  << " line " << iLine << ".";
0387         }
0388       } else if (iValueSet < 73) {  //bx offsets
0389         assert((unsigned)iCh < sr.bxOffsets_.size());
0390         if (d)
0391           cout << "bxOffset_[" << iCh << "] <- " << value << "\n";
0392         sr.bxOffsets_[iCh] = value;
0393       } else if (iValueSet < 74) {  //algo type
0394         int algo = value;
0395         switch (algo) {
0396           case 0:
0397             sr.deltaEta_[0] = sr.deltaPhi_[0] = 1;
0398             break;
0399           case 1:
0400             sr.deltaEta_[0] = sr.deltaPhi_[0] = 2;
0401             break;
0402           default:
0403             throw cms::Exception("OutOfRange")
0404                 << "Value of parameter algo ," << algo << ", is invalid. Valid values are 0 and 1.";
0405         }
0406         if (d)
0407           cout << "deltaEta_[0] <- " << sr.deltaEta_[0] << "\t"
0408                << "deltaPhi_[0] <- " << sr.deltaPhi_[0] << "\n";
0409       } else if (iValueSet < 75) {  //action flags
0410         assert((unsigned)iCh < sr.actions_.size());
0411         if (d)
0412           cout << "actions_[" << iCh << "] <- " << value << "\n";
0413         sr.actions_[iCh] = value;
0414       } else if (iValueSet < 76) {  //pattern file directory
0415                                     //  emuDir_ = sValue;
0416                                     //  if(d) cout << "emuDir_ <= "
0417                                     //         << value << "\n";
0418       } else if (iValueSet < 77) {  //VME slots
0419         //  slotIds_[iCh] = value;
0420         //  if(d) cout << "slotIds_[" << iCh << "] <= "
0421         //         << value << "\n";
0422       } else if (iValueSet < 78) {  //card types
0423         //  cardTypes_[iCh] = sValue[0];
0424         //  if(d) cout << "cardTypes_[" << iCh << "] <= "
0425         //         << value << "\n";
0426       } else if (iValueSet < 79) {  //config mode
0427                                     //TODO validity check on value
0428         //  configMode_ = (ConfigMode)value;
0429         //  if(d) cout << "config mode <= " << value << "\n";
0430       } else if (iValueSet < 80) {  //VME I/F
0431                                     //TODO validity check on value
0432                                     //     vmeInterface_ = (Vme::type_t)value;
0433                                     //if(d) cout << "Vme Interface code <= " << value << "\n";
0434       } else if (iValueSet < 81) {  //Spy Mode
0435                                     //TODO validity check on value
0436                                     //  spyMode_[iCh] = value & 0x3;
0437                                     //  if(d) cout << "Spy mode <= " << value << "\n";
0438       } else {                      //should never be reached!
0439         assert(false);
0440       }
0441       ++iCh;
0442     }
0443     if (iCh != nChs[iValueSet]) {  //error
0444       sErr << "Syntax error in imported SRP system configuration file "
0445               /*<< filename <<*/ " line "
0446            << iLine << ".";
0447     }
0448   }
0449   if (sErr.str().empty() && iValueSet != (nValueSets - 1)) {  //error
0450     sErr << "Syntax Error in imported SRP system configuration file "
0451             /*<< filename <<*/ " line "
0452          << iLine << ".";
0453   }
0454   if (!sErr.str().empty())
0455     throw cms::Exception("SyntaxError") << sErr.str();
0456 }
0457 
0458 double EcalSRCondTools::normalizeWeights(int hwWeight) {
0459   //Fix sign bit in case only the 12 least significant bits of hwWeight were set
0460   //(hardware reprensentation uses only 12 bits)
0461   if (hwWeight & (1 << 11))
0462     hwWeight |= ~0xEFF;
0463   return hwWeight / 1024.;
0464 }
0465 
0466 string EcalSRCondTools::tokenize(const string& s, const string& delim, int& pos) {
0467   if (pos < 0)
0468     return "";
0469   int pos0 = pos;
0470   int len = s.size();
0471   //eats delimeters at beginning of the string
0472   while (pos0 < len && find(delim.begin(), delim.end(), s[pos0]) != delim.end()) {
0473     ++pos0;
0474   }
0475   if (pos0 == len)
0476     return "";
0477   pos = s.find_first_of(delim, pos0);
0478   return s.substr(pos0, (pos > 0 ? pos : len) - pos0);
0479 }
0480 
0481 std::string EcalSRCondTools::trim(std::string s) {
0482   std::string::size_type pos0 = s.find_first_not_of(" \t");
0483   if (pos0 == string::npos) {
0484     pos0 = 0;
0485   }
0486   string::size_type pos1 = s.find_last_not_of(" \t") + 1;
0487   if (pos1 == string::npos) {
0488     pos1 = pos0;
0489   }
0490   return s.substr(pos0, pos1 - pos0);
0491 }