Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:52:01

0001 #include <iostream>
0002 #include <fstream>
0003 #include <string>
0004 #include <sstream>
0005 #include <xercesc/dom/DOMNode.hpp>
0006 #include <xercesc/dom/DOM.hpp>
0007 #include <xercesc/parsers/XercesDOMParser.hpp>
0008 #include "Utilities/Xerces/interface/Xerces.h"
0009 #include <xercesc/util/XMLString.hpp>
0010 #include <xercesc/sax/SAXException.hpp>
0011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
0012 
0013 #include "CondTools/Ecal/interface/EcalSRPHandler.h"
0014 #include "CondTools/Ecal/interface/EcalCondHeader.h"
0015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
0016 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
0017 #include "CondFormats/EcalObjects/interface/EcalSRSettings.h"
0018 
0019 #include "OnlineDB/EcalCondDB/interface/RunConfigDat.h"
0020 #include "OnlineDB/EcalCondDB/interface/ODRunConfigInfo.h"
0021 #include "OnlineDB/EcalCondDB/interface/ODRunConfigSeqInfo.h"
0022 
0023 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
0024 #include "OnlineDB/EcalCondDB/interface/FEConfigBadTTInfo.h"
0025 
0026 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0027 
0028 #include "SimCalorimetry/EcalSelectiveReadoutProducers/interface/EcalSRCondTools.h"
0029 
0030 using namespace XERCES_CPP_NAMESPACE;
0031 using namespace xuti;
0032 
0033 popcon::EcalSRPHandler::EcalSRPHandler(const edm::ParameterSet& ps)
0034     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalSRPHandler")) {
0035   m_firstRun = (unsigned long)atoi(ps.getParameter<std::string>("firstRun").c_str());
0036   m_lastRun = (unsigned long)atoi(ps.getParameter<std::string>("lastRun").c_str());
0037   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0038   m_user = ps.getParameter<std::string>("OnlineDBUser");
0039   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0040   m_location = ps.getParameter<std::string>("location");
0041   m_runtype = ps.getParameter<std::string>("runtype");
0042   m_gentag = ps.getParameter<std::string>("gentag");
0043   m_i_tag = "";
0044   m_debug = ps.getParameter<bool>("debug");
0045   m_i_version = 0;
0046 }
0047 
0048 popcon::EcalSRPHandler::~EcalSRPHandler() {}
0049 
0050 void popcon::EcalSRPHandler::getNewObjects() {
0051   std::ostringstream ss;
0052   ss << "ECAL ";
0053 
0054   unsigned long long max_since = 1;
0055 
0056   // this is the last inserted run
0057   max_since = tagInfo().lastInterval.since;
0058 
0059   // this is the last object in the DB
0060   Ref srp_db = lastPayload();
0061   if (m_debug)
0062     std::cout << " max_since : " << max_since << "\n retrieved last payload " << std::endl;
0063 
0064   // we copy the last valid record to a temporary object
0065   EcalSRSettings* sref = new EcalSRSettings();
0066   sref->deltaEta_ = srp_db->deltaEta_;
0067   sref->deltaPhi_ = srp_db->deltaPhi_;
0068   sref->ecalDccZs1stSample_ = srp_db->ecalDccZs1stSample_;
0069   sref->ebDccAdcToGeV_ = srp_db->ebDccAdcToGeV_;
0070   sref->eeDccAdcToGeV_ = srp_db->eeDccAdcToGeV_;
0071   sref->dccNormalizedWeights_ = srp_db->dccNormalizedWeights_;
0072   sref->symetricZS_ = srp_db->symetricZS_;
0073   sref->srpLowInterestChannelZS_ = srp_db->srpLowInterestChannelZS_;
0074   sref->srpHighInterestChannelZS_ = srp_db->srpHighInterestChannelZS_;
0075   sref->actions_ = srp_db->actions_;
0076   sref->tccMasksFromConfig_ = srp_db->tccMasksFromConfig_;
0077   sref->srpMasksFromConfig_ = srp_db->srpMasksFromConfig_;
0078   sref->dccMasks_ = srp_db->dccMasks_;
0079   sref->srfMasks_ = srp_db->srfMasks_;
0080   sref->substitutionSrfs_ = srp_db->substitutionSrfs_;
0081   sref->testerTccEmuSrpIds_ = srp_db->testerTccEmuSrpIds_;
0082   sref->testerSrpEmuSrpIds_ = srp_db->testerSrpEmuSrpIds_;
0083   sref->testerDccTestSrpIds_ = srp_db->testerDccTestSrpIds_;
0084   sref->testerSrpTestSrpIds_ = srp_db->testerSrpTestSrpIds_;
0085   sref->bxOffsets_ = srp_db->bxOffsets_;
0086   sref->bxGlobalOffset_ = srp_db->bxGlobalOffset_;
0087   sref->automaticMasks_ = srp_db->automaticMasks_;
0088   sref->automaticSrpSelect_ = srp_db->automaticSrpSelect_;
0089 
0090   // now read the actual status from the online DB
0091 
0092   std::cout << "Retrieving DAQ status from OMDS DB ... " << std::endl;
0093   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0094   std::cout << "Connection done" << std::endl;
0095 
0096   if (!econn) {
0097     std::cout << " Problem with OMDS: connection parameters " << m_sid << "/" << m_user << std::endl;
0098     throw cms::Exception("OMDS not available");
0099   }
0100 
0101   LocationDef my_locdef;
0102   my_locdef.setLocation(m_location);
0103 
0104   RunTypeDef my_rundef;
0105   my_rundef.setRunType(m_runtype);
0106 
0107   RunTag my_runtag;
0108   my_runtag.setLocationDef(my_locdef);
0109   my_runtag.setRunTypeDef(my_rundef);
0110   my_runtag.setGeneralTag(m_gentag);
0111 
0112   // range of validity
0113   int min_run = 0;
0114   if (m_firstRun <= (unsigned long)max_since) {
0115     min_run = (int)max_since + 1;  // we have to add 1 to the last transferred one
0116   } else {
0117     min_run = (int)m_firstRun;
0118   }
0119 
0120   int max_run = (int)m_lastRun;
0121   if (m_debug)
0122     std::cout << "min_run " << min_run << " max_run " << max_run << std::endl;
0123 
0124   std::ofstream fout;
0125   if (m_debug) {
0126     char outfile[800];
0127     sprintf(outfile, "SRP_run%d.txt", min_run);
0128     fout.open(outfile, std::fstream::out);
0129     fout << " ORCOFF last run max_since : " << max_since << std::endl;
0130     PrintPayload(*sref, fout);
0131   }
0132 
0133   RunList my_list;
0134   my_list = econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
0135 
0136   std::vector<RunIOV> run_vec = my_list.getRuns();
0137   int num_runs = run_vec.size();
0138 
0139   if (m_debug) {
0140     fout << " number of runs is : " << num_runs << std::endl;
0141   }
0142   unsigned long irun = 0;
0143   if (num_runs > 0) {
0144     int fe_conf_id_old = 0;
0145     //    int krmax = std::min(num_runs, 100);
0146     //    for(int kr = 0; kr < krmax; kr++) {
0147     for (int kr = 0; kr < num_runs; kr++) {  // allow any number of runs, exit on transfert size
0148       irun = (unsigned long)run_vec[kr].getRunNumber();
0149       std::string geneTag = run_vec[kr].getRunTag().getGeneralTag();
0150       if (geneTag != "GLOBAL") {
0151         if (m_debug)
0152           fout << "\n New run " << irun << " with tag " << geneTag << " giving up " << std::endl;
0153         continue;
0154       }
0155       if (m_debug)
0156         fout << "\n New run " << irun << " geneTag " << geneTag << std::endl;
0157 
0158       // First, RUN_CONFIGURATION
0159       std::map<EcalLogicID, RunConfigDat> dataset;
0160       econn->fetchDataSet(&dataset, &run_vec[kr]);
0161       std::string myconfig_tag = "";
0162       int myconfig_version = 0;
0163       std::map<EcalLogicID, RunConfigDat>::const_iterator it;
0164       if (dataset.size() != 1) {
0165         std::cout << "\n\n run " << irun << " strange number of dataset " << dataset.size() << std::endl;
0166         if (m_debug)
0167           fout << "\n\n run " << irun << " strange number of dataset " << dataset.size() << " giving up " << std::endl;
0168         continue;
0169       }
0170 
0171       it = dataset.begin();
0172       RunConfigDat dat = it->second;
0173       myconfig_tag = dat.getConfigTag();
0174       //      if (myconfig_tag.substr(0, 15) == "ZeroSuppression") {
0175       if (myconfig_tag.substr(0, 15) == "ZeroSuppression" || myconfig_tag.substr(0, 11) == "FullReadout" ||
0176           myconfig_tag.substr(0, 11) == "AlmostEmpty") {
0177         if (m_debug)
0178           fout << " run " << irun << " tag " << myconfig_tag << " giving up " << std::endl;
0179         continue;
0180       }
0181 
0182       // Now  FE_DAQ_CONFIG
0183       typedef std::map<EcalLogicID, RunFEConfigDat>::const_iterator feConfIter;
0184       std::map<EcalLogicID, RunFEConfigDat> feconfig;
0185       econn->fetchDataSet(&feconfig, &run_vec[kr]);
0186       if (feconfig.size() != 1) {
0187         if (m_debug)
0188           fout << "\n\n run " << irun << " strange number of FE config " << feconfig.size() << " giving up "
0189                << std::endl;
0190         continue;
0191       }
0192       RunFEConfigDat rd_fe;
0193       int fe_conf_id = 0;
0194       feConfIter p = feconfig.begin();
0195       rd_fe = p->second;
0196       fe_conf_id = rd_fe.getConfigId();
0197 
0198       myconfig_version = dat.getConfigVersion();
0199       if (m_debug)
0200         fout << " run " << irun << " tag " << myconfig_tag << " version " << myconfig_version << " Fe config "
0201              << fe_conf_id << std::endl;
0202       // here we should check if it is the same as previous run.
0203       if (myconfig_tag != m_i_tag || myconfig_version != m_i_version || fe_conf_id != fe_conf_id_old) {
0204         if (m_debug)
0205           fout << " run= " << irun << " different tag  ... retrieving last config set from DB" << std::endl;
0206 
0207         bool FromCLOB = false;
0208         EcalSRSettings* sr = new EcalSRSettings;
0209         sr->ebDccAdcToGeV_ = 0.035;
0210         sr->eeDccAdcToGeV_ = 0.060;
0211         sr->symetricZS_.push_back(0);
0212 
0213         ODRunConfigInfo od_run_info;
0214         od_run_info.setTag(myconfig_tag);
0215         od_run_info.setVersion(myconfig_version);
0216 
0217         try {
0218           econn->fetchConfigSet(&od_run_info);
0219           int config_id = od_run_info.getId();
0220 
0221           ODRunConfigSeqInfo seq;
0222           seq.setEcalConfigId(config_id);
0223           seq.setSequenceNumber(0);
0224           econn->fetchConfigSet(&seq);
0225           int sequenceid = seq.getSequenceId();
0226 
0227           ODEcalCycle ecal_cycle;
0228           ecal_cycle.setSequenceId(sequenceid);
0229           econn->fetchConfigSet(&ecal_cycle);
0230           int cycle_id = ecal_cycle.getId();
0231           int srp_id = ecal_cycle.getSRPId();
0232           if (srp_id == 0) {
0233             if (m_debug)
0234               fout << " no SRP config for this run, give up " << std::endl;
0235             delete sr;
0236             continue;  //  no SRP config
0237           }
0238           int dcc_id = ecal_cycle.getDCCId();
0239           if (m_debug)
0240             fout << " cycleid " << cycle_id << " SRP id " << srp_id << " DCC id " << dcc_id << std::endl;
0241           /**************************/
0242           /*          SRP           */
0243           /**************************/
0244           ODSRPConfig srp;
0245           srp.setId(srp_id);
0246           econn->fetchConfigSet(&srp);
0247 
0248           unsigned char* cbuffer = srp.getSRPClob();
0249           unsigned int SRPClobSize = srp.getSRPClobSize();
0250           std::string srpstr((char*)cbuffer, SRPClobSize);
0251           std::string SRPClob = srpstr;
0252           std::fstream myfile;
0253           myfile.open("srp.txt", std::fstream::out);
0254           for (std::string::iterator it = SRPClob.begin(); it < SRPClob.end(); it++)
0255             myfile << *it;
0256           myfile.close();
0257           std::ifstream f("srp.txt");
0258           if (!f.good()) {
0259             throw cms::Exception("EcalSRPHandler") << " Failed to open file srp.txt";
0260             if (m_debug)
0261               fout << " Failed to open file srp.txt" << std::endl;
0262           }
0263           EcalSRCondTools::importSrpConfigFile(*sr, f, m_debug);
0264           f.close();
0265           int rv = system("rm srp.txt");
0266           if (m_debug && rv != 0)
0267             fout << "rm srp.txt result code: " << rv << "\n";
0268 
0269           sr->bxGlobalOffset_ = srp.getSRP0BunchAdjustPosition();
0270           sr->automaticMasks_ = srp.getAutomaticMasks();
0271           sr->automaticSrpSelect_ = srp.getAutomaticSrpSelect();
0272           /**************************/
0273           /*          DCC           */
0274           /**************************/
0275           ODDCCConfig dcc;
0276           dcc.setId(dcc_id);
0277           econn->fetchConfigSet(&dcc);
0278           std::string weightsMode = dcc.getDCCWeightsMode();
0279           if (m_debug)
0280             fout << " DCC weightsMode " << weightsMode << std::endl
0281                  << " weight size beg " << sr->dccNormalizedWeights_.size() << std::endl;
0282           if (weightsMode == "CLOB") {
0283             FromCLOB = true;
0284             if (m_debug)
0285               fout << " will read weights from DCC CLOB " << std::endl;
0286           }
0287           cbuffer = dcc.getDCCClob();
0288           unsigned int DCCClobSize = dcc.getDCCClobSize();
0289           std::string dccstr((char*)cbuffer, DCCClobSize);
0290           std::string DCCClob = dccstr;
0291           std::ostringstream osd;
0292           osd << "dcc.txt";
0293           std::string fname = osd.str();
0294           myfile.open(fname.c_str(), std::fstream::out);
0295           for (std::string::iterator it = DCCClob.begin(); it < DCCClob.end(); it++)
0296             myfile << *it;
0297           myfile.close();
0298           importDccConfigFile(*sr, fname, FromCLOB);
0299           if (m_debug)
0300             fout << " weight size after CLOB " << sr->dccNormalizedWeights_.size() << std::endl;
0301           rv = system("rm dcc.txt");
0302           if (m_debug && rv != 0)
0303             fout << "rm dcc.txt result code: " << rv << "\n";
0304         } catch (std::exception& e) {
0305           // we should not come here...
0306           std::cout << "ERROR: This config does not exist: tag " << myconfig_tag << " version " << myconfig_version
0307                     << std::endl;
0308           if (m_debug)
0309             fout << "ERROR: This config does not exist: tag " << myconfig_tag << " version " << myconfig_version
0310                  << std::endl;
0311           //        m_i_run_number = irun;
0312         }
0313         if (m_debug)
0314           fout << " FromCLOB " << FromCLOB << std::endl;
0315         if (!FromCLOB) {  // data from FE, we need to get FE_DAQ_CONFIG table
0316           // reading this configuration
0317           ODFEDAQConfig myconfig;
0318           myconfig.setId(fe_conf_id);
0319           econn->fetchConfigSet(&myconfig);
0320 
0321           // list weights
0322           int mywei = myconfig.getWeightId();
0323           if (m_debug)
0324             fout << " WEI_ID " << mywei << std::endl;
0325 
0326           if (mywei != 0) {
0327             ODWeightsSamplesDat samp;
0328             samp.setId(mywei);
0329             econn->fetchConfigSet(&samp);
0330             int mySample = samp.getSampleId();
0331             if (m_debug)
0332               fout << " SAMPLE_ID " << mySample << std::endl;
0333             sr->ecalDccZs1stSample_.push_back(mySample);
0334 
0335             ODWeightsDat weights;
0336             weights.setId(mywei);
0337             econn->fetchConfigSet(&weights);
0338 
0339             std::vector<std::vector<float> > my_dccw = weights.getWeight();
0340             int imax = my_dccw.size();
0341             if (m_debug)
0342               fout << " weight size before check " << imax << std::endl;
0343             if (imax == 75848) {  // all the channel present. Check for change
0344               bool WeightsChange = false, WeightsChangeEB = false, WeightsChangeEE = false;
0345               for (int i = 1; i < 61200; i++)  // EB
0346                 for (int ich = 0; ich < 6; ich++)
0347                   if (my_dccw[i][ich] != my_dccw[0][ich])
0348                     WeightsChangeEB = true;
0349               if (m_debug)
0350                 fout << " WeightsChangeEB " << WeightsChangeEB << std::endl;
0351               for (int i = 61201; i < 75848; i++)  // EE
0352                 for (int ich = 0; ich < 6; ich++)
0353                   if (my_dccw[i][ich] != my_dccw[61200][ich])
0354                     WeightsChangeEE = true;
0355               if (m_debug)
0356                 fout << " WeightsChangeEE " << WeightsChangeEE << std::endl;
0357               for (int ich = 0; ich < 6; ich++)
0358                 if (my_dccw[0][ich] != my_dccw[61200][ich])
0359                   WeightsChange = true;
0360               if (m_debug)
0361                 fout << " WeightsChange " << WeightsChange << std::endl;
0362 
0363               if (WeightsChangeEB || WeightsChangeEE)  // differences between channels, keep them all
0364                 sr->dccNormalizedWeights_ = my_dccw;
0365               else if (WeightsChange) {  // difference between EB and EE, keep only 1 channel from each
0366                 std::vector<float> dccwRowEB, dccwRowEE;
0367                 for (int ich = 0; ich < 6; ich++) {
0368                   dccwRowEB.push_back(my_dccw[0][ich]);
0369                   dccwRowEE.push_back(my_dccw[61200][ich]);
0370                 }
0371                 sr->dccNormalizedWeights_.push_back(dccwRowEB);
0372                 sr->dccNormalizedWeights_.push_back(dccwRowEE);
0373               } else {  // no difference keep only one
0374                 std::vector<float> dccwRow;
0375                 dccwRow.reserve(6);
0376                 for (int ich = 0; ich < 6; ich++) {
0377                   dccwRow.push_back(my_dccw[0][ich]);
0378                 }
0379                 sr->dccNormalizedWeights_.push_back(dccwRow);
0380                 if (m_debug) {
0381                   fout << " weight ";
0382                   for (int ich = 0; ich < 6; ich++)
0383                     fout << " ch " << ich << " " << sr->dccNormalizedWeights_[0][ich];
0384                   fout << std::endl;
0385                 }
0386               }
0387             }  // all channels
0388             else {
0389               sr->dccNormalizedWeights_ = my_dccw;
0390             }
0391             if (m_debug)
0392               fout << " weight size after DB " << sr->dccNormalizedWeights_.size() << std::endl;
0393           }  // WEI_ID != 0
0394         }    // weights got from FE
0395 
0396         // check if we have found the weights
0397         if (sr->dccNormalizedWeights_.empty()) {  // use the firmware default weights
0398           //          float opt[] = { -383, -383, -372, 279, 479, 380};
0399           float def[] = {-1215, 20, 297, 356, 308, 232};
0400           std::vector<float> dccw(def, def + 6);
0401           if (m_debug)
0402             fout << " default weights ";
0403           for (int i = 0; i < 6; i++) {
0404             if (m_debug)
0405               fout << " i " << i << " def " << def[i] << " dccw " << dccw[i] << " \n";
0406           }
0407           sr->dccNormalizedWeights_.push_back(dccw);  // vector vector
0408         }
0409         // normalize online weights
0410         int imax = sr->dccNormalizedWeights_.size();
0411         if (m_debug)
0412           fout << " weight size " << imax << " normalized weights : " << std::endl;
0413         for (int i = 0; i < imax; i++)
0414           for (int ich = 0; ich < 6; ich++) {
0415             sr->dccNormalizedWeights_[i][ich] /= 1024.;
0416             if (m_debug && i == 0)
0417               fout << " ch " << ich << " weight " << sr->dccNormalizedWeights_[i][ich] << std::endl;
0418           }
0419 
0420         /**************************/
0421         /*  checking for change   */
0422         /**************************/
0423         if (m_debug)
0424           fout << " checking for change " << std::endl;
0425         bool nochange = true;
0426         int imaxref, imaxnew;
0427 
0428         if (sref->deltaEta_ != sr->deltaEta_) {
0429           nochange = false;
0430           if (m_debug) {
0431             imaxref = sref->deltaEta_.size();
0432             imaxnew = sr->deltaEta_.size();
0433             if (imaxref != imaxnew) {
0434               fout << " deltaEta_ size ref " << imaxref << " now " << imaxnew << std::endl;
0435             } else {
0436               for (int i = 0; i < imaxref; i++) {
0437                 if (sref->deltaEta_[i] != sr->deltaEta_[i]) {
0438                   fout << " deltaEta_[" << i << "] ref " << sref->deltaEta_[i] << " now " << sr->deltaEta_[i]
0439                        << std::endl;
0440                 }
0441               }
0442             }
0443           }
0444         }
0445 
0446         if (sref->deltaPhi_ != sr->deltaPhi_) {
0447           nochange = false;
0448           if (m_debug) {
0449             imaxref = sref->deltaPhi_.size();
0450             imaxnew = sr->deltaPhi_.size();
0451             if (imaxref != imaxnew) {
0452               fout << " deltaPhi size ref " << imaxref << " now " << imaxnew << std::endl;
0453             } else {
0454               for (int i = 0; i < imaxref; i++) {
0455                 if (sref->deltaPhi_[i] != sr->deltaPhi_[i]) {
0456                   fout << " deltaPhi[" << i << "] ref " << sref->deltaPhi_[i] << " now " << sr->deltaPhi_[i]
0457                        << std::endl;
0458                 }
0459               }
0460             }
0461           }
0462         }
0463 
0464         if (sref->ecalDccZs1stSample_ != sr->ecalDccZs1stSample_) {
0465           nochange = false;
0466           if (m_debug) {
0467             imaxref = sref->ecalDccZs1stSample_.size();
0468             imaxnew = sr->ecalDccZs1stSample_.size();
0469             if (imaxref != imaxnew) {
0470               fout << " ecalDccZs1stSample size ref " << imaxref << " now " << imaxnew << std::endl;
0471             } else {
0472               for (int i = 0; i < imaxref; i++) {
0473                 if (sref->ecalDccZs1stSample_[i] != sr->ecalDccZs1stSample_[i]) {
0474                   fout << " ecalDccZs1stSample_[" << i << "] ref " << sref->ecalDccZs1stSample_[i] << " now "
0475                        << sr->ecalDccZs1stSample_[i] << std::endl;
0476                 }
0477               }
0478             }
0479           }
0480         }
0481 
0482         if (sref->ebDccAdcToGeV_ != sr->ebDccAdcToGeV_ || sref->eeDccAdcToGeV_ != sr->eeDccAdcToGeV_) {
0483           nochange = false;
0484           if (m_debug)
0485             fout << " ebDccAdcToGeV ref " << sref->ebDccAdcToGeV_ << " ee " << sref->eeDccAdcToGeV_ << " now "
0486                  << sr->ebDccAdcToGeV_ << " ee " << sr->eeDccAdcToGeV_ << std::endl;
0487         }
0488 
0489         if (sref->dccNormalizedWeights_ != sr->dccNormalizedWeights_) {
0490           nochange = false;
0491           if (m_debug) {
0492             imaxref = sref->dccNormalizedWeights_.size();
0493             imaxnew = sr->dccNormalizedWeights_.size();
0494             if (imaxref != imaxnew) {
0495               fout << " dccNormalizedWeights size ref " << imaxref << " now " << imaxnew << std::endl;
0496             } else {
0497               int i = 0;
0498               for (int ich = 0; ich < 6; ich++) {
0499                 if (sref->dccNormalizedWeights_[i][ich] != sr->dccNormalizedWeights_[i][ich]) {
0500                   fout << " dccNormalizedWeights_[" << i << "][" << ich << "] ref "
0501                        << sref->dccNormalizedWeights_[i][ich] << " now " << sr->dccNormalizedWeights_[i][ich]
0502                        << std::endl;
0503                 }
0504               }
0505             }
0506           }
0507         }
0508 
0509         if (sref->symetricZS_ != sr->symetricZS_) {
0510           nochange = false;
0511           if (m_debug) {
0512             imaxref = sref->symetricZS_.size();
0513             imaxnew = sr->symetricZS_.size();
0514             if (imaxref != imaxnew) {
0515               fout << " symetricZS size ref " << imaxref << " now " << imaxnew << std::endl;
0516             } else {
0517               for (int i = 0; i < imaxref; i++) {
0518                 if (sref->symetricZS_[i] != sr->symetricZS_[i]) {
0519                   fout << " symetricZS[" << i << "] ref " << sref->symetricZS_[i] << " now " << sr->symetricZS_[i]
0520                        << std::endl;
0521                 }
0522               }
0523             }
0524           }
0525         }
0526 
0527         if (sref->srpLowInterestChannelZS_ != sr->srpLowInterestChannelZS_) {
0528           nochange = false;
0529           if (m_debug) {
0530             imaxref = sref->srpLowInterestChannelZS_.size();
0531             imaxnew = sr->srpLowInterestChannelZS_.size();
0532             if (imaxref != imaxnew) {
0533               fout << " srpLowInterestChannelZS size ref " << imaxref << " now " << imaxnew << std::endl;
0534             } else {
0535               for (int i = 0; i < imaxref; i++) {
0536                 if (sref->srpLowInterestChannelZS_[i] != sr->srpLowInterestChannelZS_[i]) {
0537                   fout << " srpLowInterestChannelZS[" << i << "] ref " << sref->srpLowInterestChannelZS_[i] << " now "
0538                        << sr->srpLowInterestChannelZS_[i] << std::endl;
0539                 }
0540               }
0541             }
0542           }
0543         }
0544 
0545         if (sref->srpHighInterestChannelZS_ != sr->srpHighInterestChannelZS_) {
0546           nochange = false;
0547           if (m_debug) {
0548             imaxref = sref->srpHighInterestChannelZS_.size();
0549             imaxnew = sr->srpHighInterestChannelZS_.size();
0550             if (imaxref != imaxnew) {
0551               fout << " srpHighInterestChannelZS size ref " << imaxref << " now " << imaxnew << std::endl;
0552             } else {
0553               for (int i = 0; i < imaxref; i++) {
0554                 if (sref->srpHighInterestChannelZS_[i] != sr->srpHighInterestChannelZS_[i]) {
0555                   fout << " srpHighInterestChannelZS[" << i << "] ref " << sref->srpHighInterestChannelZS_[i] << " now "
0556                        << sr->srpHighInterestChannelZS_[i] << std::endl;
0557                 }
0558               }
0559             }
0560           }
0561         }
0562 
0563         if (sref->actions_ != sr->actions_) {
0564           nochange = false;
0565           if (m_debug) {
0566             for (int i = 0; i < 4; i++) {
0567               if (sref->actions_[i] != sr->actions_[i]) {
0568                 fout << " actions " << i << " ref " << sref->actions_[i] << " now " << sr->actions_[i] << std::endl;
0569               }
0570             }
0571           }
0572         }
0573 
0574         if (sref->tccMasksFromConfig_ != sr->tccMasksFromConfig_) {
0575           nochange = false;
0576           if (m_debug) {
0577             for (int i = 0; i < 108; i++) {
0578               if (sref->tccMasksFromConfig_[i] != sr->tccMasksFromConfig_[i]) {
0579                 fout << " tccMasks " << i << " ref " << sref->tccMasksFromConfig_[i] << " now "
0580                      << sr->tccMasksFromConfig_[i] << std::endl;
0581               }
0582             }
0583           }
0584         }
0585 
0586         if (sref->srpMasksFromConfig_ != sr->srpMasksFromConfig_) {
0587           nochange = false;
0588           if (m_debug) {
0589             for (int i = 0; i < 12; i++) {
0590               for (int ich = 0; ich < 8; ich++) {
0591                 if (sref->srpMasksFromConfig_[i][ich] != sr->srpMasksFromConfig_[i][ich]) {
0592                   fout << " srpMasks " << i << " " << ich << " ref " << sref->srpMasksFromConfig_[i][ich] << " now "
0593                        << sr->srpMasksFromConfig_[i][ich] << std::endl;
0594                 }
0595               }
0596             }
0597           }
0598         }
0599 
0600         if (sref->dccMasks_ != sr->dccMasks_) {
0601           nochange = false;
0602           if (m_debug) {
0603             for (int i = 0; i < 54; i++) {
0604               if (sref->dccMasks_[i] != sr->dccMasks_[i]) {
0605                 fout << " dccMasks " << i << " ref " << sref->dccMasks_[i] << " now " << sr->dccMasks_[i] << std::endl;
0606               }
0607             }
0608           }
0609         }
0610 
0611         if (sref->srfMasks_ != sr->srfMasks_) {
0612           nochange = false;
0613           if (m_debug) {
0614             for (int i = 0; i < 12; i++) {
0615               if (sref->srfMasks_[i] != sr->srfMasks_[i]) {
0616                 fout << " srfMasks " << i << " ref " << sref->srfMasks_[i] << " now " << sr->srfMasks_[i] << std::endl;
0617               }
0618             }
0619           }
0620         }
0621 
0622         if (sref->substitutionSrfs_ != sr->substitutionSrfs_) {
0623           nochange = false;
0624           if (m_debug) {
0625             for (int i = 0; i < 12; i++) {
0626               for (int ich = 0; ich < 68; ich++) {
0627                 if (sref->substitutionSrfs_[i][ich] != sr->substitutionSrfs_[i][ich]) {
0628                   fout << " substitutionSrfs " << i << " " << ich << " ref " << sref->substitutionSrfs_[i][ich]
0629                        << " now " << sr->substitutionSrfs_[i][ich] << std::endl;
0630                 }
0631               }
0632             }
0633           }
0634         }
0635 
0636         if (sref->testerTccEmuSrpIds_ != sr->testerTccEmuSrpIds_) {
0637           nochange = false;
0638           if (m_debug) {
0639             for (int i = 0; i < 12; i++) {
0640               fout << " testerTccEmuSrpIds " << i << " ref " << sref->testerTccEmuSrpIds_[i] << " now "
0641                    << sr->testerTccEmuSrpIds_[i] << std::endl;
0642             }
0643           }
0644         }
0645 
0646         if (sref->testerSrpEmuSrpIds_ != sr->testerSrpEmuSrpIds_) {
0647           nochange = false;
0648           if (m_debug)
0649             for (int i = 0; i < 12; i++) {
0650               fout << " testerSrpEmuSrpIds " << i << " ref " << sref->testerSrpEmuSrpIds_[i] << " now "
0651                    << sr->testerSrpEmuSrpIds_[i] << std::endl;
0652             }
0653         }
0654 
0655         if (sref->testerDccTestSrpIds_ != sr->testerDccTestSrpIds_) {
0656           nochange = false;
0657           if (m_debug)
0658             for (int i = 0; i < 12; i++) {
0659               fout << " testerDccTestSrpIds " << i << " ref " << sref->testerDccTestSrpIds_[i] << " now "
0660                    << sr->testerDccTestSrpIds_[i] << std::endl;
0661             }
0662         }
0663 
0664         if (sref->testerSrpTestSrpIds_ != sr->testerSrpTestSrpIds_) {
0665           nochange = false;
0666           if (m_debug)
0667             for (int i = 0; i < 12; i++) {
0668               fout << " testerSrpTestSrpIds " << i << " ref " << sref->testerSrpTestSrpIds_[i] << " now "
0669                    << sr->testerSrpTestSrpIds_[i] << std::endl;
0670             }
0671         }
0672 
0673         if (sref->bxOffsets_ != sr->bxOffsets_) {
0674           nochange = false;
0675           if (m_debug)
0676             for (int i = 0; i < 12; i++) {
0677               fout << " bxOffsets " << i << " ref " << sref->bxOffsets_[i] << " now " << sr->bxOffsets_[i] << std::endl;
0678             }
0679         }
0680 
0681         if (sref->bxGlobalOffset_ != sr->bxGlobalOffset_) {
0682           nochange = false;
0683           if (m_debug)
0684             fout << " bxGlobalOffset ref " << sr->bxGlobalOffset_ << " now " << sr->bxGlobalOffset_ << std::endl;
0685         }
0686 
0687         if (sref->automaticMasks_ != sr->automaticMasks_) {
0688           nochange = false;
0689           if (m_debug)
0690             fout << " automaticMasks ref " << sref->automaticMasks_ << " now " << sr->automaticMasks_ << std::endl;
0691         }
0692 
0693         if (sref->automaticSrpSelect_ != sr->automaticSrpSelect_) {
0694           nochange = false;
0695           if (m_debug)
0696             fout << " automaticSrpSelect ref " << sref->automaticSrpSelect_ << " now " << sr->automaticSrpSelect_
0697                  << std::endl;
0698         }
0699 
0700         if (nochange) {
0701           if (m_debug)
0702             fout << " no change has been found " << std::endl;
0703           std::ostringstream ss;
0704           ss << "Run=" << irun << "_SRPunchanged_" << std::endl;
0705           m_userTextLog = ss.str() + ";";
0706         } else {
0707           if (m_debug) {
0708             fout << " Change has been found !\n   New payload :" << std::endl;
0709             PrintPayload(*sr, fout);
0710           }
0711           ChangePayload(*sref, *sr);  // new reference
0712           // write the new payload to ORCON/ORCOFF
0713           EcalSRSettings* srp_pop = new EcalSRSettings();
0714           ChangePayload(*srp_pop, *sr);  // add this payload to DB
0715           m_to_transfer.push_back(std::make_pair(srp_pop, irun));
0716 
0717           std::ostringstream ss;
0718           ss << "Run=" << irun << "_SRPchanged_" << std::endl;
0719           m_userTextLog = ss.str() + ";";
0720 
0721           if (m_to_transfer.size() >= 20)
0722             break;
0723         }
0724         delete sr;
0725         m_i_tag = myconfig_tag;
0726         m_i_version = myconfig_version;
0727         fe_conf_id_old = fe_conf_id;
0728       }  // different tag or version or fe config
0729     }    // loop over runs
0730   }      // test on number of runs
0731 
0732   // disconnect from DB
0733   delete econn;
0734   fout.close();
0735 }
0736 
0737 void popcon::EcalSRPHandler::importDccConfigFile(EcalSRSettings& sr, const std::string& filename, bool useCLOB) {
0738   cms::concurrency::xercesInitialize();
0739 
0740   XercesDOMParser* parser = new XercesDOMParser;
0741   parser->setValidationScheme(XercesDOMParser::Val_Never);
0742   parser->setDoNamespaces(false);
0743   parser->setDoSchema(false);
0744 
0745   parser->parse(filename.c_str());
0746 
0747   DOMDocument* xmlDoc = parser->getDocument();
0748   if (!xmlDoc) {
0749     std::cout << "importDccConfigFile Error parsing document" << std::endl;
0750   }
0751 
0752   DOMElement* element = xmlDoc->getDocumentElement();
0753   std::string type = cms::xerces::toString(element->getTagName());
0754 
0755   // 1st level
0756   int EBEE = -1;
0757   int L1ZS[2] = {0, 0}, L2ZS[2] = {0, 0};
0758   for (DOMNode* childNode = element->getFirstChild(); childNode; childNode = childNode->getNextSibling()) {
0759     if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
0760       const std::string foundName = cms::xerces::toString(childNode->getNodeName());
0761       DOMElement* child = static_cast<DOMElement*>(childNode);
0762       DOMNamedNodeMap* attributes = child->getAttributes();
0763       unsigned int numAttributes = attributes->getLength();
0764       for (unsigned int j = 0; j < numAttributes; ++j) {
0765         DOMNode* attributeNode = attributes->item(j);
0766         DOMAttr* attribute = static_cast<DOMAttr*>(attributeNode);
0767         const std::string info = cms::xerces::toString(attribute->getName());
0768         const std::string scope = cms::xerces::toString(attribute->getValue());
0769         if (info == "_scope") {
0770           if (scope.substr(0, 2) == "EE")
0771             EBEE = 1;
0772           else
0773             EBEE = 0;
0774         }
0775       }
0776       // 2nd level
0777       for (DOMNode* subchildNode = childNode->getFirstChild(); subchildNode;
0778            subchildNode = subchildNode->getNextSibling()) {
0779         if (subchildNode->getNodeType() == DOMNode::ELEMENT_NODE) {
0780           const std::string subName = cms::xerces::toString(subchildNode->getNodeName());
0781           // 3rd level
0782           for (DOMNode* subsubchildNode = subchildNode->getFirstChild(); subsubchildNode;
0783                subsubchildNode = subsubchildNode->getNextSibling()) {
0784             if (subsubchildNode->getNodeType() == DOMNode::ELEMENT_NODE) {
0785               const std::string subName = cms::xerces::toString(subsubchildNode->getNodeName());
0786               if (subName == "L1ZSUPPRESSION")
0787                 GetNodeData(subsubchildNode, L1ZS[EBEE]);
0788               if (subName == "L2ZSUPPRESSION")
0789                 GetNodeData(subsubchildNode, L2ZS[EBEE]);
0790               if (subName == "FIRSTZSSAMPLE") {
0791                 int ZS;
0792                 GetNodeData(subsubchildNode, ZS);
0793                 if (useCLOB)
0794                   sr.ecalDccZs1stSample_.push_back(ZS);
0795               }
0796               if (subName == "CXTALWEIGHTS") {
0797                 std::vector<float> dcc(6);
0798                 float w;
0799                 for (int iw = 0; iw < 6; iw++) {
0800                   GetNodeData(subsubchildNode, w);
0801                   dcc.push_back(w);
0802                 }
0803                 if (useCLOB)
0804                   sr.dccNormalizedWeights_.push_back(dcc);  // vector vector
0805               }
0806             }
0807           }  // loop over subsubchild
0808         }
0809       }  // loop over subchild
0810     }
0811   }                                                             // loop over child
0812   sr.srpLowInterestChannelZS_.push_back(L1ZS[0] * 0.035 / 4);   //  EB
0813   sr.srpLowInterestChannelZS_.push_back(L1ZS[1] * 0.060 / 4);   //  EE
0814   sr.srpHighInterestChannelZS_.push_back(L2ZS[0] * 0.035 / 4);  //  EB
0815   sr.srpHighInterestChannelZS_.push_back(L2ZS[1] * 0.060 / 4);  //  EE
0816   delete parser;
0817   cms::concurrency::xercesTerminate();
0818 }
0819 
0820 void popcon::EcalSRPHandler::PrintPayload(EcalSRSettings& sr, std::ofstream& fout) {
0821   int imax = sr.deltaEta_.size();
0822   fout << " deltaEta[" << imax << "] ";
0823   for (int i = 0; i < imax; i++) {
0824     fout << sr.deltaEta_[i] << " ";
0825   }
0826   fout << std::endl;
0827 
0828   imax = sr.deltaPhi_.size();
0829   fout << " deltaPhi[" << imax << "] ";
0830   for (int i = 0; i < imax; i++) {
0831     fout << sr.deltaPhi_[i] << " ";
0832   }
0833   fout << std::endl;
0834 
0835   imax = sr.ecalDccZs1stSample_.size();
0836   fout << " ecalDccZs1stSample[" << imax << "] ";
0837   for (int i = 0; i < imax; i++) {
0838     fout << sr.ecalDccZs1stSample_[i] << " ";
0839   }
0840   fout << std::endl;
0841 
0842   fout << " ebDccAdcToGeV " << sr.ebDccAdcToGeV_ << std::endl;
0843 
0844   fout << " eeDccAdcToGeV " << sr.eeDccAdcToGeV_ << std::endl;
0845 
0846   fout << " dccNormalizedWeights" << std::endl;
0847   for (int i = 0; i < (int)sr.dccNormalizedWeights_.size(); ++i) {
0848     fout << " Channel " << i;
0849     for (int j = 0; j < (int)sr.dccNormalizedWeights_[i].size(); ++j)
0850       fout << " " << sr.dccNormalizedWeights_[i][j];
0851     fout << std::endl;
0852   }
0853 
0854   imax = sr.symetricZS_.size();
0855   fout << " symetricZS[" << imax << "] ";
0856   for (int i = 0; i < imax; i++) {
0857     fout << sr.symetricZS_[i] << " ";
0858   }
0859   fout << std::endl;
0860 
0861   imax = sr.srpLowInterestChannelZS_.size();
0862   fout << " srpLowInterestChannelZS[" << imax << "] ";
0863   for (int i = 0; i < imax; i++) {
0864     fout << sr.srpLowInterestChannelZS_[i] << " ";
0865   }
0866   fout << std::endl;
0867 
0868   imax = sr.srpHighInterestChannelZS_.size();
0869   fout << " srpHighInterestChannelZS[" << imax << "] ";
0870   for (int i = 0; i < imax; i++) {
0871     fout << sr.srpHighInterestChannelZS_[i] << " ";
0872   }
0873   fout << std::endl;
0874 
0875   imax = sr.actions_.size();
0876   fout << " actions[" << imax << "] ";
0877   for (int i = 0; i < imax; i++) {
0878     fout << sr.actions_[i] << " ";
0879   }
0880   fout << std::endl;
0881 
0882   imax = sr.tccMasksFromConfig_.size();
0883   fout << " tccMasksFromConfig[" << imax << "] ";
0884   for (int i = 0; i < imax; i++) {
0885     fout << sr.tccMasksFromConfig_[i] << " ";
0886   }
0887   fout << std::endl;
0888 
0889   fout << " srpMasksFromConfig" << std::endl;
0890   for (int i = 0; i < (int)sr.srpMasksFromConfig_.size(); ++i) {
0891     for (int j = 0; j < (int)sr.srpMasksFromConfig_[i].size(); ++j)
0892       fout << sr.srpMasksFromConfig_[i][j] << " ";
0893     fout << std::endl;
0894   }
0895 
0896   imax = sr.dccMasks_.size();
0897   fout << " dccMasks[" << imax << "] ";
0898   for (int i = 0; i < imax; i++) {
0899     fout << sr.dccMasks_[i] << " ";
0900   }
0901   fout << std::endl;
0902 
0903   imax = sr.srfMasks_.size();
0904   fout << " srfMasks[" << imax << "] ";
0905   for (int i = 0; i < imax; i++) {
0906     fout << sr.srfMasks_[i] << " ";
0907   }
0908   fout << std::endl;
0909 
0910   fout << "substitutionSrfs" << std::endl;
0911   for (int i = 0; i < (int)sr.substitutionSrfs_.size(); ++i) {
0912     for (int j = 0; j < (int)sr.substitutionSrfs_[i].size(); ++j)
0913       fout << sr.substitutionSrfs_[i][j] << " ";
0914     fout << std::endl;
0915   }
0916 
0917   imax = sr.testerTccEmuSrpIds_.size();
0918   fout << " testerTccEmuSrpIds[" << imax << "] ";
0919   for (int i = 0; i < imax; i++) {
0920     fout << sr.testerTccEmuSrpIds_[i] << " ";
0921   }
0922   fout << std::endl;
0923 
0924   imax = sr.testerSrpEmuSrpIds_.size();
0925   fout << " testerSrpEmuSrpIds[" << imax << "] ";
0926   for (int i = 0; i < imax; i++) {
0927     fout << sr.testerSrpEmuSrpIds_[i] << " ";
0928   }
0929   fout << std::endl;
0930 
0931   imax = sr.testerDccTestSrpIds_.size();
0932   fout << " testerDccTestSrpIds[" << imax << "] ";
0933   for (int i = 0; i < imax; i++) {
0934     fout << sr.testerDccTestSrpIds_[i] << " ";
0935   }
0936   fout << std::endl;
0937 
0938   imax = sr.testerSrpTestSrpIds_.size();
0939   fout << " testerSrpTestSrpIds[" << imax << "] ";
0940   for (int i = 0; i < imax; i++) {
0941     fout << sr.testerSrpTestSrpIds_[i] << " ";
0942   }
0943   fout << std::endl;
0944 
0945   imax = sr.bxOffsets_.size();
0946   fout << " bxOffsets[" << imax << "] ";
0947   for (int i = 0; i < imax; i++) {
0948     fout << sr.bxOffsets_[i] << " ";
0949   }
0950   fout << std::endl;
0951 
0952   fout << " bxGlobalOffset " << sr.bxGlobalOffset_ << std::endl;
0953   fout << " automaticMasks " << sr.automaticMasks_ << std::endl;
0954   fout << " automaticSrpSelect " << sr.automaticSrpSelect_ << std::endl;
0955 }
0956 
0957 void popcon::EcalSRPHandler::ChangePayload(EcalSRSettings& sref, EcalSRSettings& sr) {
0958   sref.deltaEta_ = sr.deltaEta_;
0959   sref.deltaPhi_ = sr.deltaPhi_;
0960   sref.ecalDccZs1stSample_ = sr.ecalDccZs1stSample_;
0961   sref.ebDccAdcToGeV_ = sr.ebDccAdcToGeV_;
0962   sref.eeDccAdcToGeV_ = sr.eeDccAdcToGeV_;
0963   sref.dccNormalizedWeights_ = sr.dccNormalizedWeights_;
0964   sref.symetricZS_ = sr.symetricZS_;
0965   sref.srpLowInterestChannelZS_ = sr.srpLowInterestChannelZS_;
0966   sref.srpHighInterestChannelZS_ = sr.srpHighInterestChannelZS_;
0967   sref.actions_ = sr.actions_;
0968   sref.tccMasksFromConfig_ = sr.tccMasksFromConfig_;
0969   sref.srpMasksFromConfig_ = sr.srpMasksFromConfig_;
0970   sref.dccMasks_ = sr.dccMasks_;
0971   sref.srfMasks_ = sr.srfMasks_;
0972   sref.substitutionSrfs_ = sr.substitutionSrfs_;
0973   sref.testerTccEmuSrpIds_ = sr.testerTccEmuSrpIds_;
0974   sref.testerSrpEmuSrpIds_ = sr.testerSrpEmuSrpIds_;
0975   sref.testerDccTestSrpIds_ = sr.testerDccTestSrpIds_;
0976   sref.testerSrpTestSrpIds_ = sr.testerSrpTestSrpIds_;
0977   sref.bxOffsets_ = sr.bxOffsets_;
0978   sref.bxGlobalOffset_ = sr.bxGlobalOffset_;
0979   sref.automaticMasks_ = sr.automaticMasks_;
0980   sref.automaticSrpSelect_ = sr.automaticSrpSelect_;
0981 }