CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondTools/Ecal/src/EcalSRPHandler.cc

Go to the documentation of this file.
00001 #include<iostream>
00002 #include <fstream>
00003 #include <string>
00004 #include <sstream>
00005 #include <xercesc/dom/DOMNode.hpp>
00006 #include <xercesc/dom/DOM.hpp>
00007 #include <xercesc/parsers/XercesDOMParser.hpp>
00008 #include <xercesc/util/PlatformUtils.hpp>
00009 #include <xercesc/util/XMLString.hpp>
00010 #include <xercesc/sax/SAXException.hpp>
00011 #include <xercesc/framework/LocalFileFormatTarget.hpp>
00012 
00013 #include "CondTools/Ecal/interface/EcalSRPHandler.h"
00014 #include "CondTools/Ecal/interface/EcalCondHeader.h"
00015 #include "CondTools/Ecal/interface/DOMHelperFunctions.h"
00016 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00017 #include "CondFormats/EcalObjects/interface/EcalSRSettings.h"
00018 
00019 #include "OnlineDB/EcalCondDB/interface/RunConfigDat.h"
00020 #include "OnlineDB/EcalCondDB/interface/ODRunConfigInfo.h"
00021 #include "OnlineDB/EcalCondDB/interface/ODRunConfigSeqInfo.h"
00022 
00023 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
00024 #include "OnlineDB/EcalCondDB/interface/FEConfigBadTTInfo.h"
00025 
00026 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00027 
00028 #include "SimCalorimetry/EcalSelectiveReadoutProducers/interface/EcalSRCondTools.h"
00029 
00030 using namespace XERCES_CPP_NAMESPACE;
00031 using namespace xuti;
00032 
00033 popcon::EcalSRPHandler::EcalSRPHandler(const edm::ParameterSet & ps)
00034   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalSRPHandler")) {
00035 
00036   m_firstRun =(unsigned long)atoi( ps.getParameter<std::string>("firstRun").c_str());
00037   m_lastRun  =(unsigned long)atoi( ps.getParameter<std::string>("lastRun").c_str());
00038   m_sid      = ps.getParameter<std::string>("OnlineDBSID");
00039   m_user     = ps.getParameter<std::string>("OnlineDBUser");
00040   m_pass     = ps.getParameter<std::string>("OnlineDBPassword");
00041   m_location = ps.getParameter<std::string>("location"); 
00042   m_runtype  = ps.getParameter<std::string>("runtype"); 
00043   m_gentag   = ps.getParameter<std::string>("gentag"); 
00044   m_i_tag    = "";
00045   m_debug    = ps.getParameter<bool>("debug"); 
00046   m_i_version= 0;
00047 
00048 }
00049 
00050 popcon::EcalSRPHandler::~EcalSRPHandler() {}
00051 
00052 void popcon::EcalSRPHandler::getNewObjects() {
00053 
00054   std::ostringstream ss; 
00055   ss<<"ECAL ";
00056 
00057   unsigned long long max_since = 1;
00058 
00059   // this is the last inserted run 
00060   max_since = tagInfo().lastInterval.first;
00061 
00062   // this is the last object in the DB 
00063   Ref srp_db = lastPayload();
00064   if(m_debug)
00065     std::cout << " max_since : "  << max_since
00066               << "\n retrieved last payload "  << std::endl;
00067 
00068   // we copy the last valid record to a temporary object
00069   EcalSRSettings* sref = new EcalSRSettings();
00070   sref->deltaEta_ = srp_db->deltaEta_;
00071   sref->deltaPhi_ = srp_db->deltaPhi_;
00072   sref->ecalDccZs1stSample_ = srp_db->ecalDccZs1stSample_;
00073   sref->ebDccAdcToGeV_ = srp_db->ebDccAdcToGeV_;
00074   sref->eeDccAdcToGeV_ = srp_db->eeDccAdcToGeV_;
00075   sref->dccNormalizedWeights_ = srp_db->dccNormalizedWeights_;
00076   sref->symetricZS_ = srp_db->symetricZS_;
00077   sref->srpLowInterestChannelZS_ = srp_db->srpLowInterestChannelZS_;
00078   sref->srpHighInterestChannelZS_ = srp_db->srpHighInterestChannelZS_;
00079   sref->actions_ = srp_db->actions_;
00080   sref->tccMasksFromConfig_ = srp_db->tccMasksFromConfig_;
00081   sref->srpMasksFromConfig_ = srp_db->srpMasksFromConfig_;
00082   sref->dccMasks_ = srp_db->dccMasks_;
00083   sref->srfMasks_ = srp_db->srfMasks_;
00084   sref->substitutionSrfs_= srp_db->substitutionSrfs_;
00085   sref->testerTccEmuSrpIds_ = srp_db->testerTccEmuSrpIds_;
00086   sref->testerSrpEmuSrpIds_ = srp_db->testerSrpEmuSrpIds_;
00087   sref->testerDccTestSrpIds_ = srp_db->testerDccTestSrpIds_;
00088   sref->testerSrpTestSrpIds_ = srp_db->testerSrpTestSrpIds_;
00089   sref->bxOffsets_ = srp_db->bxOffsets_;
00090   sref->bxGlobalOffset_ = srp_db->bxGlobalOffset_;
00091   sref->automaticMasks_ = srp_db->automaticMasks_;
00092   sref->automaticSrpSelect_ = srp_db->automaticSrpSelect_;
00093 
00094   // now read the actual status from the online DB
00095 
00096   std::cout << "Retrieving DAQ status from OMDS DB ... " << std::endl;
00097   econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00098   std::cout << "Connection done" << std::endl;
00099         
00100   if (!econn) {
00101     std::cout << " Problem with OMDS: connection parameters " << m_sid << "/" << m_user << std::endl;
00102     throw cms::Exception("OMDS not available");
00103   }
00104 
00105   LocationDef my_locdef;
00106   my_locdef.setLocation(m_location); 
00107 
00108   RunTypeDef my_rundef;
00109   my_rundef.setRunType(m_runtype); 
00110 
00111   RunTag my_runtag;
00112   my_runtag.setLocationDef( my_locdef );
00113   my_runtag.setRunTypeDef( my_rundef );
00114   my_runtag.setGeneralTag(m_gentag); 
00115 
00116   // range of validity
00117   int min_run = 0;
00118   if(m_firstRun <= (unsigned long)max_since) {
00119     min_run=  (int)max_since+1;    // we have to add 1 to the last transferred one
00120   } 
00121   else { min_run=(int)m_firstRun; }
00122 
00123   int max_run=(int)m_lastRun;
00124   if(m_debug)
00125     std::cout << "min_run " << min_run << " max_run " << max_run << std::endl;
00126 
00127   std::ofstream fout;
00128   if(m_debug) {
00129     char outfile[800];
00130     sprintf(outfile,"SRP_run%d.txt",min_run);
00131     fout.open(outfile, std::fstream::out);
00132     fout << " ORCOFF last run max_since : "  << max_since << std::endl;
00133     PrintPayload(*sref, fout);
00134   }
00135 
00136   RunList my_list; 
00137   my_list = econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef); 
00138               
00139   std::vector<RunIOV> run_vec = my_list.getRuns();
00140   int num_runs = run_vec.size();
00141 
00142   if(m_debug) {
00143     fout << " number of runs is : " << num_runs << std::endl;
00144   }
00145   unsigned long irun = 0;
00146   if(num_runs > 0){
00147     int fe_conf_id_old = 0;
00148     //    int krmax = std::min(num_runs, 100);
00149     //    for(int kr = 0; kr < krmax; kr++) {
00150     for(int kr = 0; kr < num_runs; kr++) {  // allow any number of runs, exit on transfert size
00151       irun = (unsigned long) run_vec[kr].getRunNumber();
00152       std::string geneTag = run_vec[kr].getRunTag().getGeneralTag();
00153       if(geneTag != "GLOBAL") {
00154         if(m_debug)
00155           fout <<  "\n New run " << irun << " with tag " << geneTag << " giving up " << std::endl;
00156         continue;
00157       }
00158       if(m_debug)
00159         fout << "\n New run " << irun << " geneTag " << geneTag << std::endl;
00160 
00161       // First, RUN_CONFIGURATION
00162       std::map<EcalLogicID, RunConfigDat> dataset;
00163       econn->fetchDataSet(&dataset, &run_vec[kr]);
00164       std::string myconfig_tag = "";
00165       int myconfig_version = 0;
00166       std::map< EcalLogicID, RunConfigDat>::const_iterator it;
00167       if(dataset.size() != 1) {
00168         std::cout << "\n\n run " << irun << " strange number of dataset "
00169                   << dataset.size() << std::endl;
00170         if(m_debug)
00171           fout << "\n\n run " << irun << " strange number of dataset "
00172                << dataset.size() << " giving up " << std::endl;
00173         continue;
00174       }
00175 
00176       it = dataset.begin();
00177       RunConfigDat dat = it->second;
00178       myconfig_tag = dat.getConfigTag();
00179       if(myconfig_tag.substr(0,15) == "ZeroSuppression") {
00180         if(m_debug)
00181           fout << " run " << irun << " tag " << myconfig_tag << " giving up " << std::endl;
00182         continue;
00183       }
00184 
00185       // Now  FE_DAQ_CONFIG
00186       typedef std::map<EcalLogicID, RunFEConfigDat>::const_iterator feConfIter;
00187       std::map<EcalLogicID, RunFEConfigDat> feconfig;
00188       econn->fetchDataSet(&feconfig, &run_vec[kr]);
00189       if(feconfig.size() != 1) {
00190         if(m_debug)
00191           fout << "\n\n run " << irun << " strange number of FE config "
00192                << feconfig.size() << " giving up " << std::endl;
00193         continue;
00194       }
00195       RunFEConfigDat rd_fe;
00196       int fe_conf_id = 0;
00197       feConfIter p=feconfig.begin();
00198       rd_fe   = p->second;
00199       fe_conf_id = rd_fe.getConfigId();
00200 
00201       myconfig_version = dat.getConfigVersion();
00202       if(m_debug)
00203         fout << " run " << irun << " tag " << myconfig_tag 
00204              << " version " << myconfig_version 
00205              << " Fe config " << fe_conf_id << std::endl;
00206       // here we should check if it is the same as previous run.
00207       if(myconfig_tag != m_i_tag || myconfig_version != m_i_version || fe_conf_id != fe_conf_id_old) {
00208         if(m_debug)
00209           fout << " run= " << irun 
00210                << " different tag  ... retrieving last config set from DB" << std::endl;
00211 
00212         bool FromCLOB = false;
00213         EcalSRSettings* sr = new EcalSRSettings;
00214         sr->ebDccAdcToGeV_ = 0.035;
00215         sr->eeDccAdcToGeV_ = 0.060;
00216         sr->symetricZS_.push_back(0);
00217 
00218         ODRunConfigInfo od_run_info;
00219         od_run_info.setTag(myconfig_tag);
00220         od_run_info.setVersion(myconfig_version);
00221 
00222         try { 
00223           econn-> fetchConfigSet(&od_run_info);
00224           int config_id = od_run_info.getId();
00225 
00226           ODRunConfigSeqInfo seq;
00227           seq.setEcalConfigId(config_id);
00228           seq.setSequenceNumber(0);
00229           econn-> fetchConfigSet(&seq);
00230           int sequenceid = seq.getSequenceId();
00231 
00232           ODEcalCycle ecal_cycle;
00233           ecal_cycle.setSequenceId(sequenceid);
00234           econn-> fetchConfigSet(&ecal_cycle);
00235           int cycle_id = ecal_cycle.getId();
00236           int srp_id = ecal_cycle.getSRPId();
00237           if(srp_id == 0) {
00238             if(m_debug)
00239               fout << " no SRP config for this run, give up " << std::endl;
00240             delete sr;
00241             continue;    //  no SRP config
00242           }
00243           int dcc_id = ecal_cycle.getDCCId();
00244           if(m_debug)
00245             fout << " cycleid " << cycle_id << " SRP id " << srp_id << " DCC id " << dcc_id << std::endl;
00246             /**************************/
00247            /*          SRP           */ 
00248           /**************************/
00249           ODSRPConfig srp;
00250           srp.setId(srp_id);
00251           econn-> fetchConfigSet(&srp);
00252 
00253           unsigned char* cbuffer = srp.getSRPClob();
00254           unsigned int SRPClobSize = srp.getSRPClobSize();
00255           std::string srpstr((char*)cbuffer, SRPClobSize);
00256           std::string SRPClob = srpstr;
00257           std::fstream myfile;
00258           myfile.open ("srp.txt", std::fstream::out);
00259           for (std::string::iterator it = SRPClob.begin() ; it < SRPClob.end(); it++ )
00260             myfile << *it;
00261           myfile.close();
00262           std::ifstream f("srp.txt");
00263           if(!f.good()){
00264             throw cms::Exception("EcalSRPHandler") << " Failed to open file srp.txt";
00265             if(m_debug)
00266               fout << " Failed to open file srp.txt" << std::endl;
00267           }
00268           EcalSRCondTools::importSrpConfigFile(*sr, f, m_debug);
00269           f.close();
00270           int rv = system("rm srp.txt");
00271           if(m_debug && rv != 0) fout << "rm srp.txt result code: " << rv << "\n";
00272 
00273           sr->bxGlobalOffset_ = srp.getSRP0BunchAdjustPosition();
00274           sr->automaticMasks_ = srp.getAutomaticMasks();
00275           sr->automaticSrpSelect_ = srp.getAutomaticSrpSelect();
00276             /**************************/
00277            /*          DCC           */ 
00278           /**************************/
00279           ODDCCConfig dcc;
00280           dcc.setId(dcc_id);
00281           econn-> fetchConfigSet(&dcc);
00282           std::string weightsMode = dcc.getDCCWeightsMode();
00283           if(m_debug)
00284             fout << " DCC weightsMode " << weightsMode << std::endl
00285                  << " weight size beg " <<  sr->dccNormalizedWeights_.size() << std::endl;
00286           if(weightsMode == "CLOB") {
00287             FromCLOB = true;
00288             if(m_debug)
00289               fout << " will read weights from DCC CLOB " << std::endl;
00290           }
00291           cbuffer = dcc.getDCCClob();
00292           unsigned int DCCClobSize = dcc.getDCCClobSize();
00293           std::string dccstr((char*)cbuffer, DCCClobSize);
00294           std::string DCCClob = dccstr;
00295           std::ostringstream osd;
00296           osd << "dcc.txt";
00297           std::string fname = osd.str();
00298           myfile.open (fname.c_str(), std::fstream::out);
00299           for (std::string::iterator it = DCCClob.begin() ; it < DCCClob.end(); it++ )
00300             myfile << *it;
00301           myfile.close();
00302           importDccConfigFile(*sr, fname, FromCLOB);
00303           if(m_debug)
00304             fout << " weight size after CLOB " <<  sr->dccNormalizedWeights_.size() << std::endl;
00305           rv = system("rm dcc.txt");
00306           if(m_debug && rv != 0) fout << "rm dcc.txt result code: " << rv << "\n";
00307         }
00308         catch (std::exception &e) { 
00309           // we should not come here... 
00310           std::cout << "ERROR: This config does not exist: tag " << myconfig_tag
00311                     <<" version "<< myconfig_version<< std::endl;
00312           if(m_debug)
00313             fout << "ERROR: This config does not exist: tag " << myconfig_tag
00314                  <<" version "<< myconfig_version<< std::endl;
00315           //        m_i_run_number = irun;
00316         }
00317         if(m_debug)
00318           fout << " FromCLOB " << FromCLOB << std::endl;
00319         if(!FromCLOB) {  // data from FE, we need to get FE_DAQ_CONFIG table
00320           // reading this configuration
00321           ODFEDAQConfig myconfig;
00322           myconfig.setId(fe_conf_id);
00323           econn->fetchConfigSet(&myconfig);
00324 
00325           // list weights
00326           int mywei = myconfig.getWeightId();
00327           if(m_debug)
00328             fout << " WEI_ID " << mywei << std::endl;
00329 
00330           if(mywei != 0) {
00331             ODWeightsSamplesDat samp;
00332             samp.setId(mywei);
00333             econn->fetchConfigSet(&samp);
00334             int mySample = samp.getSampleId();
00335             if(m_debug)
00336               fout << " SAMPLE_ID " << mySample << std::endl;
00337             sr->ecalDccZs1stSample_.push_back(mySample);
00338 
00339             ODWeightsDat weights;
00340             weights.setId(mywei);
00341             econn->fetchConfigSet(&weights);
00342 
00343             std::vector<std::vector<float> > my_dccw =  weights.getWeight();
00344             int imax = my_dccw.size();
00345             if(m_debug)
00346               fout << " weight size before check " << imax << std::endl;
00347             if(imax == 75848) {  // all the channel present. Check for change
00348               bool WeightsChange = false, WeightsChangeEB = false, WeightsChangeEE = false;
00349               for (int i = 1; i < 61200 ; i++)   // EB
00350                 for (int ich = 0; ich < 6 ; ich++) 
00351                   if(my_dccw[i][ich] != my_dccw[0][ich]) WeightsChangeEB = true;
00352               if(m_debug)
00353                 fout << " WeightsChangeEB " << WeightsChangeEB << std::endl;
00354               for(int i = 61201; i < 75848 ; i++)   // EE
00355                 for (int ich = 0; ich < 6 ; ich++) 
00356                   if(my_dccw[i][ich] != my_dccw[61200][ich]) WeightsChangeEE = true;
00357               if(m_debug)
00358                 fout << " WeightsChangeEE " << WeightsChangeEE << std::endl;
00359               for(int ich = 0; ich < 6 ; ich++) 
00360                 if(my_dccw[0][ich] != my_dccw[61200][ich]) WeightsChange = true;
00361               if(m_debug)
00362                 fout << " WeightsChange " << WeightsChange << std::endl;
00363 
00364               if(WeightsChangeEB || WeightsChangeEE)  // differences between channels, keep them all
00365                 sr->dccNormalizedWeights_ = my_dccw;
00366               else if (WeightsChange) {  // difference between EB and EE, keep only 1 channel from each
00367                 std::vector<float> dccwRowEB, dccwRowEE;
00368                 for (int ich = 0; ich < 6 ; ich++) {
00369                   dccwRowEB.push_back(my_dccw[0][ich]);
00370                   dccwRowEE.push_back(my_dccw[61200][ich]);
00371                 }
00372                 sr->dccNormalizedWeights_.push_back(dccwRowEB);
00373                 sr->dccNormalizedWeights_.push_back(dccwRowEE);
00374               }
00375               else {  // no difference keep only one
00376                 std::vector<float> dccwRow;
00377                 for (int ich = 0; ich < 6 ; ich++) {
00378                   dccwRow.push_back(my_dccw[0][ich]);
00379                 }
00380                 sr->dccNormalizedWeights_.push_back(dccwRow);
00381                 if(m_debug) {
00382                   fout << " weight ";
00383                   for(int ich = 0; ich < 6 ; ich++) 
00384                     fout << " ch " << ich << " " <<  sr->dccNormalizedWeights_[0][ich] ;
00385                   fout << std::endl;
00386                 }
00387               }
00388             }   // all channels
00389             else {
00390               sr->dccNormalizedWeights_ = my_dccw;
00391             }
00392             if(m_debug)
00393               fout << " weight size after DB " <<  sr->dccNormalizedWeights_.size() << std::endl;
00394           } // WEI_ID != 0
00395         }  // weights got from FE
00396 
00397         // check if we have found the weights
00398         if(sr->dccNormalizedWeights_.size() == 0) {  // use the firmware default weights
00399           //          float opt[] = { -383, -383, -372, 279, 479, 380};
00400           float def[] = {-1215,   20,  297, 356, 308, 232};
00401           std::vector<float> dccw(def,def+6);
00402           if(m_debug)
00403             fout << " default weights " ;
00404           for(int i = 0; i < 6; i++) {
00405             if(m_debug)
00406               fout << " i " << i << " def " << def [i] << " dccw " << dccw[i] << " \n";
00407           }
00408           sr->dccNormalizedWeights_.push_back(dccw);  // vector vector
00409         }
00410         // normalize online weights
00411         int imax = sr->dccNormalizedWeights_.size();
00412         if(m_debug)
00413           fout << " weight size " << imax << " normalized weights : "<< std::endl;
00414         for(int i = 0; i < imax ; i++)
00415           for(int ich = 0; ich < 6 ; ich++) {
00416             sr->dccNormalizedWeights_[i][ich] /= 1024.;
00417             if (m_debug && i == 0) 
00418               fout << " ch " << ich << " weight " <<  sr->dccNormalizedWeights_[i][ich] << std::endl;
00419           }
00420 
00421           /**************************/
00422          /*  checking for change   */
00423         /**************************/
00424         if(m_debug)
00425           fout << " checking for change " << std::endl;
00426         bool nochange = true;
00427         int imaxref, imaxnew;
00428 
00429         if(sref->deltaEta_ != sr->deltaEta_) {
00430           nochange = false;
00431           if(m_debug) {
00432             imaxref = sref->deltaEta_.size();
00433             imaxnew = sr->deltaEta_.size();
00434             if(imaxref != imaxnew) {
00435               fout << " deltaEta_ size ref " << imaxref << " now " << imaxnew << std::endl;
00436             }
00437             else {
00438               for(int i = 0; i < imaxref ; i++) {
00439                 if(sref->deltaEta_[i]  != sr->deltaEta_[i]) {
00440                   fout << " deltaEta_[" << i << "] ref "<< sref->deltaEta_[i] 
00441                        << " now " <<sr->deltaEta_[i] << std::endl;
00442                 }
00443               }
00444             }
00445           }
00446         }
00447 
00448         if(sref->deltaPhi_ != sr->deltaPhi_) {
00449           nochange = false;
00450           if(m_debug) {
00451             imaxref = sref->deltaPhi_.size();
00452             imaxnew = sr->deltaPhi_.size();
00453             if(imaxref != imaxnew) {
00454               fout << " deltaPhi size ref " << imaxref << " now " << imaxnew << std::endl;
00455             }
00456             else {
00457               for(int i = 0; i < imaxref ; i++) {
00458                 if(sref->deltaPhi_[i]  != sr->deltaPhi_[i]) {
00459                   fout << " deltaPhi[" << i << "] ref "<< sref->deltaPhi_[i] 
00460                        << " now " <<sr->deltaPhi_[i] << std::endl;
00461                 }
00462               }
00463             }
00464           }
00465         }
00466 
00467         if(sref->ecalDccZs1stSample_ != sr->ecalDccZs1stSample_) {
00468           nochange = false;
00469           if(m_debug) {
00470             imaxref = sref->ecalDccZs1stSample_.size();
00471             imaxnew = sr->ecalDccZs1stSample_.size();
00472             if(imaxref != imaxnew) {
00473               fout << " ecalDccZs1stSample size ref " << imaxref << " now " << imaxnew << std::endl;
00474             }
00475             else {
00476               for(int i = 0; i < imaxref ; i++) {
00477                 if(sref->ecalDccZs1stSample_[i]  != sr->ecalDccZs1stSample_[i]) {
00478                   fout << " ecalDccZs1stSample_[" << i << "] ref "<< sref->ecalDccZs1stSample_[i] 
00479                        << " now " <<sr->ecalDccZs1stSample_[i] << std::endl;
00480                 }
00481               }
00482             }
00483           }
00484         }
00485 
00486         if(sref->ebDccAdcToGeV_  != sr->ebDccAdcToGeV_ || sref->eeDccAdcToGeV_  != sr->eeDccAdcToGeV_) {
00487           nochange = false;
00488           if(m_debug)
00489             fout << " ebDccAdcToGeV ref " << sref->ebDccAdcToGeV_ << " ee " << sref->eeDccAdcToGeV_
00490                  << " now " << sr->ebDccAdcToGeV_ << " ee " << sr->eeDccAdcToGeV_ << std::endl;
00491         }
00492 
00493         if(sref->dccNormalizedWeights_ != sr->dccNormalizedWeights_) {
00494           nochange = false;
00495           if(m_debug) {
00496             imaxref = sref->dccNormalizedWeights_.size();
00497             imaxnew = sr->dccNormalizedWeights_.size();
00498             if(imaxref != imaxnew) {
00499               fout << " dccNormalizedWeights size ref " << imaxref << " now " << imaxnew << std::endl;
00500             }
00501             else {
00502               int i = 0;
00503               for(int ich = 0; ich < 6 ; ich++) {
00504                 if(sref->dccNormalizedWeights_[i][ich]  != sr->dccNormalizedWeights_[i][ich]) {
00505                   fout << " dccNormalizedWeights_[" << i << "][" << ich << "] ref "
00506                        << sref->dccNormalizedWeights_[i][ich] 
00507                        << " now " << sr->dccNormalizedWeights_[i][ich] << std::endl;
00508                 }
00509               }
00510             }
00511           }
00512         }
00513 
00514         if(sref->symetricZS_ != sr->symetricZS_) {
00515           nochange = false;
00516           if(m_debug) {
00517             imaxref = sref->symetricZS_.size();
00518             imaxnew = sr->symetricZS_.size();
00519             if(imaxref != imaxnew) {
00520               fout << " symetricZS size ref " << imaxref << " now " << imaxnew << std::endl;
00521             }
00522             else {
00523               for(int i = 0; i < imaxref ; i++) {
00524                 if(sref->symetricZS_[i]  != sr->symetricZS_[i]) {
00525                   fout << " symetricZS[" << i << "] ref "<< sref->symetricZS_[i] 
00526                        << " now " <<sr->symetricZS_[i] << std::endl;
00527                 }
00528               }
00529             }
00530           }
00531         }
00532 
00533         if(sref->srpLowInterestChannelZS_ != sr->srpLowInterestChannelZS_) {
00534           nochange = false;
00535           if(m_debug) {
00536             imaxref = sref->srpLowInterestChannelZS_.size();
00537             imaxnew = sr->srpLowInterestChannelZS_.size();
00538             if(imaxref != imaxnew) {
00539               fout << " srpLowInterestChannelZS size ref " << imaxref << " now " << imaxnew << std::endl;
00540             }
00541             else {
00542               for(int i = 0; i < imaxref ; i++) {
00543                 if(sref->srpLowInterestChannelZS_[i]  != sr->srpLowInterestChannelZS_[i]) {
00544                   fout << " srpLowInterestChannelZS[" << i << "] ref "<< sref->srpLowInterestChannelZS_[i] 
00545                        << " now " <<sr->srpLowInterestChannelZS_[i] << std::endl;
00546                 }
00547               }
00548             }
00549           }
00550         }
00551 
00552         if(sref->srpHighInterestChannelZS_ != sr->srpHighInterestChannelZS_) {
00553           nochange = false;
00554           if(m_debug) {
00555             imaxref = sref->srpHighInterestChannelZS_.size();
00556             imaxnew = sr->srpHighInterestChannelZS_.size();
00557             if(imaxref != imaxnew) {
00558               fout << " srpHighInterestChannelZS size ref " << imaxref << " now " << imaxnew << std::endl;
00559             }
00560             else {
00561               for(int i = 0; i < imaxref ; i++) {
00562                 if(sref->srpHighInterestChannelZS_[i]  != sr->srpHighInterestChannelZS_[i]) {
00563                   fout << " srpHighInterestChannelZS[" << i << "] ref "<< sref->srpHighInterestChannelZS_[i] 
00564                        << " now " <<sr->srpHighInterestChannelZS_[i] << std::endl;
00565                 }
00566               }
00567             }
00568           }
00569         }
00570 
00571         if(sref->actions_ != sr->actions_) {
00572           nochange = false;
00573           if(m_debug) {
00574             for(int i = 0; i < 4; i++) {
00575               if(sref->actions_[i] != sr->actions_[i]) {
00576                 fout << " actions " << i <<" ref " << sref->actions_[i] 
00577                      << " now " << sr->actions_[i] << std::endl;
00578               }
00579             }
00580           }
00581         }
00582 
00583         if(sref->tccMasksFromConfig_ != sr->tccMasksFromConfig_) {
00584           nochange = false;
00585           if(m_debug) {
00586             for(int i = 0; i < 108; i++) {
00587               if(sref->tccMasksFromConfig_[i] != sr->tccMasksFromConfig_[i]) {
00588                 fout << " tccMasks " << i << " ref " << sref->tccMasksFromConfig_[i] 
00589                      << " now " << sr->tccMasksFromConfig_[i] << std::endl;
00590               }
00591             }
00592           }
00593         }
00594 
00595         if(sref->srpMasksFromConfig_ != sr->srpMasksFromConfig_) {
00596           nochange = false;
00597           if(m_debug) {
00598             for(int i = 0; i < 12; i++) {
00599               for(int ich = 0; ich < 8; ich++) {
00600                 if(sref->srpMasksFromConfig_[i][ich] != sr->srpMasksFromConfig_[i][ich]) {
00601                   fout << " srpMasks " << i << " " << ich << " ref " << sref->srpMasksFromConfig_[i][ich] 
00602                        << " now " << sr->srpMasksFromConfig_[i][ich] << std::endl;
00603                 }
00604               }
00605             }
00606           }
00607         }
00608 
00609         if(sref->dccMasks_ != sr->dccMasks_) {
00610           nochange = false;
00611           if(m_debug) {
00612             for(int i = 0; i < 54; i++) {
00613               if(sref->dccMasks_[i] != sr->dccMasks_[i]) {
00614                 fout << " dccMasks " << i << " ref " << sref->dccMasks_[i] 
00615                      << " now " << sr->dccMasks_[i] << std::endl;
00616               }
00617             }
00618           }
00619         }
00620 
00621         if(sref->srfMasks_ != sr->srfMasks_) {
00622           nochange = false;
00623           if(m_debug) {
00624             for(int i = 0; i < 12; i++) {
00625               if(sref->srfMasks_[i] != sr->srfMasks_[i]) {
00626                 fout << " srfMasks " << i << " ref " << sref->srfMasks_[i] 
00627                      << " now " << sr->srfMasks_[i] << std::endl;
00628               }
00629             }
00630           }
00631         }
00632 
00633         if(sref->substitutionSrfs_ != sr->substitutionSrfs_) {
00634           nochange = false;
00635           if(m_debug) {
00636             for(int i = 0; i < 12; i++) {
00637               for(int ich = 0; ich < 68; ich++) {
00638                 if(sref->substitutionSrfs_[i][ich] != sr->substitutionSrfs_[i][ich]) {
00639                   fout << " substitutionSrfs " << i << " " << ich << " ref " << sref->substitutionSrfs_[i][ich] 
00640                        << " now " << sr->substitutionSrfs_[i][ich] << std::endl;
00641                 }
00642               }
00643             }
00644           }
00645         }
00646 
00647         if(sref->testerTccEmuSrpIds_ != sr->testerTccEmuSrpIds_) {
00648           nochange = false;
00649           if(m_debug) {
00650             for(int i = 0; i < 12; i++) {
00651               fout << " testerTccEmuSrpIds " << i << " ref " << sref->testerTccEmuSrpIds_[i] 
00652                    << " now " << sr->testerTccEmuSrpIds_[i] << std::endl;
00653             }
00654           }
00655         }
00656 
00657         if(sref->testerSrpEmuSrpIds_ != sr->testerSrpEmuSrpIds_) {
00658           nochange = false;
00659           if(m_debug)
00660             for(int i = 0; i < 12; i++) {
00661               fout << " testerSrpEmuSrpIds " << i << " ref " << sref->testerSrpEmuSrpIds_[i] 
00662                    << " now " << sr->testerSrpEmuSrpIds_[i] << std::endl;
00663             }     
00664         }
00665 
00666         if(sref->testerDccTestSrpIds_ != sr->testerDccTestSrpIds_) {
00667           nochange = false;
00668           if(m_debug)
00669             for(int i = 0; i < 12; i++) {
00670               fout << " testerDccTestSrpIds " << i << " ref " << sref->testerDccTestSrpIds_[i] 
00671                    << " now " << sr->testerDccTestSrpIds_[i] << std::endl;
00672             }
00673         }
00674 
00675         if(sref->testerSrpTestSrpIds_ != sr->testerSrpTestSrpIds_) {
00676           nochange = false;
00677           if(m_debug)
00678             for(int i = 0; i < 12; i++) {
00679               fout << " testerSrpTestSrpIds " << i << " ref " << sref->testerSrpTestSrpIds_[i] 
00680                    << " now " << sr->testerSrpTestSrpIds_[i] << std::endl;
00681             }
00682         }
00683 
00684         if(sref->bxOffsets_ != sr->bxOffsets_) {
00685           nochange = false;
00686           if(m_debug)
00687             for(int i = 0; i < 12; i++) {
00688               fout << " bxOffsets " << i << " ref " << sref->bxOffsets_[i] 
00689                    << " now " << sr->bxOffsets_[i] << std::endl;
00690             }
00691         }
00692 
00693         if(sref->bxGlobalOffset_ != sr->bxGlobalOffset_) {
00694           nochange = false;
00695           if(m_debug)
00696             fout << " bxGlobalOffset ref " << sr->bxGlobalOffset_ 
00697                  << " now " << sr->bxGlobalOffset_ << std::endl;
00698         }
00699 
00700         if(sref->automaticMasks_ != sr->automaticMasks_) {
00701           nochange = false;
00702           if(m_debug)
00703             fout << " automaticMasks ref " << sref->automaticMasks_ 
00704                  << " now " << sr->automaticMasks_ << std::endl;
00705         }
00706 
00707         if(sref->automaticSrpSelect_ != sref->automaticSrpSelect_) {
00708           nochange = false;
00709           if(m_debug)
00710             fout << " automaticSrpSelect ref " << sref->automaticSrpSelect_ 
00711                  << " now " << sr->automaticSrpSelect_ << std::endl;
00712         }
00713 
00714         if(nochange) {
00715           if(m_debug)
00716             fout << " no change has been found " << std::endl;
00717           std::ostringstream ss; 
00718           ss << "Run=" << irun << "_SRPunchanged_"<<std::endl; 
00719           m_userTextLog = ss.str()+";";
00720         }
00721         else {
00722           if(m_debug) {
00723             fout << " Change has been found !\n   New payload :"  << std::endl;
00724             PrintPayload(*sr, fout);
00725           }
00726           ChangePayload(*sref, *sr);  // new reference
00727           // write the new payload to ORCON/ORCOFF
00728           EcalSRSettings* srp_pop = new EcalSRSettings();
00729           ChangePayload(*srp_pop, *sr);  // add this payload to DB
00730           m_to_transfer.push_back(std::make_pair(srp_pop, irun));
00731 
00732           std::ostringstream ss; 
00733           ss << "Run=" << irun << "_SRPchanged_"<<std::endl; 
00734           m_userTextLog = ss.str()+";";
00735 
00736           if(m_to_transfer.size() >= 20 ) break;
00737         }
00738         delete sr;
00739         m_i_tag = myconfig_tag;
00740         m_i_version = myconfig_version;
00741         fe_conf_id_old = fe_conf_id;
00742       }  // different tag or version or fe config
00743     }  // loop over runs
00744   }  // test on number of runs
00745 
00746   // disconnect from DB 
00747   delete econn;
00748   fout.close();
00749 
00750 }
00751 
00752 void popcon::EcalSRPHandler::importDccConfigFile(EcalSRSettings& sr, const std::string& filename, bool useCLOB){
00753   XMLPlatformUtils::Initialize();
00754 
00755   XercesDOMParser* parser = new XercesDOMParser;
00756   parser->setValidationScheme( XercesDOMParser::Val_Never );
00757   parser->setDoNamespaces( false );
00758   parser->setDoSchema( false );
00759   
00760   parser->parse(filename.c_str());
00761 
00762   DOMDocument* xmlDoc = parser->getDocument();
00763   if (!xmlDoc) {
00764     std::cout << "importDccConfigFile Error parsing document" << std::endl;
00765   }
00766 
00767   DOMElement* element = xmlDoc->getDocumentElement();
00768   std::string type = toNative(element->getTagName());
00769 
00770   // 1st level
00771   int EBEE = -1;
00772   int L1ZS[2] = {0, 0}, L2ZS[2] = {0, 0};
00773   for (DOMNode* childNode = element->getFirstChild(); 
00774        childNode; childNode = childNode->getNextSibling()) {    
00775     if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
00776       const std::string foundName = toNative(childNode->getNodeName());
00777       DOMElement *child = static_cast<DOMElement *>(childNode);
00778       DOMNamedNodeMap *attributes = child->getAttributes();
00779       unsigned int numAttributes = attributes->getLength();
00780       for (unsigned int j = 0; j < numAttributes; ++j) {
00781         DOMNode *attributeNode = attributes->item(j);
00782         DOMAttr *attribute = static_cast<DOMAttr *>(attributeNode);
00783         const std::string info = toNative(attribute->getName());
00784         const std::string scope = toNative(attribute->getValue());
00785         if(info == "_scope") {
00786           if(scope.substr(0,2) == "EE") EBEE = 1;
00787           else EBEE = 0;
00788         }
00789       }
00790       // 2nd level
00791       for (DOMNode* subchildNode = childNode->getFirstChild(); 
00792            subchildNode; subchildNode = subchildNode->getNextSibling()) {    
00793         if (subchildNode->getNodeType() == DOMNode::ELEMENT_NODE) {
00794           const std::string subName = toNative(subchildNode->getNodeName());
00795           // 3rd level
00796           for (DOMNode* subsubchildNode = subchildNode->getFirstChild(); 
00797                subsubchildNode; subsubchildNode = subsubchildNode->getNextSibling()) {    
00798             if (subsubchildNode->getNodeType() == DOMNode::ELEMENT_NODE) {
00799               const std::string subName = toNative(subsubchildNode->getNodeName());
00800               if(subName == "L1ZSUPPRESSION")
00801                 GetNodeData(subsubchildNode,L1ZS[EBEE]);
00802               if(subName == "L2ZSUPPRESSION")
00803                 GetNodeData(subsubchildNode,L2ZS[EBEE]);
00804               if(subName == "FIRSTZSSAMPLE") {
00805                 int ZS;
00806                 GetNodeData(subsubchildNode,ZS);
00807                 if(useCLOB) sr.ecalDccZs1stSample_.push_back(ZS);
00808               }
00809               if(subName == "CXTALWEIGHTS") {
00810                 std::vector<float> dcc(6);
00811                 float w;
00812                 for(int iw = 0; iw < 6; iw++) {
00813                   GetNodeData(subsubchildNode,w);
00814                   dcc.push_back( w );
00815                 }
00816                 if(useCLOB) sr.dccNormalizedWeights_.push_back(dcc);  // vector vector
00817               }
00818             }
00819           }  // loop over subsubchild 
00820         }
00821       }  // loop over subchild 
00822     }
00823   }  // loop over child 
00824   sr.srpLowInterestChannelZS_.push_back(L1ZS[0]*0.035/4);  //  EB
00825   sr.srpLowInterestChannelZS_.push_back(L1ZS[1]*0.060/4);  //  EE
00826   sr.srpHighInterestChannelZS_.push_back(L2ZS[0]*0.035/4);  //  EB
00827   sr.srpHighInterestChannelZS_.push_back(L2ZS[1]*0.060/4);  //  EE
00828   delete parser;
00829   XMLPlatformUtils::Terminate();
00830 }
00831 
00832 void popcon::EcalSRPHandler::PrintPayload(EcalSRSettings& sr, std::ofstream& fout){
00833   int imax = sr.deltaEta_.size();
00834   fout << " deltaEta[" << imax << "] ";
00835   for(int i = 0; i < imax ; i++) {
00836     fout << sr.deltaEta_[i] << " ";
00837   }
00838   fout << std::endl;
00839 
00840   imax = sr.deltaPhi_.size();
00841   fout << " deltaPhi[" << imax << "] ";
00842   for(int i = 0; i < imax ; i++) {
00843     fout << sr.deltaPhi_[i] << " ";
00844   }
00845   fout << std::endl;
00846 
00847   imax = sr.ecalDccZs1stSample_.size();
00848   fout << " ecalDccZs1stSample[" << imax << "] ";
00849   for(int i = 0; i < imax ; i++) {
00850     fout << sr.ecalDccZs1stSample_[i] << " ";
00851   }
00852   fout << std::endl;
00853 
00854   fout << " ebDccAdcToGeV " << sr.ebDccAdcToGeV_ << std::endl;
00855 
00856   fout << " eeDccAdcToGeV " << sr.eeDccAdcToGeV_ << std::endl;
00857 
00858   fout << " dccNormalizedWeights" << std::endl;
00859   for(int i = 0; i < (int)sr.dccNormalizedWeights_.size(); ++i){
00860     fout << " Channel " << i;
00861     for(int j = 0; j < (int)sr.dccNormalizedWeights_[i].size(); ++j) 
00862       fout  << " " << sr.dccNormalizedWeights_[i][j];
00863     fout << std::endl;
00864   }
00865 
00866   imax = sr.symetricZS_.size();
00867   fout << " symetricZS["<< imax << "] ";
00868   for(int i = 0; i < imax ; i++) {
00869     fout << sr.symetricZS_[i] << " ";
00870   }
00871   fout << std::endl;
00872 
00873   imax = sr.srpLowInterestChannelZS_.size();
00874   fout << " srpLowInterestChannelZS["<< imax << "] ";
00875   for(int i = 0; i < imax ; i++) {
00876     fout << sr.srpLowInterestChannelZS_[i] << " ";
00877   }
00878   fout << std::endl;
00879 
00880   imax = sr.srpHighInterestChannelZS_.size();
00881   fout << " srpHighInterestChannelZS["<< imax << "] ";
00882   for(int i = 0; i < imax ; i++) {
00883     fout << sr.srpHighInterestChannelZS_[i] << " ";
00884   }
00885   fout << std::endl;
00886 
00887   imax = sr.actions_.size();
00888   fout << " actions["<< imax << "] ";
00889   for(int i = 0; i < imax ; i++) {
00890     fout << sr.actions_[i] << " ";
00891   }
00892   fout << std::endl;
00893 
00894   imax = sr.tccMasksFromConfig_.size();
00895   fout << " tccMasksFromConfig["<< imax << "] ";
00896   for(int i = 0; i < imax ; i++) {
00897     fout << sr.tccMasksFromConfig_[i] << " ";
00898   }
00899   fout << std::endl;
00900 
00901   fout << " srpMasksFromConfig" << std::endl;
00902   for(int i = 0; i < (int)sr.srpMasksFromConfig_.size(); ++i){
00903     for(int j = 0; j < (int)sr.srpMasksFromConfig_[i].size(); ++j) 
00904       fout << sr.srpMasksFromConfig_[i][j] << " ";
00905     fout << std::endl;
00906   }
00907 
00908   imax = sr.dccMasks_.size();
00909   fout << " dccMasks[" << imax << "] ";
00910   for(int i = 0; i < imax ; i++) {
00911     fout << sr.dccMasks_[i] << " ";
00912   }
00913   fout << std::endl;
00914 
00915   imax = sr.srfMasks_.size();
00916   fout << " srfMasks[" << imax << "] ";
00917   for(int i = 0; i < imax ; i++) {
00918     fout << sr.srfMasks_[i] << " ";
00919   }
00920   fout << std::endl;
00921 
00922   fout << "substitutionSrfs" << std::endl;
00923   for(int i = 0; i < (int)sr.substitutionSrfs_.size(); ++i){
00924     for(int j = 0; j < (int)sr.substitutionSrfs_[i].size(); ++j) 
00925       fout << sr.substitutionSrfs_[i][j] << " ";
00926     fout << std::endl;
00927   }
00928 
00929   imax = sr.testerTccEmuSrpIds_.size();
00930   fout << " testerTccEmuSrpIds[" << imax << "] ";
00931   for(int i = 0; i < imax ; i++) {
00932     fout << sr.testerTccEmuSrpIds_[i] << " ";
00933   }
00934   fout << std::endl;
00935 
00936   imax = sr.testerSrpEmuSrpIds_.size();
00937   fout << " testerSrpEmuSrpIds[" << imax << "] ";
00938   for(int i = 0; i < imax ; i++) {
00939     fout << sr.testerSrpEmuSrpIds_[i] << " ";
00940   }
00941   fout << std::endl;
00942 
00943   imax = sr.testerDccTestSrpIds_.size();
00944   fout << " testerDccTestSrpIds[" << imax << "] ";
00945   for(int i = 0; i < imax ; i++) {
00946     fout << sr.testerDccTestSrpIds_[i] << " ";
00947   }
00948   fout << std::endl;
00949 
00950   imax = sr. testerSrpTestSrpIds_.size();
00951   fout << " testerSrpTestSrpIds[" << imax << "] ";
00952   for(int i = 0; i < imax ; i++) {
00953     fout << sr.testerSrpTestSrpIds_[i] << " ";
00954   }
00955   fout << std::endl;
00956 
00957   imax = sr.bxOffsets_.size();
00958   fout << " bxOffsets[" << imax << "] ";
00959   for(int i = 0; i < imax ; i++) {
00960     fout << sr.bxOffsets_[i] << " ";
00961   }
00962   fout << std::endl;
00963 
00964   fout << " bxGlobalOffset " << sr.bxGlobalOffset_ << std::endl;
00965   fout << " automaticMasks " << sr.automaticMasks_ << std::endl;
00966   fout << " automaticSrpSelect " << sr.automaticSrpSelect_ << std::endl;
00967 }
00968 
00969 void popcon::EcalSRPHandler::ChangePayload(EcalSRSettings& sref, EcalSRSettings& sr ){
00970   sref.deltaEta_ =  sr.deltaEta_;
00971   sref.deltaPhi_ = sr.deltaPhi_;
00972   sref.ecalDccZs1stSample_ = sr.ecalDccZs1stSample_;
00973   sref.ebDccAdcToGeV_ = sr.ebDccAdcToGeV_;
00974   sref.eeDccAdcToGeV_ = sr.eeDccAdcToGeV_;
00975   sref.dccNormalizedWeights_ = sr.dccNormalizedWeights_;
00976   sref.symetricZS_ = sr.symetricZS_;
00977   sref.srpLowInterestChannelZS_ = sr.srpLowInterestChannelZS_;
00978   sref.srpHighInterestChannelZS_ = sr.srpHighInterestChannelZS_;
00979   sref.actions_ = sr.actions_;
00980   sref.tccMasksFromConfig_ = sr.tccMasksFromConfig_;
00981   sref.srpMasksFromConfig_ = sr.srpMasksFromConfig_;
00982   sref.dccMasks_ = sr.dccMasks_;
00983   sref.srfMasks_ = sr.srfMasks_;
00984   sref.substitutionSrfs_ = sr.substitutionSrfs_;
00985   sref.testerTccEmuSrpIds_ = sr.testerTccEmuSrpIds_;
00986   sref.testerSrpEmuSrpIds_ = sr.testerSrpEmuSrpIds_;
00987   sref.testerDccTestSrpIds_ = sr.testerDccTestSrpIds_;
00988   sref.testerSrpTestSrpIds_ = sr.testerSrpTestSrpIds_;
00989   sref.bxOffsets_ = sr.bxOffsets_;
00990   sref.bxGlobalOffset_ = sr.bxGlobalOffset_;
00991   sref.automaticMasks_ = sr.automaticMasks_;
00992   sref.automaticSrpSelect_ = sr.automaticSrpSelect_;
00993 }