00001
00002
00003
00004
00005
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
00019 #include <string>
00020 #include <fstream>
00021 #include <algorithm>
00022
00023 static int tccNum[12][12] = {
00024
00025 { 36, 19, 20, 21, 22, 23, 18, 1, 2, 3, 4, 5},
00026 { 24, 25, 26, 27, 28, 29, 6, 7, 8, 9, 10, 11},
00027 { 30, 31, 32, 33, 34, 35, 12, 13, 14, 15, 16, 17},
00028
00029 { 54, 37, 38, 39, 40, 41, -1, -1, -1, -1, -1, -1},
00030 { 42, 43, 44, 45, 46, 47, -1, -1, -1, -1, -1, -1},
00031 { 48, 49, 50, 51, 52, 53, -1, -1, -1, -1, -1, -1},
00032
00033 { 72, 55, 56, 57, 58, 59, -1, -1, -1, -1, -1, -1},
00034 { 60, 61, 62, 63, 64, 65, -1, -1, -1, -1, -1, -1},
00035 { 66, 67, 68, 69, 70, 71, -1, -1, -1, -1, -1, -1},
00036
00037 { 90, 73, 74, 75, 76, 77,108, 91, 92, 93, 94, 95},
00038 { 78, 79, 80, 81, 82, 83, 96, 97, 98, 99,100,101},
00039 { 84, 85, 86, 87, 88, 89,102,103,104,105,106,107}
00040 };
00041
00042 static int dccNum[12][12] = {
00043 { 1, 2, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00044 { 4, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00045 { 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00046 { 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1},
00047 { 16, 17, 18, 19, 20, 21, -1, -1, -1, -1, -1, -1},
00048 { 22, 23, 24, 25, 26, 27, -1, -1, -1, -1, -1, -1},
00049 { 28, 29, 30, 31, 32, 33, -1, -1, -1, -1, -1, -1},
00050 { 34, 35, 36, 37, 38, 39, -1, -1, -1, -1, -1, -1},
00051 { 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, -1, -1},
00052 { 46, 47, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00053 { 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1},
00054 { 52, 53, 54, -1, -1, -1, -1, -1, -1, -1, -1, -1}
00055 };
00056
00057
00058 using namespace std;
00059
00060 EcalSRCondTools::EcalSRCondTools(const edm::ParameterSet& ps):ps_(ps){
00061 }
00062
00063
00064 EcalSRCondTools::~EcalSRCondTools(){
00065 }
00066
00067 void
00068 EcalSRCondTools::analyze(const edm::Event& event, const edm::EventSetup& es){
00069 static bool done = false;
00070 if(done) return;
00071 EcalSRSettings* sr = new EcalSRSettings;
00072
00073 string mode = ps_.getParameter<string>("mode");
00074 bool iomode;
00075 const bool iomode_read = false;
00076 const bool iomode_write = true;
00077
00078 if(mode == "online_config" || mode == "combine_config"){
00079 iomode =iomode_write;
00080 string fname = ps_.getParameter<string>("onlineSrpConfigFile");
00081 ifstream f(fname.c_str());
00082 if(!f.good()){
00083 throw cms::Exception("EcalSRCondTools") << "Failed to open file " << fname;
00084 }
00085 importSrpConfigFile(*sr, f, true);
00086 }
00087
00088 if(mode=="python_config" || mode=="combine_config"){
00089 iomode = iomode_write;
00090 importParameterSet(*sr, ps_);
00091 }
00092
00093 if(mode=="read"){
00094 iomode = iomode_read;
00095 }
00096
00097 if(!(mode=="python_config" || mode == "online_config" || mode == "combine_config" || (mode=="read"))){
00098 throw cms::Exception("Config") << "Invalid value," << mode << ", for parameter mode. "
00099 << "Valid values: online_config, python_config, combine_config, read";
00100 }
00101
00102 if(iomode==iomode_write){
00103 sr->bxGlobalOffset_ = ps_.getParameter<int>("bxGlobalOffset");
00104 sr->automaticSrpSelect_ = ps_.getParameter<int>("automaticSrpSelect");
00105 sr->automaticMasks_ = ps_.getParameter<int>("automaticMasks");
00106
00107 edm::Service<cond::service::PoolDBOutputService> db;
00108 if( !db.isAvailable() ){
00109 throw cms::Exception("CondDBAccess") << "Failed to connect to PoolDBOutputService\n";
00110 }
00111
00112
00113 cond::Time_t firstSinceTime = db->beginOfTime();
00114 db->writeOne(sr,firstSinceTime,"EcalSRSettingsRcd");
00115 done = true;
00116 } else {
00117 edm::ESHandle<EcalSRSettings> hSr;
00118 es.get<EcalSRSettingsRcd>().get(hSr);
00119 const EcalSRSettings* sr = hSr.product();
00120 cout << "ECAL Seletive readout settings:\n";
00121 cout << *sr << "\n" << endl;
00122 }
00123 }
00124
00125 void EcalSRCondTools::importParameterSet(EcalSRSettings& sr, const edm::ParameterSet& ps){
00126 sr.deltaPhi_.resize(1);
00127 sr.deltaPhi_[0] = ps.getParameter<int >("deltaPhi");
00128 sr.deltaEta_.resize(1);
00129 sr.deltaEta_[0] = ps.getParameter<int >("deltaEta");
00130 sr.ecalDccZs1stSample_.resize(1);
00131 sr.ecalDccZs1stSample_[0] = ps.getParameter<int >("ecalDccZs1stSample");
00132 sr.ebDccAdcToGeV_ = ps.getParameter<double >("ebDccAdcToGeV");
00133 sr.eeDccAdcToGeV_ = ps.getParameter<double >("eeDccAdcToGeV");
00134 sr.dccNormalizedWeights_.resize(1);
00135 const std::vector<double>& w = ps.getParameter<std::vector<double> >("dccNormalizedWeights");
00136 sr.dccNormalizedWeights_[0].resize(w.size());
00137 std::copy(w.begin(), w.end(), sr.dccNormalizedWeights_[0].begin());
00138 sr.symetricZS_.resize(1);
00139 sr.symetricZS_[0] = ps.getParameter<bool >("symetricZS");
00140 sr.srpLowInterestChannelZS_.resize(2);
00141 const int eb = 0;
00142 const int ee = 1;
00143 sr.srpLowInterestChannelZS_[eb] = ps.getParameter<double >("srpBarrelLowInterestChannelZS");
00144 sr.srpLowInterestChannelZS_[ee] = ps.getParameter<double >("srpEndcapLowInterestChannelZS");
00145 sr.srpHighInterestChannelZS_.resize(2);
00146 sr.srpHighInterestChannelZS_[eb] = ps.getParameter<double >("srpBarrelHighInterestChannelZS");
00147 sr.srpHighInterestChannelZS_[ee] = ps.getParameter<double >("srpEndcapHighInterestChannelZS");
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160 sr.actions_ = ps.getParameter<std::vector<int> >("actions");
00161 }
00162
00163 void EcalSRCondTools::importSrpConfigFile(EcalSRSettings& sr, std::istream& f, bool d){
00164
00165 sr.deltaEta_ = vector<int>(1,0);
00166 sr.deltaPhi_ = vector<int>(1,0);
00167 sr.actions_ = vector<int>(4, 0);
00168 sr.tccMasksFromConfig_ = vector<short>(EcalSRSettings::nTccs_, 0);
00169 sr.srpMasksFromConfig_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(8, 0));
00170 sr.dccMasks_ = vector<short>(EcalSRSettings::nDccs_);
00171 sr.srfMasks_ = vector<short>(EcalSRSettings::nDccs_);
00172 sr.substitutionSrfs_ = vector<vector<short> >(EcalSRSettings::nSrps_, vector<short>(68,0));
00173 sr.testerTccEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00174 sr.testerSrpEmuSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00175 sr.testerDccTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00176 sr.testerSrpTestSrpIds_ = vector<int>(EcalSRSettings::nSrps_, 0);
00177 sr.bxOffsets_ = vector<short>(EcalSRSettings::nSrps_, 0);
00178 sr.automaticMasks_ = 0;
00179 sr.automaticSrpSelect_ = 0;
00180
00181
00182 int iLine = 0;
00183 int iValueSet = -1;
00184 const int nValueSets = 6*EcalSRSettings::nSrps_+9;
00185 string line;
00186 stringstream sErr("");
00187 while(!f.eof() && sErr.str().empty()){
00188 getline(f, line);
00189 ++iLine;
00190 line = trim(line);
00191 if(line[0] == '#' || line.empty()){
00192 continue;
00193 } else{
00194 ++iValueSet;
00195 }
00196 if(iValueSet>=nValueSets) break;
00197 uint32_t value;
00198 string sValue;
00199 int pos = 0;
00200 int iCh = 0;
00201 int nChs[nValueSets] = {
00202
00203 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00204
00205 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
00206
00207 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
00208
00209 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
00210
00211 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68, 68,
00212
00213 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
00214
00215 12,
00216
00217 1,
00218
00219 4,
00220
00221 1,
00222
00223 12,
00224
00225 12,
00226
00227 1,
00228
00229 1,
00230
00231 12,
00232 };
00233
00234 while(((sValue = tokenize(line, " \t", pos))!=string(""))
00235 && (iCh<nChs[iValueSet]) && sErr.str().empty()){
00236 value = strtoul(sValue.c_str(), 0, 0);
00237 const int iSrp = iValueSet%EcalSRSettings::nSrps_;
00238 if(iValueSet<12){
00239 assert((unsigned)iSrp < sizeof(tccNum) / sizeof(tccNum[0]));
00240 assert((unsigned)iCh < sizeof(tccNum[0]) / sizeof(tccNum[0][0]));
00241 int tcc = tccNum[iSrp][iCh];
00242 if(tcc>=0) {
00243 if(d) cout << "tccMasksFromConfig_[" << tcc << "] <- "
00244 << value << "\n";
00245 sr.tccMasksFromConfig_[tcc-1] = value;
00246 }
00247 } else if(iValueSet<24){
00248 if(d) cout << "srpMasks_[" << iSrp << "][" << iCh << "] <- "
00249 << value << "\n";
00250 sr.srpMasksFromConfig_[iSrp][iCh] = value;
00251 } else if(iValueSet<36) {
00252 assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
00253 assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
00254 int dcc = dccNum[iSrp][iCh];
00255 if(dcc > 0){
00256 assert((unsigned)(dcc-1) < sr.dccMasks_.size());
00257 if(d) cout << "dccMasks_[" << (dcc-1) << "] <- "
00258 << value << "\n";
00259 sr.dccMasks_[dcc-1] = value;
00260 }
00261 } else if(iValueSet<48){
00262 assert((unsigned)iSrp < sizeof(dccNum) / sizeof(dccNum[0]));
00263 assert((unsigned)iCh < sizeof(dccNum[0]) / sizeof(dccNum[0][0]));
00264 int dcc = dccNum[iSrp][iCh];
00265 if(dcc > 0){
00266 if(d) cout << "srfMasks_[" << (dcc-1) << "] <- "
00267 << value << "\n";
00268 assert((unsigned)(dcc-1) < sr.srfMasks_.size());
00269 sr.srfMasks_[dcc-1] = value;
00270 }
00271 } else if(iValueSet<60){
00272 assert((unsigned)iSrp < sr.substitutionSrfs_.size());
00273 assert((unsigned)iCh < sr.substitutionSrfs_[0].size());
00274 if(d) cout << "substitutionMasks_[" << iSrp << "][" << iCh << "] <- "
00275 << value << "\n";
00276 sr.substitutionSrfs_[iSrp][iCh] = value;
00277 } else if(iValueSet<72){
00278 switch(iCh){
00279 case 0:
00280 assert((unsigned)iSrp < sr.testerTccEmuSrpIds_.size());
00281 if(d) cout << "testerTccEmuSrpIds_[" << iSrp << "] <- "
00282 << value << "\n";
00283 sr.testerTccEmuSrpIds_[iSrp] = value;
00284 break;
00285 case 1:
00286 assert((unsigned)iSrp < sr.testerSrpEmuSrpIds_.size());
00287 if(d) cout << "testerSrpEmuSrpIds_[" << iSrp << "] <- "
00288 << value << "\n";
00289 sr.testerSrpEmuSrpIds_[iSrp] = value;
00290 break;
00291 case 2:
00292 assert((unsigned)iSrp < sr.testerDccTestSrpIds_.size());
00293 if(d) cout << "testerDccTestSrpIds_[" << iSrp << "] <- "
00294 << value << "\n";
00295 sr.testerDccTestSrpIds_[iSrp] = value;
00296 break;
00297 case 3:
00298 assert((unsigned)iSrp < sr.testerSrpTestSrpIds_.size());
00299 if(d) cout << "testerSrpTestSrpIds_[" << iSrp << "] <- "
00300 << value << "\n";
00301 sr.testerSrpTestSrpIds_[iSrp] = value;
00302 break;
00303 default:
00304 sErr << "Syntax error in SRP system configuration "
00305 << " line " << iLine << ".";
00306 }
00307 } else if(iValueSet<73){
00308 assert((unsigned)iCh < sr.bxOffsets_.size());
00309 if(d) cout << "bxOffset_[" << iCh << "] <- "
00310 << value << "\n";
00311 sr.bxOffsets_[iCh] = value;
00312 } else if(iValueSet<74){
00313 int algo = value;
00314 switch(algo){
00315 case 0:
00316 sr.deltaEta_[0] = sr.deltaPhi_[0] = 1;
00317 break;
00318 case 1:
00319 sr.deltaEta_[0] = sr.deltaPhi_[0] = 2;
00320 break;
00321 default:
00322 throw cms::Exception("OutOfRange") << "Value of parameter algo ," << algo
00323 << ", is invalid. Valid values are 0 and 1.";
00324 }
00325 if(d) cout << "deltaEta_[0] <- " << sr.deltaEta_[0] << "\t"
00326 << "deltaPhi_[0] <- " << sr.deltaPhi_[0] << "\n";
00327 } else if(iValueSet<75){
00328 assert((unsigned)iCh < sr.actions_.size());
00329 if(d) cout << "actions_[" << iCh << "] <- "
00330 << value << "\n";
00331 sr.actions_[iCh] = value;
00332 } else if(iValueSet<76){
00333
00334
00335
00336 } else if(iValueSet<77){
00337
00338
00339
00340 } else if(iValueSet<78){
00341
00342
00343
00344 } else if (iValueSet<79){
00345
00346
00347
00348 } else if (iValueSet<80){
00349
00350
00351
00352 } else if (iValueSet<81){
00353
00354
00355
00356 } else{
00357 assert(false);
00358 }
00359 ++iCh;
00360 }
00361 if(iCh!=nChs[iValueSet]){
00362 sErr << "Syntax error in imported SRP system configuration file "
00363 " line " << iLine << ".";
00364 }
00365 }
00366 if(sErr.str().empty() && iValueSet!=(nValueSets-1)){
00367 sErr << "Syntax Error in imported SRP system configuration file "
00368 " line " << iLine << ".";
00369 }
00370 if(sErr.str().size()!=0) throw cms::Exception("SyntaxError") << sErr.str();
00371 }
00372
00373 double EcalSRCondTools::normalizeWeights(int hwWeight){
00374
00375
00376 if(hwWeight & (1<<11)) hwWeight |= ~0xEFF;
00377 return hwWeight/1024.;
00378 }
00379
00380 string EcalSRCondTools::tokenize(const string& s, const string& delim, int& pos){
00381 if(pos<0) return "";
00382 int pos0 = pos;
00383 int len = s.size();
00384
00385 while(pos0<len && find(delim.begin(), delim.end(), s[pos0])!=delim.end()){
00386 ++pos0;
00387 }
00388 if(pos0==len) return "";
00389 pos = s.find_first_of(delim, pos0);
00390 return s.substr(pos0, (pos>0?pos:len)-pos0);
00391 }
00392
00393 std::string EcalSRCondTools::trim(std::string s){
00394 std::string::size_type pos0 = s.find_first_not_of(" \t");
00395 if(pos0==string::npos){
00396 pos0=0;
00397 }
00398 string::size_type pos1 = s.find_last_not_of(" \t") + 1;
00399 if(pos1==string::npos){
00400 pos1 = pos0;
00401 }
00402 return s.substr(pos0, pos1-pos0);
00403 }