Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
0002 
0003 #include "CondTools/Ecal/interface/EcalPedestalsHandler.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0005 
0006 #include <cstring>
0007 #include <iostream>
0008 #include <fstream>
0009 
0010 #include "TFile.h"
0011 #include "TTree.h"
0012 
0013 const Int_t kChannels = 75848, kEBChannels = 61200, kEEChannels = 14648, kGains = 3;
0014 const Int_t Nbpedxml = 81;  // Number of Gain1 Gain6 files in 2016 (26), 2017 (25), 2018 (30) : L1248 and L1600
0015 const Int_t gainValues[kGains] = {12, 6, 1};
0016 
0017 popcon::EcalPedestalsHandler::EcalPedestalsHandler(const edm::ParameterSet& ps)
0018     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalPedestalsHandler")) {
0019   edm::LogInfo("EcalPedestals Source handler constructor\n");
0020   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0021   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0022   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0023   m_user = ps.getParameter<std::string>("OnlineDBUser");
0024   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0025   m_locationsource = ps.getParameter<std::string>("LocationSource");
0026   m_location = ps.getParameter<std::string>("Location");
0027   m_gentag = ps.getParameter<std::string>("GenTag");
0028   m_runtag = ps.getParameter<std::string>("RunTag");
0029   m_filename = ps.getUntrackedParameter<std::string>("filename", "EcalPedestals.txt");
0030   m_runtype = ps.getUntrackedParameter<int>("RunType", 1);
0031   m_corrected = ps.getUntrackedParameter<bool>("corrected", false);
0032 
0033   edm::LogInfo("EcalPedestalsHandler") << m_sid << "/"
0034                                        << "/" << m_location << "/" << m_gentag;
0035 }
0036 
0037 popcon::EcalPedestalsHandler::~EcalPedestalsHandler() {}
0038 
0039 void popcon::EcalPedestalsHandler::getNewObjects() {
0040   edm::LogInfo("------- Ecal - > getNewObjects\n");
0041   if (m_locationsource == "P5") {
0042     getNewObjectsP5();
0043   } else if (m_locationsource == "H2") {
0044     getNewObjectsH2();
0045   } else if (m_locationsource == "File") {
0046     readPedestalFile();
0047   } else if (m_locationsource == "MC") {
0048     readPedestalMC();
0049   } else if (m_locationsource == "2017") {
0050     readPedestal2017();
0051   } else if (m_locationsource == "Tree") {
0052     readPedestalTree();
0053   } else if (m_locationsource == "Timestamp") {
0054     readPedestalTimestamp();
0055   } else {
0056     edm::LogInfo(" unknown location ") << m_locationsource << " give up ";
0057     exit(-1);
0058   }
0059 }
0060 
0061 bool popcon::EcalPedestalsHandler::checkPedestal(EcalPedestals::Item* item) {
0062   // true means all is standard and OK
0063   bool result = true;
0064   if (item->rms_x12 > 3 || item->rms_x12 <= 0)
0065     result = false;
0066   if (item->rms_x6 > 2 || item->rms_x6 <= 0)
0067     result = false;
0068   if (item->rms_x1 > 1 || item->rms_x1 <= 0)
0069     result = false;
0070   if (item->mean_x12 > 300 || item->mean_x12 <= 100)
0071     result = false;
0072   if (item->mean_x1 > 300 || item->mean_x1 <= 100)
0073     result = false;
0074   if (item->mean_x6 > 300 || item->mean_x6 <= 100)
0075     result = false;
0076   return result;
0077 }
0078 
0079 ////////////////////////////////////////////////////////////////////////////////////
0080 
0081 void popcon::EcalPedestalsHandler::getNewObjectsP5() {
0082   std::ostringstream ss;
0083   ss << "ECAL ";
0084 
0085   unsigned int max_since = 0;
0086   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0087   edm::LogInfo("max_since : ") << max_since;
0088   Ref ped_db = lastPayload();
0089 
0090   // we copy the last valid record to a temporary object peds
0091   EcalPedestals* peds = new EcalPedestals();
0092   edm::LogInfo("retrieved last payload ");
0093 
0094   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
0095     if (iEta == 0)
0096       continue;
0097     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
0098       // make an EBDetId since we need EBDetId::rawId() to be used as the key for the pedestals
0099       if (EBDetId::validDetId(iEta, iPhi)) {
0100         EBDetId ebdetid(iEta, iPhi, EBDetId::ETAPHIMODE);
0101         EcalPedestals::const_iterator it = ped_db->find(ebdetid.rawId());
0102         EcalPedestals::Item aped = (*it);
0103 
0104         // here I copy the last valid value in the peds object
0105         EcalPedestals::Item item;
0106         item.mean_x1 = aped.mean_x1;
0107         item.rms_x1 = aped.rms_x1;
0108         item.mean_x6 = aped.mean_x6;
0109         item.rms_x6 = aped.rms_x6;
0110         item.mean_x12 = aped.mean_x12;
0111         item.rms_x12 = aped.rms_x12;
0112 
0113         peds->insert(std::make_pair(ebdetid.rawId(), item));
0114       }
0115     }
0116   }
0117 
0118   for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0119     for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0120       // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
0121       if (EEDetId::validDetId(iX, iY, 1)) {
0122         EEDetId eedetidpos(iX, iY, 1);
0123 
0124         EcalPedestals::const_iterator it = ped_db->find(eedetidpos.rawId());
0125         EcalPedestals::Item aped = (*it);
0126 
0127         //  unsigned int hiee = eedetidpos.hashedIndex();
0128         //  EcalPedestals::Item aped= ped_db->endcap(hiee);
0129 
0130         // here I copy the last valid value in the peds object
0131         EcalPedestals::Item item;
0132         item.mean_x1 = aped.mean_x1;
0133         item.rms_x1 = aped.rms_x1;
0134         item.mean_x6 = aped.mean_x6;
0135         item.rms_x6 = aped.rms_x6;
0136         item.mean_x12 = aped.mean_x12;
0137         item.rms_x12 = aped.rms_x12;
0138         peds->insert(std::make_pair(eedetidpos.rawId(), item));
0139       }
0140       if (EEDetId::validDetId(iX, iY, -1)) {
0141         EEDetId eedetidneg(iX, iY, -1);
0142 
0143         EcalPedestals::const_iterator it = ped_db->find(eedetidneg.rawId());
0144         EcalPedestals::Item aped = (*it);
0145         //     unsigned int hiee = eedetidneg.hashedIndex();
0146         //     EcalPedestals::Item aped= ped_db->endcap(hiee);
0147 
0148         // here I copy the last valid value in the peds object
0149         EcalPedestals::Item item;
0150         item.mean_x1 = aped.mean_x1;
0151         item.rms_x1 = aped.rms_x1;
0152         item.mean_x6 = aped.mean_x6;
0153         item.rms_x6 = aped.rms_x6;
0154         item.mean_x12 = aped.mean_x12;
0155         item.rms_x12 = aped.rms_x12;
0156         peds->insert(std::make_pair(eedetidneg.rawId(), item));
0157       }
0158     }
0159   }
0160 
0161   // here we retrieve all the runs after the last from online DB
0162 
0163   edm::LogInfo("Retrieving run list from ONLINE DB ... ");
0164   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0165   edm::LogInfo("Connection done");
0166 
0167   // these are the online conditions DB classes
0168   RunList my_runlist;
0169   RunTag my_runtag;
0170   LocationDef my_locdef;
0171   RunTypeDef my_rundef;
0172 
0173   my_locdef.setLocation(m_location);
0174   //    my_rundef.setRunType("PEDESTAL");  // 2017
0175   my_rundef.setRunType(m_runtag);
0176   my_runtag.setLocationDef(my_locdef);
0177   my_runtag.setRunTypeDef(my_rundef);
0178   my_runtag.setGeneralTag(m_gentag);
0179 
0180   // here we retrieve the Monitoring run records
0181 
0182   MonVersionDef monverdef;
0183   monverdef.setMonitoringVersion("test01");
0184 
0185   MonRunTag mon_tag;
0186   //    mon_tag.setGeneralTag("CMSSW");
0187   mon_tag.setGeneralTag("CMSSW-offline-private");
0188   mon_tag.setMonVersionDef(monverdef);
0189   MonRunList mon_list;
0190   mon_list.setMonRunTag(mon_tag);
0191   mon_list.setRunTag(my_runtag);
0192   //    mon_list=econn->fetchMonRunList(my_runtag, mon_tag);
0193   unsigned int min_run = 0;
0194   if (m_firstRun < max_since) {
0195     min_run = max_since + 1;  // we have to add 1 to the last transferred one
0196   } else {
0197     min_run = m_firstRun;
0198   }
0199 
0200   unsigned int max_run = m_lastRun;
0201   mon_list = econn->fetchMonRunList(my_runtag, mon_tag, min_run, max_run);
0202 
0203   std::vector<MonRunIOV> mon_run_vec = mon_list.getRuns();
0204   int mon_runs = mon_run_vec.size();
0205   edm::LogInfo("number of Mon runs is : ") << mon_runs;
0206 
0207   if (mon_runs > 0) {
0208     int krmax = std::min(mon_runs, 30);
0209     for (int kr = 0; kr < krmax; kr++) {
0210       edm::LogInfo("-kr------:  ") << kr;
0211 
0212       unsigned int irun = static_cast<unsigned int>(mon_run_vec[kr].getRunIOV().getRunNumber());
0213       edm::LogInfo("retrieve the data for run number: ") << mon_run_vec[kr].getRunIOV().getRunNumber();
0214       if (mon_run_vec[kr].getSubRunNumber() <= 1) {
0215         // retrieve the data for a given run
0216         RunIOV runiov_prime = mon_run_vec[kr].getRunIOV();
0217         // retrieve the pedestals from OMDS for this run
0218         std::map<EcalLogicID, MonPedestalsDat> dataset_mon;
0219         econn->fetchDataSet(&dataset_mon, &mon_run_vec[kr]);
0220         edm::LogInfo("OMDS record for run ") << irun << " is made of " << dataset_mon.size();
0221         int nEB = 0, nEE = 0, nEBbad = 0, nEEbad = 0;
0222         typedef std::map<EcalLogicID, MonPedestalsDat>::const_iterator CImon;
0223         EcalLogicID ecid_xt;
0224         MonPedestalsDat rd_ped;
0225 
0226         // this to validate ...
0227         int nbad = 0;
0228         for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
0229           ecid_xt = p->first;
0230           rd_ped = p->second;
0231           int sm_num = ecid_xt.getID1();
0232           int xt_num = ecid_xt.getID2();
0233           int yt_num = ecid_xt.getID3();
0234 
0235           EcalPedestals::Item item;
0236           item.mean_x1 = rd_ped.getPedMeanG1();
0237           item.rms_x1 = rd_ped.getPedRMSG1();
0238           item.mean_x6 = rd_ped.getPedMeanG6();
0239           item.rms_x6 = rd_ped.getPedRMSG6();
0240           item.mean_x12 = rd_ped.getPedMeanG12();
0241           item.rms_x12 = rd_ped.getPedRMSG12();
0242           /*    
0243       if(irun != 280216 && irun != 280218 && irun != 280259 && irun != 280261 && irun != 280392 && irun != 280394
0244          && irun != 280762 && irun != 280765 && irun != 280936 && irun != 280939 && irun != 281756 && irun != 281757) {
0245       */
0246           if (ecid_xt.getName() == "EB_crystal_number") {
0247             nEB++;
0248             if (!checkPedestal(&item))
0249               nEBbad++;
0250           } else {
0251             nEE++;
0252             if (!checkPedestal(&item))
0253               nEEbad++;
0254           }
0255 
0256           // here we check and count how many bad channels we have
0257 
0258           if (!checkPedestal(&item)) {
0259             nbad++;
0260             if (nbad < 10)
0261               std::cout << "BAD LIST: channel " << sm_num << "/" << xt_num << "/" << yt_num << "ped/rms "
0262                         << item.mean_x12 << "/" << item.rms_x12 << std::endl;
0263           }
0264           /*
0265       }
0266       else {      // only gain 12 in these runs
0267         if(ecid_xt.getName()=="EB_crystal_number") {
0268           nEB++;
0269           if(item.mean_x12 <= 100 || item.mean_x12 > 300 || item.rms_x12 > 3 || item.rms_x12 <= 0) {
0270         nbad++;
0271         nEBbad++;
0272           }
0273         }
0274         else {
0275           nEE++;
0276           if(item.mean_x12 <= 100 || item.mean_x12 > 300 || item.rms_x12 > 3 || item.rms_x12 <= 0) {
0277         nbad++;
0278         nEEbad++;
0279           }
0280         }
0281       }    //    only gain 12 in these runs
0282         */
0283         }  // loop over all channels
0284 
0285         // ok or bad?
0286         // a bad run is for more than 5% bad channels
0287 
0288         //        if(nbad<(dataset_mon.size()*0.1)){
0289         if (nbad < (dataset_mon.size() * 0.05) && (nEB > 10200 || nEE > 2460)) {
0290           for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
0291             ecid_xt = p->first;
0292             rd_ped = p->second;
0293             int sm_num = ecid_xt.getID1();
0294             int xt_num = ecid_xt.getID2();
0295             int yt_num = ecid_xt.getID3();
0296 
0297             EcalPedestals::Item item;
0298             item.mean_x1 = rd_ped.getPedMeanG1();
0299             item.rms_x1 = rd_ped.getPedRMSG1();
0300             item.mean_x6 = rd_ped.getPedMeanG6();
0301             item.rms_x6 = rd_ped.getPedRMSG6();
0302             item.mean_x12 = rd_ped.getPedMeanG12();
0303             item.rms_x12 = rd_ped.getPedRMSG12();
0304 
0305             if (ecid_xt.getName() == "EB_crystal_number") {
0306               // Barrel channel
0307               EBDetId ebdetid(sm_num, xt_num, EBDetId::SMCRYSTALMODE);
0308 
0309               // individual objects check
0310               if (item.mean_x1 == -1 || item.rms_x1 == -1 || item.mean_x6 == -1 || item.rms_x6 == -1 ||
0311                   item.mean_x12 == -1 || item.rms_x12 == -1 || item.mean_x1 == 0 || item.rms_x1 == 0 ||
0312                   item.mean_x6 == 0 || item.rms_x6 == 0 || item.mean_x12 == 0 || item.rms_x12 == 0) {
0313                 // if one is bad we
0314                 // retrieve the old valid value
0315                 unsigned int hieb = ebdetid.hashedIndex();
0316                 EcalPedestals::Item previous_ped = peds->barrel(hieb);
0317                 if (item.mean_x1 == -1 || item.mean_x1 == 0)
0318                   item.mean_x1 = previous_ped.mean_x1;
0319                 if (item.rms_x1 == -1 || item.rms_x1 == 0)
0320                   item.rms_x1 = previous_ped.rms_x1;
0321                 if (item.mean_x6 == -1 || item.mean_x6 == 0)
0322                   item.mean_x6 = previous_ped.mean_x6;
0323                 if (item.rms_x6 == -1 || item.rms_x6 == 0)
0324                   item.rms_x6 = previous_ped.rms_x6;
0325                 if (item.mean_x12 == -1 || item.mean_x12 == 0)
0326                   item.mean_x12 = previous_ped.mean_x12;
0327                 if (item.rms_x12 == -1 || item.rms_x12 == 0)
0328                   item.rms_x12 = previous_ped.rms_x12;
0329               }
0330 
0331               // here we change in the peds object only the channels that are available in the online DB
0332               // otherwise we keep the previous value
0333               peds->insert(std::make_pair(ebdetid.rawId(), item));
0334             } else {
0335               // Endcap channel
0336               // in this case xt_num is x
0337               // yt_num is y and sm_num is the side +/- 1
0338               if (EEDetId::validDetId(xt_num, yt_num, sm_num)) {
0339                 EEDetId eedetid(xt_num, yt_num, sm_num);
0340 
0341                 // individual objects check
0342                 if (item.mean_x1 == -1 || item.rms_x1 == -1 || item.mean_x6 == -1 || item.rms_x6 == -1 ||
0343                     item.mean_x12 == -1 || item.rms_x12 == -1 || item.mean_x1 == 0 || item.rms_x1 == 0 ||
0344                     item.mean_x6 == 0 || item.rms_x6 == 0 || item.mean_x12 == 0 || item.rms_x12 == 0) {
0345                   // if one is bad we
0346                   // retrieve the old valid value
0347                   unsigned int hiee = eedetid.hashedIndex();
0348                   EcalPedestals::Item previous_ped = peds->endcap(hiee);
0349                   if (item.mean_x1 == -1 || item.mean_x1 == 0)
0350                     item.mean_x1 = previous_ped.mean_x1;
0351                   if (item.rms_x1 == -1 || item.rms_x1 == 0)
0352                     item.rms_x1 = previous_ped.rms_x1;
0353                   if (item.mean_x6 == -1 || item.mean_x6 == 0)
0354                     item.mean_x6 = previous_ped.mean_x6;
0355                   if (item.rms_x6 == -1 || item.rms_x6 == 0)
0356                     item.rms_x6 = previous_ped.rms_x6;
0357                   if (item.mean_x12 == -1 || item.mean_x12 == 0)
0358                     item.mean_x12 = previous_ped.mean_x12;
0359                   if (item.rms_x12 == -1 || item.rms_x12 == 0)
0360                     item.rms_x12 = previous_ped.rms_x12;
0361                 }
0362                 // here we change in the peds object only the channels that are available in the online DB
0363                 // otherwise we keep the previous value
0364                 peds->insert(std::make_pair(eedetid.rawId(), item));
0365               }
0366             }
0367           }
0368 
0369           edm::LogInfo("Generating popcon record for run ") << irun << "..." << std::flush;
0370           // now I copy peds in pedtemp and I ship pedtemp to popcon
0371           // if I use always the same peds I always overwrite
0372           // so I really have to create new objects for each new run
0373           // popcon deletes everything for me
0374 
0375           EcalPedestals* pedtemp = new EcalPedestals();
0376           for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
0377             if (iEta == 0)
0378               continue;
0379             for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
0380               // make an EBDetId since we need EBDetId::rawId() to be used as the key for the pedestals
0381               if (EBDetId::validDetId(iEta, iPhi)) {
0382                 EBDetId ebdetid(iEta, iPhi);
0383                 unsigned int hiee = ebdetid.hashedIndex();
0384                 EcalPedestals::Item aped = peds->barrel(hiee);
0385 
0386                 // here I copy the last valid value in the peds object
0387                 EcalPedestals::Item item;
0388                 item.mean_x1 = aped.mean_x1;
0389                 item.rms_x1 = aped.rms_x1;
0390                 item.mean_x6 = aped.mean_x6;
0391                 item.rms_x6 = aped.rms_x6;
0392                 item.mean_x12 = aped.mean_x12;
0393                 item.rms_x12 = aped.rms_x12;
0394                 // here I copy the last valid value in the pedtemp object
0395                 pedtemp->insert(std::make_pair(ebdetid.rawId(), item));
0396                 if ((iEta == -1 || iEta == 1) && iPhi == 20) {
0397                   float x = aped.mean_x12;
0398                   edm::LogInfo("channel:") << iEta << "/" << iPhi << "/" << hiee << " ped mean 12=" << x;
0399                 }
0400               }
0401             }
0402           }
0403           // endcaps
0404           for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0405             for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0406               // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
0407               if (EEDetId::validDetId(iX, iY, 1)) {
0408                 EEDetId eedetid(iX, iY, 1);
0409                 unsigned int hiee = eedetid.hashedIndex();
0410                 EcalPedestals::Item aped = peds->endcap(hiee);
0411                 // here I copy the last valid value in the peds object
0412                 EcalPedestals::Item item;
0413                 item.mean_x1 = aped.mean_x1;
0414                 item.rms_x1 = aped.rms_x1;
0415                 item.mean_x6 = aped.mean_x6;
0416                 item.rms_x6 = aped.rms_x6;
0417                 item.mean_x12 = aped.mean_x12;
0418                 item.rms_x12 = aped.rms_x12;
0419                 // here I copy the last valid value in the pedtemp object
0420                 pedtemp->insert(std::make_pair(eedetid.rawId(), item));
0421               }
0422               if (EEDetId::validDetId(iX, iY, -1)) {
0423                 EEDetId eedetid(iX, iY, -1);
0424                 unsigned int hiee = eedetid.hashedIndex();
0425                 EcalPedestals::Item aped = peds->endcap(hiee);
0426                 // here I copy the last valid value in the peds object
0427                 EcalPedestals::Item item;
0428                 item.mean_x1 = aped.mean_x1;
0429                 item.rms_x1 = aped.rms_x1;
0430                 item.mean_x6 = aped.mean_x6;
0431                 item.rms_x6 = aped.rms_x6;
0432                 item.mean_x12 = aped.mean_x12;
0433                 item.rms_x12 = aped.rms_x12;
0434                 // here I copy the last valid value in the pedtemp object
0435                 pedtemp->insert(std::make_pair(eedetid.rawId(), item));
0436               }
0437             }
0438           }
0439 
0440           Time_t snc = (Time_t)irun;
0441           m_to_transfer.push_back(std::make_pair((EcalPedestals*)pedtemp, snc));
0442 
0443           ss << "Run=" << irun << "_WAS_GOOD_" << std::endl;
0444           m_userTextLog = ss.str() + ";";
0445         }  // good run : write in DB
0446         else {
0447           edm::LogInfo("Run ") << irun << " was BAD !!!! not sent to the DB";
0448           if (nbad >= (dataset_mon.size() * 0.05))
0449             edm::LogInfo(" number of bad channels = ") << nbad;
0450           if (nEB <= 10200)
0451             edm::LogInfo(" number of EB channels = ") << nEB;
0452           if (nEE <= 2440)
0453             edm::LogInfo(" number of EE channels = ") << nEE;
0454           ss << "Run=" << irun << "_WAS_BAD_" << std::endl;
0455           m_userTextLog = ss.str() + ";";
0456         }  //  bad run : do not write in DB
0457       }    //  SubRunNumber
0458     }      // loop over runs
0459 
0460     delete econn;
0461     delete peds;  // this is the only one that popcon does not delete
0462   }               // runs to analyze ?
0463   edm::LogInfo("Ecal - > end of getNewObjects -----------\n");
0464 }
0465 
0466 ////////////////////////////////////////////////////////////////////////////////////
0467 
0468 void popcon::EcalPedestalsHandler::getNewObjectsH2() {
0469   unsigned int max_since = 0;
0470   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0471   edm::LogInfo("max_since : ") << max_since;
0472   Ref ped_db = lastPayload();
0473 
0474   edm::LogInfo("retrieved last payload ");
0475 
0476   // we copy the last valid record to a temporary object peds
0477   EcalPedestals* peds = new EcalPedestals();
0478 
0479   // get from offline DB the last valid pedestal set ped_db
0480   //    edm::ESHandle<EcalPedestals> pedestals;
0481   //    esetup.get<EcalPedestalsRcd>().get(pedestals);
0482 
0483   // only positive endcap side and
0484   // x from 86 to 95
0485   // y from 46 to 55
0486   int ixmin = 86;
0487   int ixmax = 95;
0488   int iymin = 46;
0489   int iymax = 55;
0490   for (int iX = ixmin; iX <= ixmax; ++iX) {
0491     for (int iY = iymin; iY <= iymax; ++iY) {
0492       if (EEDetId::validDetId(iX, iY, 1)) {
0493         EEDetId eedetidpos(iX, iY, 1);
0494         unsigned int hiee = eedetidpos.hashedIndex();
0495         EcalPedestals::Item aped = ped_db->endcap(hiee);
0496 
0497         // here I copy the last valid value in the peds object
0498         EcalPedestals::Item item;
0499         item.mean_x1 = aped.mean_x1;
0500         item.rms_x1 = aped.rms_x1;
0501         item.mean_x6 = aped.mean_x6;
0502         item.rms_x6 = aped.rms_x6;
0503         item.mean_x12 = aped.mean_x12;
0504         item.rms_x12 = aped.rms_x12;
0505         peds->insert(std::make_pair(eedetidpos.rawId(), item));
0506         if (iX == ixmin && iY == iymin)
0507           std::cout << "ped12 " << item.mean_x12 << std::endl;
0508       }
0509     }
0510   }
0511 
0512   edm::LogInfo("We just retrieved the last valid record from DB ");
0513 
0514   // here we retrieve all the runs after the last from online DB
0515 
0516   edm::LogInfo("Retrieving run list from ONLINE DB ... ");
0517 
0518   edm::LogInfo("Making connection...") << std::flush;
0519   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0520   edm::LogInfo("Done.");
0521 
0522   if (!econn) {
0523     edm::LogInfo(" connection parameters ") << m_sid;
0524     throw cms::Exception("OMDS not available");
0525   }
0526 
0527   // these are the online conditions DB classes
0528   RunList my_runlist;
0529   RunTag my_runtag;
0530   LocationDef my_locdef;
0531   RunTypeDef my_rundef;
0532 
0533   my_locdef.setLocation("H2_07");
0534   my_rundef.setRunType("PEDESTAL");
0535   my_runtag.setLocationDef(my_locdef);
0536   my_runtag.setRunTypeDef(my_rundef);
0537   my_runtag.setGeneralTag("LOCAL");
0538 
0539   // here we retrieve the Monitoring run records
0540 
0541   MonVersionDef monverdef;
0542   monverdef.setMonitoringVersion("test01");
0543 
0544   MonRunTag mon_tag;
0545   mon_tag.setGeneralTag("CMSSW");
0546   mon_tag.setMonVersionDef(monverdef);
0547   MonRunList mon_list;
0548   mon_list.setMonRunTag(mon_tag);
0549   mon_list.setRunTag(my_runtag);
0550   //    mon_list=econn->fetchMonRunList(my_runtag, mon_tag);
0551   unsigned int min_run = max_since + 1;  // we have to add 1 to the last transferred one
0552 
0553   unsigned int max_run = m_lastRun;
0554   mon_list = econn->fetchMonRunList(my_runtag, mon_tag, min_run, max_run);
0555 
0556   std::vector<MonRunIOV> mon_run_vec = mon_list.getRuns();
0557   size_t mon_runs = mon_run_vec.size();
0558   edm::LogInfo("number of Mon runs is : ") << mon_runs;
0559 
0560   if (mon_runs > 0) {
0561     for (size_t kr = 0; kr < mon_runs; kr++) {
0562       unsigned int irun = static_cast<unsigned int>(mon_run_vec[kr].getRunIOV().getRunNumber());
0563 
0564       edm::LogInfo("here is first sub run : ") << mon_run_vec[kr].getSubRunNumber();
0565       edm::LogInfo("here is the run number: ") << mon_run_vec[kr].getRunIOV().getRunNumber();
0566 
0567       edm::LogInfo(" retrieve the data for a given run");
0568 
0569       if (mon_run_vec[kr].getSubRunNumber() <= 1) {
0570         // retrieve the data for a given run
0571         RunIOV runiov_prime = mon_run_vec[kr].getRunIOV();
0572 
0573         // retrieve the pedestals from OMDS for this run
0574         std::map<EcalLogicID, MonPedestalsDat> dataset_mon;
0575         econn->fetchDataSet(&dataset_mon, &mon_run_vec[kr]);
0576         std::cout << "OMDS record for run " << irun << " is made of " << dataset_mon.size() << std::endl;
0577         typedef std::map<EcalLogicID, MonPedestalsDat>::const_iterator CImon;
0578         EcalLogicID ecid_xt;
0579         MonPedestalsDat rd_ped;
0580 
0581         //int iEta=0;
0582         //int iPhi=0;
0583         int ix = 0;
0584         int iy = 0;
0585 
0586         for (CImon p = dataset_mon.begin(); p != dataset_mon.end(); p++) {
0587           ecid_xt = p->first;
0588           rd_ped = p->second;
0589           //int sm_num=ecid_xt.getID1();
0590           int xt_num = ecid_xt.getID2();  // careful here !!! we number the channels from 1 to 1700
0591 
0592           //iEta=(xt_num/20)+1;
0593           //iPhi=20-(xt_num-(iEta-1)*20);
0594 
0595           ix = 95 - (xt_num - 1) / 20;
0596           iy = 46 + (xt_num - 1) % 20;
0597 
0598           EcalPedestals::Item item;
0599           item.mean_x1 = rd_ped.getPedMeanG1();
0600           item.rms_x1 = rd_ped.getPedRMSG1();
0601           item.mean_x6 = rd_ped.getPedMeanG6();
0602           item.rms_x6 = rd_ped.getPedRMSG6();
0603           item.mean_x12 = rd_ped.getPedMeanG12();
0604           item.rms_x12 = rd_ped.getPedRMSG12();
0605 
0606           EEDetId eedetidpos(ix, iy, 1);
0607           // EBDetId ebdetid(iEta,iPhi);
0608 
0609           // here we change in the peds object only the values that are available in the online DB
0610           // otherwise we keep the old value
0611 
0612           peds->insert(std::make_pair(eedetidpos.rawId(), item));
0613           if (ix == ixmin && iy == iymin)
0614             std::cout << "ped12 " << item.mean_x12 << std::endl;
0615         }
0616 
0617         edm::LogInfo("Generating popcon record for run ") << irun << "..." << std::flush;
0618 
0619         // now I copy peds in pedtemp and I ship pedtemp to popcon
0620         // if I use always the same peds I always overwrite
0621         // so I really have to create new objects for each new run
0622         // popcon deletes everything for me
0623 
0624         EcalPedestals* pedtemp = new EcalPedestals();
0625 
0626         for (int iX = ixmin; iX <= ixmax; ++iX) {
0627           for (int iY = iymin; iY <= iymax; ++iY) {
0628             if (EEDetId::validDetId(iX, iY, 1)) {
0629               EEDetId eedetidpos(iX, iY, 1);
0630               unsigned int hiee = eedetidpos.hashedIndex();
0631               EcalPedestals::Item aped = peds->endcap(hiee);
0632 
0633               EcalPedestals::Item item;
0634               item.mean_x1 = aped.mean_x1;
0635               item.rms_x1 = aped.rms_x1;
0636               item.mean_x6 = aped.mean_x6;
0637               item.rms_x6 = aped.rms_x6;
0638               item.mean_x12 = aped.mean_x12;
0639               item.rms_x12 = aped.rms_x12;
0640               // here I copy the last valid value in the pedtemp object
0641               pedtemp->insert(std::make_pair(eedetidpos.rawId(), item));
0642               if (iX == ixmin && iY == iymin)
0643                 std::cout << "ped12 " << item.mean_x12 << std::endl;
0644             }
0645           }
0646         }
0647 
0648         Time_t snc = (Time_t)irun;
0649 
0650         m_to_transfer.push_back(std::make_pair((EcalPedestals*)pedtemp, snc));
0651 
0652         edm::LogInfo("Ecal - > end of getNewObjectsH2 -----------\n");
0653       }
0654     }
0655   }
0656   delete econn;
0657   delete peds;  // this is the only one that popcon does not delete
0658 }
0659 //////////////////////////////////////////////////////////////////////////////////////
0660 
0661 void popcon::EcalPedestalsHandler::readPedestalFile() {
0662   edm::LogInfo(" reading the input file ") << m_filename;
0663   std::ifstream fInput;
0664   fInput.open(m_filename);
0665   if (!fInput.is_open()) {
0666     edm::LogInfo("ERROR : cannot open file ") << m_filename;
0667     exit(1);
0668   }
0669   //  string pos, dummyLine;
0670   int hashedId;
0671   float EBmean12[kEBChannels], EBrms12[kEBChannels], EBmean6[kEBChannels], EBrms6[kEBChannels], EBmean1[kEBChannels],
0672       EBrms1[kEBChannels];
0673   //*****************  barrel
0674   //  getline(fInput, dummyLine);   // skip first line
0675   for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
0676     fInput >> hashedId >> EBmean12[iChannel] >> EBrms12[iChannel] >> EBmean6[iChannel] >> EBrms6[iChannel] >>
0677         EBmean1[iChannel] >> EBrms1[iChannel];
0678     if (hashedId != iChannel + 1) {
0679       edm::LogInfo("File") << m_filename << " strange hash " << hashedId << " while iChannel " << iChannel;
0680       exit(-1);
0681     }
0682   }
0683 
0684   // ***************** now EE *****************
0685   float EEmean12[kEEChannels], EErms12[kEEChannels], EEmean6[kEEChannels], EErms6[kEEChannels], EEmean1[kEEChannels],
0686       EErms1[kEEChannels];
0687   for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
0688     fInput >> hashedId >> EEmean12[iChannel] >> EErms12[iChannel] >> EEmean6[iChannel] >> EErms6[iChannel] >>
0689         EEmean1[iChannel] >> EErms1[iChannel];
0690     if (hashedId != iChannel + kEBChannels + 1) {
0691       edm::LogInfo("File") << m_filename << " strange hash " << hashedId << " while iChannel " << iChannel;
0692       exit(-1);
0693     }
0694   }
0695   fInput.close();
0696 
0697   EcalPedestals* ped = new EcalPedestals();
0698   // barrel
0699   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
0700     if (iEta == 0)
0701       continue;
0702     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
0703       if (EBDetId::validDetId(iEta, iPhi)) {
0704         EBDetId ebdetid(iEta, iPhi);
0705         unsigned int hieb = ebdetid.hashedIndex();
0706         EcalPedestals::Item item;
0707         item.mean_x1 = EBmean1[hieb];
0708         item.rms_x1 = EBrms1[hieb];
0709         item.mean_x6 = EBmean6[hieb];
0710         item.rms_x6 = EBrms6[hieb];
0711         item.mean_x12 = EBmean12[hieb];
0712         item.rms_x12 = EBrms12[hieb];
0713         ped->insert(std::make_pair(ebdetid.rawId(), item));
0714       }  // valid EBId
0715     }    //  loop over phi
0716   }      //   loop over eta
0717   // endcaps
0718   //  std::ofstream fout;
0719   //  fout.open("Pedestal.check");
0720   for (int iz = -1; iz < 2; iz = iz + 2) {  // z : -1 and +1
0721     for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0722       for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0723         if (EEDetId::validDetId(iX, iY, iz)) {
0724           EEDetId eedetid(iX, iY, iz);
0725           unsigned int hiee = eedetid.hashedIndex();
0726           //      fout << hiee << " mean 12 " << EEmean12[hiee] << std::endl;
0727           EcalPedestals::Item item;
0728           item.mean_x1 = EEmean1[hiee];
0729           item.rms_x1 = EErms1[hiee];
0730           item.mean_x6 = EEmean6[hiee];
0731           item.rms_x6 = EErms6[hiee];
0732           item.mean_x12 = EEmean12[hiee];
0733           item.rms_x12 = EErms12[hiee];
0734           ped->insert(std::make_pair(eedetid.rawId(), item));
0735         }  // val EEId
0736       }    //  loop over y
0737     }      //   loop over x
0738   }        //   loop over z
0739   //  fout.close();
0740 
0741   unsigned int irun = m_firstRun;
0742   Time_t snc = (Time_t)irun;
0743   m_to_transfer.push_back(std::make_pair((EcalPedestals*)ped, snc));
0744 
0745   edm::LogInfo("Ecal - > end of readPedestalFile -----------\n");
0746 }
0747 //////////////////////////////////////////////////////////////////////////////
0748 
0749 void popcon::EcalPedestalsHandler::readPedestalMC() {
0750   edm::LogInfo(" reading the input file ") << m_filename;
0751   std::ifstream fxml;
0752   fxml.open(m_filename);
0753   if (!fxml.is_open()) {
0754     edm::LogInfo("ERROR : cannot open file ") << m_filename;
0755     exit(1);
0756   }
0757   std::ofstream fout;
0758   fout.open("Pedestal.check");
0759   //  int hashedId;
0760   float EBmean12[kEBChannels], EBrms12[kEBChannels], EBmean6[kEBChannels], EBrms6[kEBChannels], EBmean1[kEBChannels],
0761       EBrms1[kEBChannels];
0762   //  int ringEB[kEBChannels], ringEE[kEEChannels];
0763   double RingMean[28][2][3], RingRMS[28][2][3];
0764   int NbVal[28][2][3];
0765   for (int ring = 0; ring < 28; ring++) {
0766     for (int side = 0; side < 2; side++) {
0767       for (int igain = 0; igain < kGains; igain++) {
0768         RingMean[ring][side][igain] = 0.;
0769         RingRMS[ring][side][igain] = 0.;
0770         NbVal[ring][side][igain] = 0;
0771       }
0772     }
0773   }
0774   std::string dummyLine, mean12, rms12, bid;
0775   for (int i = 0; i < 9; i++)
0776     std::getline(fxml, dummyLine);  // skip first lines
0777   // Barrel
0778   for (int iEBChannel = 0; iEBChannel < kEBChannels; iEBChannel++) {
0779     EBDetId ebdetid = EBDetId::unhashIndex(iEBChannel);
0780     int ieta = ebdetid.ieta();
0781     int ring = (abs(ieta) - 1) / 5;
0782     if (ring < 0 || ring > 16)
0783       edm::LogInfo("EB channel ") << iEBChannel << " ring " << ring;
0784     int izz = 0;
0785     if (ieta > 0)
0786       izz = 1;
0787     fxml >> bid;
0788     std::string stt = bid.substr(10, 15);
0789     std::istringstream m12(stt);
0790     m12 >> EBmean12[iEBChannel];
0791     fxml >> bid;
0792     stt = bid.substr(9, 15);
0793     std::istringstream r12(stt);
0794     r12 >> EBrms12[iEBChannel];
0795     if (EBrms12[iEBChannel] != 0. && EBrms12[iEBChannel] < 5.) {
0796       RingMean[ring][izz][0] += EBrms12[iEBChannel];
0797       RingRMS[ring][izz][0] += EBrms12[iEBChannel] * EBrms12[iEBChannel];
0798       NbVal[ring][izz][0]++;
0799     }
0800     fxml >> bid;
0801     stt = bid.substr(9, 15);
0802     std::istringstream m6(stt);
0803     m6 >> EBmean6[iEBChannel];
0804     fxml >> bid;
0805     stt = bid.substr(8, 15);
0806     std::istringstream r6(stt);
0807     r6 >> EBrms6[iEBChannel];
0808     if (EBrms6[iEBChannel] != 0. && EBrms6[iEBChannel] < 5.) {
0809       RingMean[ring][izz][1] += EBrms6[iEBChannel];
0810       RingRMS[ring][izz][1] += EBrms6[iEBChannel] * EBrms6[iEBChannel];
0811       NbVal[ring][izz][1]++;
0812     }
0813     fxml >> bid;
0814     stt = bid.substr(9, 15);
0815     std::istringstream m1(stt);
0816     m1 >> EBmean1[iEBChannel];
0817     fxml >> bid;
0818     stt = bid.substr(8, 15);
0819     std::istringstream r1(stt);
0820     r1 >> EBrms1[iEBChannel];
0821     if (EBrms1[iEBChannel] != 0. && EBrms1[iEBChannel] < 5.) {
0822       RingMean[ring][izz][2] += EBrms1[iEBChannel];
0823       RingRMS[ring][izz][2] += EBrms1[iEBChannel] * EBrms1[iEBChannel];
0824       NbVal[ring][izz][2]++;
0825     }
0826     if (iEBChannel % 10000 == 0)
0827       fout << " EB channel " << iEBChannel << " " << EBmean12[iEBChannel] << " " << EBrms12[iEBChannel] << " "
0828            << EBmean6[iEBChannel] << " " << EBrms6[iEBChannel] << " " << EBmean1[iEBChannel] << " "
0829            << EBrms1[iEBChannel] << std::endl;
0830     for (int i = 0; i < 3; i++)
0831       std::getline(fxml, dummyLine);  // skip lines
0832   }
0833 
0834   // ***************** now EE *****************
0835   std::ifstream fCrystal;
0836   fCrystal.open("Crystal");
0837   if (!fCrystal.is_open()) {
0838     edm::LogInfo("ERROR : cannot open file Crystal");
0839     exit(1);
0840   }
0841   int ringEE[kEEChannels];
0842   for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
0843     fCrystal >> ringEE[iChannel];
0844     int ring = abs(ringEE[iChannel]) - 1;
0845     if (ring < 17 || ring > 27) {
0846       edm::LogInfo(" EE channel ") << iChannel << " ring " << ringEE[iChannel];
0847       exit(-1);
0848     }
0849   }
0850   fCrystal.close();
0851 
0852   float EEmean12[kEEChannels], EErms12[kEEChannels], EEmean6[kEEChannels], EErms6[kEEChannels], EEmean1[kEEChannels],
0853       EErms1[kEEChannels];
0854   for (int i = 0; i < 6; i++)
0855     std::getline(fxml, dummyLine);  // skip lines
0856   for (int iEEChannel = 0; iEEChannel < kEEChannels; iEEChannel++) {
0857     //    int ich = iEEChannel + kEBChannels;
0858     fxml >> bid;
0859     std::string stt = bid.substr(10, 15);
0860     std::istringstream m12(stt);
0861     m12 >> EEmean12[iEEChannel];
0862     int ring = abs(ringEE[iEEChannel]) - 1;
0863     if (ring < 17 || ring > 27)
0864       edm::LogInfo("EE channel ") << iEEChannel << " ring " << ring;
0865     int izz = 1;
0866     if (iEEChannel < 7324)
0867       izz = 0;
0868     fxml >> bid;
0869     stt = bid.substr(9, 15);
0870     std::istringstream r12(stt);
0871     r12 >> EErms12[iEEChannel];
0872     if (EErms12[iEEChannel] != 0. && EErms12[iEEChannel] < 5.) {
0873       RingMean[ring][izz][0] += EErms12[iEEChannel];
0874       RingRMS[ring][izz][0] += EErms12[iEEChannel] * EErms12[iEEChannel];
0875       NbVal[ring][izz][0]++;
0876     }
0877     fxml >> bid;
0878     stt = bid.substr(9, 15);
0879     std::istringstream m6(stt);
0880     m6 >> EEmean6[iEEChannel];
0881     fxml >> bid;
0882     stt = bid.substr(8, 15);
0883     std::istringstream r6(stt);
0884     r6 >> EErms6[iEEChannel];
0885     if (EErms6[iEEChannel] != 0. && EErms6[iEEChannel] < 5.) {
0886       RingMean[ring][izz][1] += EErms6[iEEChannel];
0887       RingRMS[ring][izz][1] += EErms6[iEEChannel] * EErms6[iEEChannel];
0888       NbVal[ring][izz][1]++;
0889     }
0890     fxml >> bid;
0891     stt = bid.substr(9, 15);
0892     std::istringstream m1(stt);
0893     m1 >> EEmean1[iEEChannel];
0894     fxml >> bid;
0895     stt = bid.substr(8, 15);
0896     std::istringstream r1(stt);
0897     r1 >> EErms1[iEEChannel];
0898     if (EErms1[iEEChannel] != 0. && EErms1[iEEChannel] < 5.) {
0899       RingMean[ring][izz][2] += EErms1[iEEChannel];
0900       RingRMS[ring][izz][2] += EErms1[iEEChannel] * EErms1[iEEChannel];
0901       NbVal[ring][izz][2]++;
0902     }
0903     if (iEEChannel % 1000 == 0)
0904       fout << " EE channel " << iEEChannel << " " << EEmean12[iEEChannel] << " " << EErms12[iEEChannel] << " "
0905            << EEmean6[iEEChannel] << " " << EErms6[iEEChannel] << " " << EEmean1[iEEChannel] << " "
0906            << EErms1[iEEChannel] << std::endl;
0907     for (int i = 0; i < 3; i++)
0908       std::getline(fxml, dummyLine);  // skip lines
0909   }
0910 
0911   fxml.close();
0912 
0913   for (int gain = 0; gain < kGains; gain++) {
0914     fout << "\n"
0915          << "**** Gain ****  " << gainValues[gain] << "\n";
0916     for (int ring = 0; ring < 28; ring++) {
0917       for (int side = 0; side < 2; side++) {
0918         if (NbVal[ring][side][gain] <= 0) {
0919           edm::LogInfo(" No entry for ring ") << ring;
0920           exit(-1);
0921         }
0922         RingMean[ring][side][gain] /= (double)NbVal[ring][side][gain];
0923         double x = RingMean[ring][side][gain];
0924         RingRMS[ring][side][gain] /= (double)NbVal[ring][side][gain];
0925         double rms = sqrt(RingRMS[ring][side][gain] - x * x);
0926         RingRMS[ring][side][gain] = rms;
0927         fout << " ring " << ring + 1 << " mean " << x << " rms " << rms << "    ";
0928       }  //  loop over sides
0929       fout << std::endl;
0930       if (ring == 16)
0931         fout << "*****  End caps *****     EE-                              EE+" << std::endl;
0932     }  //  loop over rings
0933   }    //  loop over gains
0934 
0935   // read also the ring value from Crystal file
0936 
0937   EcalPedestals* ped = new EcalPedestals();
0938   // barrel
0939   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
0940     if (iEta == 0)
0941       continue;
0942     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
0943       if (EBDetId::validDetId(iEta, iPhi)) {
0944         EBDetId ebdetid(iEta, iPhi);
0945         unsigned int hieb = ebdetid.hashedIndex();
0946         EcalPedestals::Item item;
0947         item.mean_x1 = 200.;
0948         item.mean_x6 = 200.;
0949         item.mean_x12 = 200.;
0950         if (m_corrected) {
0951           int ring = (abs(iEta) - 1) / 5;
0952           if (ring < 0 || ring > 16)
0953             edm::LogInfo("EB channel ") << hieb << " ring " << ring;
0954           int side = 0;
0955           if (iEta > 0)
0956             side = 1;
0957           if (EBrms1[hieb] == 0 || EBrms1[hieb] > RingMean[ring][side][2] + 3 * RingRMS[ring][side][2]) {
0958             fout << " EB channel " << hieb << " eta " << iEta << " phi " << iPhi << " ring " << ring + 1
0959                  << " gain 1 rms " << EBrms1[hieb] << " replaced by " << RingMean[ring][side][2] << std::endl;
0960             item.rms_x1 = RingMean[ring][side][2];
0961           } else
0962             item.rms_x1 = EBrms1[hieb];
0963           if (EBrms6[hieb] == 0 || EBrms6[hieb] > RingMean[ring][side][1] + 3 * RingRMS[ring][side][1]) {
0964             fout << " EB channel " << hieb << " eta " << iEta << " phi " << iPhi << " ring " << ring + 1
0965                  << " gain 6 rms " << EBrms6[hieb] << " replaced by " << RingMean[ring][side][1] << std::endl;
0966             item.rms_x6 = RingMean[ring][side][1];
0967           } else
0968             item.rms_x6 = EBrms6[hieb];
0969           if (EBrms12[hieb] == 0 || EBrms12[hieb] > RingMean[ring][side][0] + 3 * RingRMS[ring][side][0]) {
0970             fout << " EB channel " << hieb << " eta " << iEta << " phi " << iPhi << " ring " << ring + 1
0971                  << " gain 12 rms " << EBrms12[hieb] << " replaced by " << RingMean[ring][side][0] << std::endl;
0972             item.rms_x12 = RingMean[ring][side][0];
0973           } else
0974             item.rms_x12 = EBrms12[hieb];
0975           if (hieb > 4534 && hieb < 4540)
0976             edm::LogInfo(" Channel ") << hieb << " ring " << ring << " 12 " << EBrms12[hieb] << " mean "
0977                                       << RingMean[ring][side][0] << " rms " << RingRMS[ring][side][0] << "  6 "
0978                                       << EBrms6[hieb] << " mean " << RingMean[ring][side][1] << " rms "
0979                                       << RingRMS[ring][side][1] << "  1 " << EBrms1[hieb] << " mean "
0980                                       << RingMean[ring][side][2] << " rms " << RingRMS[ring][side][2];
0981         }  // if corrected
0982         else {
0983           item.rms_x1 = EBrms1[hieb];
0984           item.rms_x6 = EBrms6[hieb];
0985           item.rms_x12 = EBrms12[hieb];
0986         }
0987         if (item.rms_x1 > 4. || item.rms_x6 > 4. || item.rms_x12 > 4.)
0988           edm::LogInfo(" Channel ") << hieb << " 12 " << item.rms_x12 << " 6 " << item.rms_x6 << " 1 " << item.rms_x1;
0989         ped->insert(std::make_pair(ebdetid.rawId(), item));
0990       }  // valid EBId
0991     }    //  loop over phi
0992   }      //   loop over eta
0993 
0994   // endcaps
0995   for (int iz = -1; iz < 2; iz = iz + 2) {  // z : -1 and +1
0996     for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0997       for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0998         if (EEDetId::validDetId(iX, iY, iz)) {
0999           EEDetId eedetid(iX, iY, iz);
1000           unsigned int hiee = eedetid.hashedIndex();
1001           //      fout << hiee << " mean 12 " << EEmean12[hiee] << std::endl;
1002           EcalPedestals::Item item;
1003           item.mean_x1 = 200.;
1004           item.mean_x6 = 200.;
1005           item.mean_x12 = 200.;
1006           if (m_corrected) {
1007             int ring = abs(ringEE[hiee]) - 1;
1008             if (ring < 17 || ring > 27)
1009               edm::LogInfo("EE channel ") << hiee << " ring " << ring;
1010             if (EErms1[hiee] == 0 || EErms1[hiee] > RingMean[ring][iz][2] + 3 * RingRMS[ring][iz][2]) {
1011               fout << " EE channel " << hiee << " x " << iX << " y " << iY << " z " << iz << " ring " << ring + 1
1012                    << " gain 1 rms " << EErms1[hiee] << " replaced by " << RingMean[ring][iz][2] << std::endl;
1013               item.rms_x1 = RingMean[ring][iz][2];
1014             } else
1015               item.rms_x1 = EErms1[hiee];
1016             if (EErms6[hiee] == 0 || EErms6[hiee] > RingMean[ring][iz][1] + 3 * RingRMS[ring][iz][1]) {
1017               fout << " EE channel " << hiee << " x " << iX << " y " << iY << " z " << iz << " ring " << ring + 1
1018                    << " gain 6 rms " << EErms6[hiee] << " replaced by " << RingMean[ring][iz][1] << std::endl;
1019               item.rms_x6 = RingMean[ring][iz][1];
1020             } else
1021               item.rms_x6 = EErms6[hiee];
1022             if (EErms12[hiee] == 0 || EErms12[hiee] > RingMean[ring][iz][0] + 3 * RingRMS[ring][iz][0]) {
1023               fout << " EE channel " << hiee << " x " << iX << " y " << iY << " z " << iz << " ring " << ring + 1
1024                    << " gain 12 rms " << EErms12[hiee] << " replaced by " << RingMean[ring][iz][0] << std::endl;
1025               item.rms_x12 = RingMean[ring][iz][0];
1026             } else
1027               item.rms_x12 = EErms12[hiee];
1028           }  // if corrected
1029           else {
1030             item.rms_x1 = EErms1[hiee];
1031             item.rms_x6 = EErms6[hiee];
1032             item.rms_x12 = EErms12[hiee];
1033           }
1034           ped->insert(std::make_pair(eedetid.rawId(), item));
1035         }  // val EEId
1036       }    //  loop over y
1037     }      //   loop over x
1038   }        //   loop over z
1039   fout.close();
1040 
1041   unsigned int irun = m_firstRun;
1042   Time_t snc = (Time_t)irun;
1043   m_to_transfer.push_back(std::make_pair((EcalPedestals*)ped, snc));
1044 
1045   edm::LogInfo("Ecal - > end of readPedestalMC -----------\n");
1046 }
1047 
1048 ///////////////////////////////////////////////////////////////////////////////////////////
1049 
1050 void popcon::EcalPedestalsHandler::readPedestal2017() {
1051   bool debug = false;
1052   edm::LogInfo(" reading local Pedestal run (2017 way)! ") << m_filename;
1053 
1054   // First we copy the last valid record to a temporary object peds
1055   Ref ped_db = lastPayload();
1056   EcalPedestals* peds = new EcalPedestals();
1057   edm::LogInfo("retrieved last payload ");
1058 
1059   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
1060     if (iEta == 0)
1061       continue;
1062     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
1063       // make an EBDetId since we need EBDetId::rawId() to be used as the key for the pedestals
1064       if (EBDetId::validDetId(iEta, iPhi)) {
1065         EBDetId ebdetid(iEta, iPhi, EBDetId::ETAPHIMODE);
1066         EcalPedestals::const_iterator it = ped_db->find(ebdetid.rawId());
1067         EcalPedestals::Item aped = (*it);
1068         // here I copy the last valid value in the peds object
1069         EcalPedestals::Item item;
1070         item.mean_x1 = aped.mean_x1;
1071         item.rms_x1 = aped.rms_x1;
1072         item.mean_x6 = aped.mean_x6;
1073         item.rms_x6 = aped.rms_x6;
1074         item.mean_x12 = aped.mean_x12;
1075         item.rms_x12 = aped.rms_x12;
1076         peds->insert(std::make_pair(ebdetid.rawId(), item));
1077       }
1078     }
1079   }
1080   for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
1081     for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
1082       // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
1083       if (EEDetId::validDetId(iX, iY, 1)) {
1084         EEDetId eedetidpos(iX, iY, 1);
1085         EcalPedestals::const_iterator it = ped_db->find(eedetidpos.rawId());
1086         EcalPedestals::Item aped = (*it);
1087         // here I copy the last valid value in the peds object
1088         EcalPedestals::Item item;
1089         item.mean_x1 = aped.mean_x1;
1090         item.rms_x1 = aped.rms_x1;
1091         item.mean_x6 = aped.mean_x6;
1092         item.rms_x6 = aped.rms_x6;
1093         item.mean_x12 = aped.mean_x12;
1094         item.rms_x12 = aped.rms_x12;
1095         peds->insert(std::make_pair(eedetidpos.rawId(), item));
1096       }
1097       if (EEDetId::validDetId(iX, iY, -1)) {
1098         EEDetId eedetidneg(iX, iY, -1);
1099         EcalPedestals::const_iterator it = ped_db->find(eedetidneg.rawId());
1100         EcalPedestals::Item aped = (*it);
1101         // here I copy the last valid value in the peds object
1102         EcalPedestals::Item item;
1103         item.mean_x1 = aped.mean_x1;
1104         item.rms_x1 = aped.rms_x1;
1105         item.mean_x6 = aped.mean_x6;
1106         item.rms_x6 = aped.rms_x6;
1107         item.mean_x12 = aped.mean_x12;
1108         item.rms_x12 = aped.rms_x12;
1109         peds->insert(std::make_pair(eedetidneg.rawId(), item));
1110       }
1111     }
1112   }
1113 
1114   edm::LogInfo(" reading the input file ") << m_filename;
1115   std::ifstream fInput;
1116   fInput.open(m_filename);
1117   if (!fInput.is_open()) {
1118     edm::LogInfo("ERROR : cannot open file ") << m_filename;
1119     exit(1);
1120   }
1121   //  string pos, dummyLine;
1122   int hashedId;
1123   float EBmean12[kEBChannels], EBrms12[kEBChannels], EBmean6[kEBChannels], EBrms6[kEBChannels], EBmean1[kEBChannels],
1124       EBrms1[kEBChannels];
1125   //*****************  barrel
1126   //  getline(fInput, dummyLine);   // skip first line
1127   for (int iChannel = 0; iChannel < kEBChannels; iChannel++) {
1128     fInput >> hashedId >> EBmean12[iChannel] >> EBrms12[iChannel] >> EBmean6[iChannel] >> EBrms6[iChannel] >>
1129         EBmean1[iChannel] >> EBrms1[iChannel];
1130     if (hashedId != iChannel + 1) {
1131       edm::LogInfo("File ") << m_filename << " strange hash " << hashedId << " while iChannel " << iChannel;
1132       exit(-1);
1133     }
1134   }
1135 
1136   // ***************** now EE *****************
1137   float EEmean12[kEEChannels], EErms12[kEEChannels], EEmean6[kEEChannels], EErms6[kEEChannels], EEmean1[kEEChannels],
1138       EErms1[kEEChannels];
1139   for (int iChannel = 0; iChannel < kEEChannels; iChannel++) {
1140     fInput >> hashedId >> EEmean12[iChannel] >> EErms12[iChannel] >> EEmean6[iChannel] >> EErms6[iChannel] >>
1141         EEmean1[iChannel] >> EErms1[iChannel];
1142     if (hashedId != iChannel + kEBChannels + 1) {
1143       edm::LogInfo("File ") << m_filename << " strange hash " << hashedId << " while iChannel " << iChannel;
1144       exit(-1);
1145     }
1146   }
1147   fInput.close();
1148 
1149   EcalPedestals* ped = new EcalPedestals();
1150   // barrel
1151   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
1152     if (iEta == 0)
1153       continue;
1154     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
1155       if (EBDetId::validDetId(iEta, iPhi)) {
1156         EBDetId ebdetid(iEta, iPhi);
1157         unsigned int hieb = ebdetid.hashedIndex();
1158         EcalPedestals::Item item;
1159         EcalPedestals::Item previous_ped = peds->barrel(hieb);
1160         if (debug & (EBmean12[hieb] == -999. || EBrms12[hieb] == -999. || EBmean6[hieb] == -999. ||
1161                      EBrms6[hieb] == -999. || EBmean1[hieb] == -999. || EBrms1[hieb] == -999.))
1162           edm::LogInfo(" bad EB channel eta ")
1163               << iEta << " phi " << iPhi << " " << EBmean12[hieb] << " " << EBrms12[hieb] << " " << EBmean6[hieb] << " "
1164               << EBrms6[hieb] << " " << EBmean1[hieb] << " " << EBrms1[hieb];
1165         if (EBmean1[hieb] != -999.)
1166           item.mean_x1 = EBmean1[hieb];
1167         else
1168           item.mean_x1 = previous_ped.mean_x1;
1169         if (EBrms1[hieb] != -999.)
1170           item.rms_x1 = EBrms1[hieb];
1171         else
1172           item.rms_x1 = previous_ped.rms_x1;
1173         if (EBmean6[hieb] != -999.)
1174           item.mean_x6 = EBmean6[hieb];
1175         else
1176           item.mean_x6 = previous_ped.mean_x6;
1177         if (EBrms6[hieb] != -999.)
1178           item.rms_x6 = EBrms6[hieb];
1179         else
1180           item.rms_x6 = previous_ped.rms_x6;
1181         if (EBmean12[hieb] != -999.)
1182           item.mean_x12 = EBmean12[hieb];
1183         else
1184           item.mean_x12 = previous_ped.mean_x12;
1185         if (EBrms12[hieb] != -999.)
1186           item.rms_x12 = EBrms12[hieb];
1187         else
1188           item.rms_x12 = previous_ped.rms_x12;
1189         ped->insert(std::make_pair(ebdetid.rawId(), item));
1190       }  // valid EBId
1191     }    //  loop over phi
1192   }      //   loop over eta
1193   // endcaps
1194   //  std::ofstream fout;
1195   //  fout.open("Pedestal.check");
1196   for (int iz = -1; iz < 2; iz = iz + 2) {  // z : -1 and +1
1197     for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
1198       for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
1199         if (EEDetId::validDetId(iX, iY, iz)) {
1200           EEDetId eedetid(iX, iY, iz);
1201           unsigned int hiee = eedetid.hashedIndex();
1202           //      fout << hiee << " mean 12 " << EEmean12[hiee] << std::endl;
1203           EcalPedestals::Item item;
1204           EcalPedestals::Item previous_ped = peds->endcap(hiee);
1205           if (debug & (EEmean12[hiee] == -999. || EErms12[hiee] == -999. || EEmean6[hiee] == -999. ||
1206                        EErms6[hiee] == -999. || EEmean1[hiee] == -999. || EErms1[hiee] == -999.))
1207             edm::LogInfo(" bad EE channel x ")
1208                 << iX << " y " << iY << " z" << iz << " " << EEmean12[hiee] << " " << EErms12[hiee] << " "
1209                 << EEmean6[hiee] << " " << EErms6[hiee] << " " << EEmean1[hiee] << " " << EErms1[hiee];
1210           if (EEmean1[hiee] != -999.)
1211             item.mean_x1 = EEmean1[hiee];
1212           else
1213             item.mean_x1 = previous_ped.mean_x1;
1214           if (EErms1[hiee] != -999.)
1215             item.rms_x1 = EErms1[hiee];
1216           else
1217             item.rms_x1 = previous_ped.rms_x1;
1218           if (EEmean6[hiee] != -999.)
1219             item.mean_x6 = EEmean6[hiee];
1220           else
1221             item.mean_x6 = previous_ped.mean_x6;
1222           if (EErms6[hiee] != -999.)
1223             item.rms_x6 = EErms6[hiee];
1224           else
1225             item.rms_x6 = previous_ped.rms_x6;
1226           if (EEmean12[hiee] != -999.)
1227             item.mean_x12 = EEmean12[hiee];
1228           else
1229             item.mean_x12 = previous_ped.mean_x12;
1230           if (EErms12[hiee] != -999.)
1231             item.rms_x12 = EErms12[hiee];
1232           else
1233             item.rms_x12 = previous_ped.rms_x12;
1234           ped->insert(std::make_pair(eedetid.rawId(), item));
1235         }  // val EEId
1236       }    //  loop over y
1237     }      //   loop over x
1238   }        //   loop over z
1239   //  fout.close();
1240 
1241   unsigned int irun = m_firstRun;
1242   Time_t snc = (Time_t)irun;
1243   m_to_transfer.push_back(std::make_pair((EcalPedestals*)ped, snc));
1244 
1245   edm::LogInfo("Ecal - > end of readPedestal2017 -----------\n");
1246 }
1247 /////////////////////////////////////////////////////////////////////////////////////////
1248 
1249 void popcon::EcalPedestalsHandler::readPedestalTree() {
1250   edm::LogInfo(" reading the root file ") << m_filename;
1251   TFile* hfile = new TFile(m_filename.c_str());
1252 
1253   TTree* treeChan = (TTree*)hfile->Get("PedChan");
1254   int iChannel = 0, ix = 0, iy = 0, iz = 0;
1255   treeChan->SetBranchAddress("Channels", &iChannel);
1256   treeChan->SetBranchAddress("x", &ix);
1257   treeChan->SetBranchAddress("y", &iy);
1258   treeChan->SetBranchAddress("z", &iz);
1259   int neventsChan = (int)treeChan->GetEntries();
1260   edm::LogInfo("PedChan nb entries ") << neventsChan;
1261   int ringEB[kEBChannels], sideEB[kEBChannels], ixEE[kEEChannels], iyEE[kEEChannels], izEE[kEEChannels];
1262   for (int entry = 0; entry < neventsChan; entry++) {
1263     treeChan->GetEntry(entry);
1264     if (entry < kEBChannels) {
1265       ringEB[iChannel] = (abs(ix) - 1) / 5;  // -85...-1, 1...85 give 0...16
1266       sideEB[iChannel] = 1;
1267       if (ix < 0)
1268         sideEB[iChannel] = 0;
1269       if (entry % 10000 == 0)
1270         edm::LogInfo(" EB channel ") << iChannel << " eta " << ix << " phi " << iy << " side " << sideEB[iChannel]
1271                                      << " ring " << ringEB[iChannel];
1272     } else {
1273       ixEE[iChannel] = ix;
1274       iyEE[iChannel] = iy;
1275       izEE[iChannel] = iz;
1276       if (entry % 1000 == 0)
1277         edm::LogInfo(" EE channel ") << iChannel << " x " << ixEE[iChannel] << " y " << iyEE[iChannel] << " z "
1278                                      << izEE[iChannel];
1279     }
1280   }
1281   //    2016 : 26
1282   Int_t pedxml[Nbpedxml] = {271948,
1283                             273634,
1284                             273931,
1285                             274705,
1286                             275403,
1287                             276108,
1288                             276510,
1289                             277169,
1290                             278123,
1291                             278183,
1292                             278246,
1293                             278389,
1294                             278499,
1295                             278693,
1296                             278858,
1297                             278888,
1298                             278931,
1299                             279728,
1300                             280129,
1301                             280263,
1302                             280941,
1303                             281753,
1304                             282631,
1305                             282833,
1306                             283199,
1307                             283766,
1308                             //    2017 : 25
1309                             286535,
1310                             293513,
1311                             293632,
1312                             293732,
1313                             295507,
1314                             295672,
1315                             296391,
1316                             296917,
1317                             297388,
1318                             298481,
1319                             299279,
1320                             299710,
1321                             300186,
1322                             300581,
1323                             301191,
1324                             302006,
1325                             302293,
1326                             302605,
1327                             303436,
1328                             303848,
1329                             304211,
1330                             304680,
1331                             305117,
1332                             305848,
1333                             306176,
1334                             //    2018 : 30
1335                             313760,
1336                             315434,
1337                             315831,
1338                             316299,
1339                             316694,
1340                             316963,
1341                             317399,
1342                             317669,
1343                             318249,
1344                             318747,
1345                             319111,
1346                             319365,
1347                             319700,
1348                             320098,
1349                             320515,
1350                             320905,
1351                             321184,
1352                             321480,
1353                             321855,
1354                             322151,
1355                             322653,
1356                             323261,
1357                             323802,
1358                             324250,
1359                             324632,
1360                             325909,
1361                             326038,
1362                             326652,
1363                             326979,
1364                             327271};
1365 
1366   Int_t run16Index = 0;
1367 
1368   Int_t fed[kChannels], chan[kChannels], id, run, run_type, seq_id, las_id, fill_num, run_num_infill, run_time,
1369       run_time_stablebeam, nxt, time[54];
1370   Float_t ped[kChannels], pedrms[kChannels], lumi, bfield;
1371   TTree* tree = (TTree*)hfile->Get("T");
1372   tree->Print();
1373   tree->SetBranchAddress("id", &id);
1374   tree->SetBranchAddress("run", &run);
1375   tree->SetBranchAddress("run_type", &run_type);
1376   tree->SetBranchAddress("seq_id", &seq_id);
1377   tree->SetBranchAddress("las_id", &las_id);
1378   tree->SetBranchAddress("fill_num", &fill_num);
1379   tree->SetBranchAddress("run_num_infill", &run_num_infill);
1380   tree->SetBranchAddress("run_time", &run_time);
1381   tree->SetBranchAddress("run_time_stablebeam", &run_time_stablebeam);
1382   tree->SetBranchAddress("lumi", &lumi);
1383   tree->SetBranchAddress("bfield", &bfield);
1384   tree->SetBranchAddress("nxt", &nxt);  // nb of filled Crystals
1385   tree->SetBranchAddress("time", time);
1386   tree->SetBranchAddress("fed", fed);
1387   tree->SetBranchAddress("chan", chan);
1388   tree->SetBranchAddress("ped", ped);
1389   tree->SetBranchAddress("pedrms", pedrms);
1390   int nevents = (int)tree->GetEntries();
1391   edm::LogInfo(" nb entries ") << nevents;
1392   std::ofstream fout;
1393   fout.open("copyTreePedestals.txt");
1394   if (!fout.is_open()) {
1395     edm::LogInfo("ERROR : cannot open file copyTreePedestals.txt");
1396     exit(1);
1397   }
1398   Double_t EAmean1[kChannels], EArms1[kChannels], EAmean6[kChannels], EArms6[kChannels], EAmean12[kChannels],
1399       EArms12[kChannels];
1400   Int_t RunEntry = 0, EAentry[kChannels];
1401   for (int ich = 0; ich < kChannels; ich++) {
1402     EAmean12[ich] = 0;
1403     EArms12[ich] = 0;
1404     EAentry[ich] = 0;
1405   }
1406   tree->GetEntry(0);
1407   fout << " first run " << run << " fill " << fill_num << " B field " << bfield << " run type " << run_type
1408        << " seq_id " << seq_id << " las_id " << las_id << " run_num_infill " << run_num_infill << " lumi " << lumi
1409        << " nb of Crystals " << nxt << std::endl;
1410   for (int ich = 0; ich < kChannels; ich++) {
1411     if (ped[ich] != 0.) {
1412       if (ich < kEBChannels)
1413         fout << " channel " << ich << " FED " << fed[ich] << " chan " << chan[ich] << " pedestal " << ped[ich]
1414              << " RMS " << pedrms[ich] << std::endl;
1415       else  //   EE
1416         fout << " channel " << ich << " EE channel " << ich - kEBChannels << " FED " << fed[ich] << " chan "
1417              << chan[ich] << " pedestal " << ped[ich] << " RMS " << pedrms[ich] << std::endl;
1418     }
1419   }
1420 
1421   int run_type_kept = m_runtype;         // 1 collision 2 cosmics 3 circulating 4 test
1422   int first_run_kept = (int)m_firstRun;  //  m_firstRun is unsigned!
1423   int last_run_kept = (int)m_lastRun;    //  m_lastRun is unsigned!
1424   int runold = run;
1425   int firsttimeFED = -1;
1426   //  int timeold = -1;
1427   for (int entry = 0; entry < nevents; entry++) {
1428     tree->GetEntry(entry);
1429     if (run < first_run_kept) {
1430       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1431            << " before first wanted " << m_firstRun << std::endl;
1432       runold = run;
1433       continue;
1434     }
1435     if (run > last_run_kept) {
1436       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1437            << " after last wanted " << m_lastRun << std::endl;
1438       runold = run;
1439       break;
1440     }
1441     if (run_type != run_type_kept) {
1442       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time << " run type "
1443            << run_type << std::endl;
1444       continue;  // use only wanted type runs
1445     }
1446     if (nxt != kChannels) {
1447       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1448            << " ***********  Number of channels " << nxt << std::endl;
1449       continue;
1450     }
1451     if (bfield < 3.79) {
1452       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1453            << " ***********  bfield = " << bfield << std::endl;
1454       //      continue;  keep these runs
1455     }
1456     if (run_type_kept == 1) {  // for collision runs, keep only sequences after stable beam
1457       int time_seq = 0;
1458       for (int ifed = 0; ifed < 54; ifed++) {
1459         if (time[ifed] < run_time_stablebeam) {
1460           if (time_seq == 0)
1461             fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1462                  << "  ***********  sequence before stable beam at " << run_time_stablebeam << " FED " << ifed << " : "
1463                  << time[ifed];
1464           else
1465             fout << " FED " << ifed << " : " << time[ifed];
1466           time_seq++;
1467         }
1468       }
1469       if (time_seq != 0) {
1470         fout << " total nb " << time_seq << std::endl;
1471         continue;
1472       }
1473     }  // only collision runs
1474     //    if(idtime != timeold) {
1475     //      edm::LogInfo(" entry ")<< entry << " run " << run << " time " << idtime << " run type " << run_type;
1476     //      timeold = idtime;
1477     //    }
1478     if (run == runold) {
1479       RunEntry++;
1480       for (int ich = 0; ich < kChannels; ich++) {
1481         if (ped[ich] < 300 && ped[ich] > 100) {
1482           EAmean12[ich] += ped[ich];
1483           EArms12[ich] += pedrms[ich];
1484           EAentry[ich]++;
1485         }
1486       }
1487     } else {
1488       // new run. Write the previous one
1489       //      edm::LogInfo(" entry ")<< entry << " fill " << fill_num << " run " << runold << " nb of events " << RunEntry
1490       //        << " time " << run_time << " " << run_time_stablebeam << " " << time[0] << " run type " << run_type;
1491       if (RunEntry == 0 || (run_type_kept == 2 && RunEntry < 6))
1492         fout << " skiped run " << runold << " not enough entries : " << RunEntry << std::endl;
1493       else {
1494         fout << " entry " << entry - 1 << " run " << runold << " nb of events " << RunEntry;
1495         firsttimeFED = time[0];
1496         for (int ifed = 0; ifed < 54; ifed++) {
1497           fout << " " << time[ifed];
1498           if (firsttimeFED < time[ifed])
1499             firsttimeFED = time[ifed];
1500         }
1501         fout << std::endl;
1502 
1503         // get gain 1 and 6 results
1504         bool foundNew = false;
1505         for (int i = run16Index; i < Nbpedxml; i++) {
1506           if (runold > pedxml[i]) {
1507             fout << " found a new gain 1, 6 file " << pedxml[i] << " at index " << i << std::endl;
1508             run16Index++;
1509             foundNew = true;
1510             if (runold < pedxml[i + 1])
1511               break;
1512           }
1513         }
1514         if (foundNew) {
1515           int Indexxml = run16Index - 1;
1516           fout << " opening Pedestals_" << pedxml[Indexxml] << ".xml at index " << Indexxml << std::endl;
1517           std::ifstream fxml;
1518           fxml.open(Form("Pedestals_%i.xml", pedxml[Indexxml]));
1519           if (!fxml.is_open()) {
1520             edm::LogInfo("ERROR : cannot open file Pedestals_") << pedxml[Indexxml] << ".xml";
1521             exit(1);
1522           }
1523           std::string dummyLine, mean12, rms12, bid;
1524           for (int i = 0; i < 9; i++)
1525             std::getline(fxml, dummyLine);  // skip first lines
1526           // Barrel
1527           for (int iEBChannel = 0; iEBChannel < kEBChannels; iEBChannel++) {
1528             fxml >> mean12 >> rms12 >> bid;
1529             //    std::string stt = bid.std::substr(10,15);
1530             //    istringstream ii(stt);
1531             //    ii >> EBmean12[iEBChannel];
1532             //    fxml >> bid;
1533             //    stt = bid.substr(9,15);
1534             //    istringstream r12(stt);
1535             //    r12 >> EBrms12[iEBChannel];
1536             //    fxml >> bid;
1537             std::string stt = bid.substr(9, 15);
1538             std::istringstream m6(stt);
1539             m6 >> EAmean6[iEBChannel];
1540             fxml >> bid;
1541             stt = bid.substr(8, 15);
1542             std::istringstream r6(stt);
1543             r6 >> EArms6[iEBChannel];
1544             fxml >> bid;
1545             stt = bid.substr(9, 15);
1546             std::istringstream m1(stt);
1547             m1 >> EAmean1[iEBChannel];
1548             fxml >> bid;
1549             stt = bid.substr(8, 15);
1550             std::istringstream r1(stt);
1551             r1 >> EArms1[iEBChannel];
1552             if (iEBChannel % 10000 == 0)
1553               fout << " EB channel " << iEBChannel << " " << mean12 << " " << rms12 << " " << EAmean6[iEBChannel] << " "
1554                    << EArms6[iEBChannel] << " " << EAmean1[iEBChannel] << " " << EArms1[iEBChannel] << std::endl;
1555             for (int i = 0; i < 3; i++)
1556               std::getline(fxml, dummyLine);  // skip lines
1557           }
1558           for (int i = 0; i < 6; i++)
1559             std::getline(fxml, dummyLine);  // skip lines
1560           for (int iEEChannel = 0; iEEChannel < kEEChannels; iEEChannel++) {
1561             int ich = iEEChannel + kEBChannels;
1562             fxml >> mean12 >> rms12 >> bid;
1563             std::string stt = bid.substr(9, 15);
1564             std::istringstream m6(stt);
1565             m6 >> EAmean6[ich];
1566             fxml >> bid;
1567             stt = bid.substr(8, 15);
1568             std::istringstream r6(stt);
1569             r6 >> EArms6[ich];
1570             fxml >> bid;
1571             stt = bid.substr(9, 15);
1572             std::istringstream m1(stt);
1573             m1 >> EAmean1[ich];
1574             fxml >> bid;
1575             stt = bid.substr(8, 15);
1576             std::istringstream r1(stt);
1577             r1 >> EArms1[ich];
1578             if (iEEChannel % 1000 == 0)
1579               fout << " EE channel " << iEEChannel << " " << mean12 << " " << rms12 << " " << EAmean6[ich] << " "
1580                    << EArms6[ich] << " " << EAmean1[ich] << " " << EArms1[ich] << std::endl;
1581             for (int i = 0; i < 3; i++)
1582               std::getline(fxml, dummyLine);  // skip lines
1583           }
1584 
1585           fxml.close();
1586         }
1587         // end gain 1 and 6 results
1588 
1589         EcalPedestals* pedestal = new EcalPedestals();
1590         EcalPedestals::Item item;
1591         for (int ich = 0; ich < kChannels; ich++) {
1592           if (EAentry[ich] != 0) {
1593             EAmean12[ich] /= EAentry[ich];
1594             EArms12[ich] /= EAentry[ich];
1595           } else {
1596             EAmean12[ich] = 200.;
1597             EArms12[ich] = 0.;
1598           }
1599           if (ich % 10000 == 0)
1600             fout << " channel " << ich << " ped " << EAmean12[ich] << " RMS " << EArms12[ich] << std::endl;
1601           //
1602           item.mean_x1 = EAmean1[ich];
1603           item.rms_x1 = EArms1[ich];
1604           item.mean_x6 = EAmean6[ich];
1605           item.rms_x6 = EArms6[ich];
1606           item.mean_x12 = EAmean12[ich];
1607           item.rms_x12 = EArms12[ich];
1608           if (ich < kEBChannels) {
1609             //  EBDetId ebdetid(fed, chan, EBDetId::SMCRYSTALMODE);
1610             EBDetId ebdetid = EBDetId::unhashIndex(ich);
1611             pedestal->insert(std::make_pair(ebdetid.rawId(), item));
1612           } else {
1613             //    EEDetId eedetid(iX, iY, iz);
1614             int iChannel = ich - kEBChannels;
1615             EEDetId eedetid = EEDetId::unhashIndex(iChannel);
1616             pedestal->insert(std::make_pair(eedetid.rawId(), item));
1617           }
1618         }  // end loop over all channels
1619         Time_t snc = (Time_t)runold;
1620         m_to_transfer.push_back(std::make_pair((EcalPedestals*)pedestal, snc));
1621       }  // skip too short cosmics runs
1622 
1623       runold = run;
1624       RunEntry = 1;
1625       for (int ich = 0; ich < kChannels; ich++) {
1626         if (ped[ich] < 300 && ped[ich] > 100) {
1627           EAmean12[ich] = ped[ich];
1628           EArms12[ich] = pedrms[ich];
1629           EAentry[ich] = 1;
1630         } else {
1631           EAmean12[ich] = 0;
1632           EArms12[ich] = 0;
1633           EAentry[ich] = 0;
1634         }
1635       }
1636     }  // new run
1637   }    // end loop over all entries
1638   // write also the last run
1639   fout << " last entry fill " << fill_num << " run " << runold << " nb of events " << RunEntry << " time " << run_time
1640        << " " << run_time_stablebeam << " " << time[0] << " run type " << run_type << std::endl;
1641   for (int ifed = 0; ifed < 54; ifed++)
1642     fout << " " << time[ifed];
1643   fout << std::endl;
1644   EcalPedestals* pedestal = new EcalPedestals();
1645   EcalPedestals::Item item;
1646   for (int ich = 0; ich < kChannels; ich++) {
1647     if (EAentry[ich] != 0) {
1648       EAmean12[ich] /= EAentry[ich];
1649       EArms12[ich] /= EAentry[ich];
1650     } else {
1651       EAmean12[ich] = 200.;
1652       EArms12[ich] = 0.;
1653     }
1654     if (ich % 10000 == 0)
1655       fout << " channel " << ich << " ped " << EAmean12[ich] << " RMS " << EArms12[ich] << std::endl;
1656     // get gain 1 and 6 results
1657     // ...
1658     //
1659     item.mean_x1 = EAmean1[ich];
1660     item.rms_x1 = EArms1[ich];
1661     item.mean_x6 = EAmean6[ich];
1662     item.rms_x6 = EArms6[ich];
1663     item.mean_x12 = EAmean12[ich];
1664     item.rms_x12 = EArms12[ich];
1665     if (ich < kEBChannels) {
1666       //    EBDetId ebdetid(fed, chan, EBDetId::SMCRYSTALMODE);
1667       EBDetId ebdetid = EBDetId::unhashIndex(ich);
1668       pedestal->insert(std::make_pair(ebdetid.rawId(), item));
1669     } else {
1670       //      EEDetId eedetid(iX, iY, iz);
1671       int iChannel = ich - kEBChannels;
1672       EEDetId eedetid = EEDetId::unhashIndex(iChannel);
1673       pedestal->insert(std::make_pair(eedetid.rawId(), item));
1674     }
1675   }  // end loop over all channels
1676   Time_t snc = (Time_t)runold;
1677   m_to_transfer.push_back(std::make_pair((EcalPedestals*)pedestal, snc));  // run based IOV
1678   edm::LogInfo("Ecal - > end of readPedestalTree -----------\n");
1679 }
1680 /////////////////////////////////////////////////////////////////////////////////////////////////
1681 
1682 void popcon::EcalPedestalsHandler::readPedestalTimestamp() {
1683   bool debug = false;
1684   edm::LogInfo(" reading the root file ") << m_filename;
1685   TFile* hfile = new TFile(m_filename.c_str());
1686 
1687   TTree* treeChan = (TTree*)hfile->Get("PedChan");
1688   int iChannel = 0, ix = 0, iy = 0, iz = 0;
1689   treeChan->SetBranchAddress("Channels", &iChannel);
1690   treeChan->SetBranchAddress("x", &ix);
1691   treeChan->SetBranchAddress("y", &iy);
1692   treeChan->SetBranchAddress("z", &iz);
1693   int neventsChan = (int)treeChan->GetEntries();
1694   edm::LogInfo("PedChan nb entries ") << neventsChan;
1695   int ringEB[kEBChannels], sideEB[kEBChannels], ixEE[kEEChannels], iyEE[kEEChannels], izEE[kEEChannels];
1696   for (int entry = 0; entry < neventsChan; entry++) {
1697     treeChan->GetEntry(entry);
1698     if (entry < kEBChannels) {
1699       ringEB[iChannel] = (abs(ix) - 1) / 5;  // -85...-1, 1...85 give 0...16
1700       sideEB[iChannel] = 1;
1701       if (ix < 0)
1702         sideEB[iChannel] = 0;
1703       if (debug && entry % 10000 == 0)
1704         edm::LogInfo(" EB channel ") << iChannel << " eta " << ix << " phi " << iy << " side " << sideEB[iChannel]
1705                                      << " ring " << ringEB[iChannel];
1706     } else {
1707       ixEE[iChannel] = ix;
1708       iyEE[iChannel] = iy;
1709       izEE[iChannel] = iz;
1710       if (debug && entry % 1000 == 0)
1711         edm::LogInfo(" EE channel ") << iChannel << " x " << ixEE[iChannel] << " y " << iyEE[iChannel] << " z "
1712                                      << izEE[iChannel];
1713     }
1714   }
1715   //    2016: 26
1716   Int_t pedxml[Nbpedxml] = {271948,
1717                             273634,
1718                             273931,
1719                             274705,
1720                             275403,
1721                             276108,
1722                             276510,
1723                             277169,
1724                             278123,
1725                             278183,
1726                             278246,
1727                             278389,
1728                             278499,
1729                             278693,
1730                             278858,
1731                             278888,
1732                             278931,
1733                             279728,
1734                             280129,
1735                             280263,
1736                             280941,
1737                             281753,
1738                             282631,
1739                             282833,
1740                             283199,
1741                             283766,
1742                             //    2017 : 25
1743                             286535,
1744                             293513,
1745                             293632,
1746                             293732,
1747                             295507,
1748                             295672,
1749                             296391,
1750                             296917,
1751                             297388,
1752                             298481,
1753                             299279,
1754                             299710,
1755                             300186,
1756                             300581,
1757                             301191,
1758                             302006,
1759                             302293,
1760                             302605,
1761                             303436,
1762                             303848,
1763                             304211,
1764                             304680,
1765                             305117,
1766                             305848,
1767                             306176,
1768                             //    2018 : 30
1769                             313760,
1770                             315434,
1771                             315831,
1772                             316299,
1773                             316694,
1774                             316963,
1775                             317399,
1776                             317669,
1777                             318249,
1778                             318747,
1779                             319111,
1780                             319365,
1781                             319700,
1782                             320098,
1783                             320515,
1784                             320905,
1785                             321184,
1786                             321480,
1787                             321855,
1788                             322151,
1789                             322653,
1790                             323261,
1791                             323802,
1792                             324250,
1793                             324632,
1794                             325909,
1795                             326038,
1796                             326652,
1797                             326979,
1798                             327271};
1799 
1800   Int_t run16Index = 0;
1801 
1802   Int_t fed[kChannels], chan[kChannels], id, run, run_type, seq_id, las_id, fill_num, run_num_infill, run_time,
1803       run_time_stablebeam, nxt, time[54];
1804   Float_t ped[kChannels], pedrms[kChannels], lumi, bfield;
1805   TTree* tree = (TTree*)hfile->Get("T");
1806   tree->Print();
1807   tree->SetBranchAddress("id", &id);
1808   tree->SetBranchAddress("run", &run);
1809   tree->SetBranchAddress("run_type", &run_type);
1810   tree->SetBranchAddress("seq_id", &seq_id);
1811   tree->SetBranchAddress("las_id", &las_id);
1812   tree->SetBranchAddress("fill_num", &fill_num);
1813   tree->SetBranchAddress("run_num_infill", &run_num_infill);
1814   tree->SetBranchAddress("run_time", &run_time);
1815   tree->SetBranchAddress("run_time_stablebeam", &run_time_stablebeam);
1816   tree->SetBranchAddress("lumi", &lumi);
1817   tree->SetBranchAddress("bfield", &bfield);
1818   tree->SetBranchAddress("nxt", &nxt);  // nb of filled Crystals
1819   tree->SetBranchAddress("time", time);
1820   tree->SetBranchAddress("fed", fed);
1821   tree->SetBranchAddress("chan", chan);
1822   tree->SetBranchAddress("ped", ped);
1823   tree->SetBranchAddress("pedrms", pedrms);
1824   int nevents = (int)tree->GetEntries();
1825   edm::LogInfo(" nb entries ") << nevents;
1826   std::ofstream fout;
1827   fout.open("copyTimestampPedestals.txt");
1828   if (!fout.is_open()) {
1829     edm::LogInfo("ERROR : cannot open file copyTimestampPedestals.txt");
1830     exit(1);
1831   }
1832   Double_t EAmean1[kChannels], EArms1[kChannels], EAmean6[kChannels], EArms6[kChannels], EAmean12[kChannels],
1833       EArms12[kChannels];
1834   for (int ich = 0; ich < kChannels; ich++) {
1835     EAmean12[ich] = 200.;
1836     EArms12[ich] = 0.;
1837   }
1838   tree->GetEntry(0);
1839   fout << " first run " << run << " fill " << fill_num << " B field " << bfield << " run type " << run_type
1840        << " seq_id " << seq_id << " las_id " << las_id << " run_num_infill " << run_num_infill << " lumi " << lumi
1841        << " nb of Crystals " << nxt << std::endl;
1842   if (debug) {
1843     for (int ich = 0; ich < kChannels; ich++) {
1844       if (ped[ich] != 0.) {
1845         if (ich < kEBChannels)
1846           fout << " channel " << ich << " FED " << fed[ich] << " chan " << chan[ich] << " pedestal " << ped[ich]
1847                << " RMS " << pedrms[ich] << std::endl;
1848         else  //   EE
1849           fout << " channel " << ich << " EE channel " << ich - kEBChannels << " FED " << fed[ich] << " chan "
1850                << chan[ich] << " pedestal " << ped[ich] << " RMS " << pedrms[ich] << std::endl;
1851       }
1852     }
1853   }  // debug
1854 
1855   int runold = -1, fillold = -1, firsttimeFEDold = -1;
1856   int firsttimeFED = -1, firstFillSequence = 0;
1857   bool firstSeqBeforeStable = false;
1858   int transfer = 0;
1859   int first_run_kept = (int)m_firstRun;  //  m_firstRun is unsigned!
1860   int last_run_kept = (int)m_lastRun;    //  m_lastRun is unsigned!
1861   for (int entry = 0; entry < nevents; entry++) {
1862     tree->GetEntry(entry);
1863     if (nxt != kChannels) {
1864       //      fout << " entry " << entry << " run " << run << " sequence " << seq_id << " run_time " << run_time
1865       fout << " entry " << entry << " run " << run << " sequence " << seq_id  // run_time always 0!
1866            << " ***********  Number of channels " << nxt;
1867       //      if(seq_id == 0) {    corrected Sep 15 2017
1868       fout << " rejected" << std::endl;
1869       continue;
1870       //      }
1871       //      else fout << std::endl;
1872     }
1873     if (run_type != 1) {
1874       fout << " entry " << entry << " run " << run << " sequence " << seq_id
1875            << " ***********  run_type ( 1 coll, 2 cosm, 3 circ, 4 test ) = " << run_type << std::endl;
1876       continue;
1877     }
1878     if (las_id != 447) {  //447 = blue laser
1879       fout << " entry " << entry << " run " << run << " sequence " << seq_id
1880            << " ***********  laser wave length = " << las_id << std::endl;
1881       continue;
1882     }
1883     //    if(las_id != 527) {  //527 = green laser
1884     //      fout << " entry " << entry << " run " << run << " sequence " << seq_id
1885     //         << " ***********  laser wave length = " << las_id << std::endl;
1886     //      continue;
1887     //    }
1888     if (bfield < 3.79) {
1889       fout << " entry " << entry << " run " << run << " sequence " << seq_id << " ***********  bfield = " << bfield
1890            << std::endl;
1891       //      continue;  keep these runs
1892     }
1893     fout << " entry " << entry << " run " << run;
1894     if (run_type == 1)
1895       fout << " fill " << fill_num;
1896     fout << " sequence " << seq_id;
1897     if (run_type == 1) {
1898       fout << " stable " << run_time_stablebeam;
1899       if (run_time_stablebeam < first_run_kept) {
1900         fout << " before first wanted " << m_firstRun << std::endl;
1901         continue;
1902       }
1903     }
1904     firsttimeFED = time[0];
1905     for (int ifed = 0; ifed < 54; ifed++) {
1906       if (time[ifed] > firsttimeFEDold && time[ifed] < firsttimeFED)
1907         firsttimeFED = time[ifed];  // take the first AFTER the previous sequence one!...
1908     }
1909     if (firsttimeFED < first_run_kept) {
1910       fout << " time " << firsttimeFED << " before first wanted " << m_firstRun << std::endl;
1911       continue;
1912     }
1913     if (firsttimeFED > last_run_kept) {
1914       fout << " entry " << entry << " time " << firsttimeFED << " after last wanted " << m_lastRun << std::endl;
1915       break;
1916     }
1917     fout << " time " << firsttimeFED << std::endl;
1918     if (firsttimeFED <= firsttimeFEDold) {
1919       edm::LogInfo(" Problem finding the IOV : old one ") << firsttimeFEDold << " new one " << firsttimeFED;
1920       for (int ifed = 0; ifed < 54; ifed++)
1921         edm::LogInfo("Time ") << time[ifed] << " ignore this entry ";
1922       continue;
1923     }
1924     firsttimeFEDold = firsttimeFED;
1925 
1926     //    if(run != runold) firstSeqBeforeStable = false;
1927     if (fill_num != fillold) {
1928       firstSeqBeforeStable = false;
1929       firstFillSequence = 0;
1930     } else
1931       firstFillSequence++;
1932     if (run_type == 1) {
1933       if (run_time_stablebeam > 0) {
1934         if (firsttimeFED < run_time_stablebeam) {
1935           fout << " data taken before stable beam, skip it" << std::endl;
1936           firstSeqBeforeStable = true;
1937           runold = run;
1938           fillold = fill_num;
1939           continue;
1940         }
1941       } else  //  problem with run_time_stablebeam
1942         fout << " *** entry " << entry << " run_time_stablebeam " << run_time_stablebeam << std::endl;
1943       if (firstSeqBeforeStable) {  // this is the first fully filled entry after stable beam
1944         firstSeqBeforeStable = false;
1945         firsttimeFED = run_time_stablebeam;
1946         fout << " first full sequence after stable; change the IOV " << firsttimeFED << std::endl;
1947       }
1948       if (firstFillSequence == 0) {  // first sequence in this fill
1949         if (firsttimeFED > run_time_stablebeam) {
1950           fout << " first full sequence " << firsttimeFED << " after stable " << run_time_stablebeam
1951                << "; change the IOV " << std::endl;
1952           firsttimeFED = run_time_stablebeam;
1953         }
1954       }
1955     }  // only collision runs
1956 
1957     for (int ich = 0; ich < kChannels; ich++) {
1958       if (ped[ich] < 300 && ped[ich] > 100) {
1959         EAmean12[ich] = ped[ich];
1960         EArms12[ich] = pedrms[ich];
1961       }
1962     }
1963     // get gain 1 and 6 results
1964     bool foundNew = false;
1965     if (run != runold) {
1966       for (int i = run16Index; i < Nbpedxml; i++) {
1967         if (run > pedxml[i]) {
1968           fout << " found a new gain 1, 6 file " << pedxml[i] << " at index " << i << std::endl;
1969           run16Index++;
1970           foundNew = true;
1971           //      if(runold < pedxml[i + 1]) break;   why runold??
1972           if (run < pedxml[i + 1])
1973             break;
1974         }
1975       }
1976       if (foundNew) {
1977         int Indexxml = run16Index - 1;
1978         fout << " opening Pedestals_" << pedxml[Indexxml] << ".xml at index " << Indexxml << std::endl;
1979         std::ifstream fxml;
1980         fxml.open(Form("Pedestals_%i.xml", pedxml[Indexxml]));
1981         if (!fxml.is_open()) {
1982           edm::LogInfo("ERROR : cannot open file Pedestals_") << pedxml[Indexxml] << ".xml";
1983           exit(1);
1984         }
1985         std::string dummyLine, mean12, rms12, bid;
1986         for (int i = 0; i < 9; i++)
1987           std::getline(fxml, dummyLine);  // skip first lines
1988         // Barrel
1989         for (int iEBChannel = 0; iEBChannel < kEBChannels; iEBChannel++) {
1990           fxml >> mean12 >> rms12 >> bid;
1991           std::string stt = bid.substr(9, 15);
1992           std::istringstream m6(stt);
1993           m6 >> EAmean6[iEBChannel];
1994           fxml >> bid;
1995           stt = bid.substr(8, 15);
1996           std::istringstream r6(stt);
1997           r6 >> EArms6[iEBChannel];
1998           fxml >> bid;
1999           stt = bid.substr(9, 15);
2000           std::istringstream m1(stt);
2001           m1 >> EAmean1[iEBChannel];
2002           fxml >> bid;
2003           stt = bid.substr(8, 15);
2004           std::istringstream r1(stt);
2005           r1 >> EArms1[iEBChannel];
2006           if (debug && iEBChannel % 10000 == 0)
2007             fout << " EB channel " << iEBChannel << " " << mean12 << " " << rms12 << " " << EAmean6[iEBChannel] << " "
2008                  << EArms6[iEBChannel] << " " << EAmean1[iEBChannel] << " " << EArms1[iEBChannel] << std::endl;
2009           for (int i = 0; i < 3; i++)
2010             std::getline(fxml, dummyLine);  // skip lines
2011         }
2012         for (int i = 0; i < 6; i++)
2013           std::getline(fxml, dummyLine);  // skip lines
2014         for (int iEEChannel = 0; iEEChannel < kEEChannels; iEEChannel++) {
2015           int ich = iEEChannel + kEBChannels;
2016           fxml >> mean12 >> rms12 >> bid;
2017           std::string stt = bid.substr(9, 15);
2018           std::istringstream m6(stt);
2019           m6 >> EAmean6[ich];
2020           fxml >> bid;
2021           stt = bid.substr(8, 15);
2022           std::istringstream r6(stt);
2023           r6 >> EArms6[ich];
2024           fxml >> bid;
2025           stt = bid.substr(9, 15);
2026           std::istringstream m1(stt);
2027           m1 >> EAmean1[ich];
2028           fxml >> bid;
2029           stt = bid.substr(8, 15);
2030           std::istringstream r1(stt);
2031           r1 >> EArms1[ich];
2032           if (debug && iEEChannel % 1000 == 0)
2033             fout << " EE channel " << iEEChannel << " " << mean12 << " " << rms12 << " " << EAmean6[ich] << " "
2034                  << EArms6[ich] << " " << EAmean1[ich] << " " << EArms1[ich] << std::endl;
2035           for (int i = 0; i < 3; i++)
2036             std::getline(fxml, dummyLine);  // skip lines
2037         }
2038         fxml.close();
2039       }  // found a new gain 1 6 file
2040     }    // check gain 1 and 6 results only for new runs
2041 
2042     EcalPedestals* pedestal = new EcalPedestals();
2043     EcalPedestals::Item item;
2044     for (int ich = 0; ich < kChannels; ich++) {
2045       if (debug && ich % 10000 == 0)
2046         fout << " channel " << ich << " ped " << EAmean12[ich] << " RMS " << EArms12[ich] << std::endl;
2047       //
2048       item.mean_x1 = EAmean1[ich];
2049       item.rms_x1 = EArms1[ich];
2050       item.mean_x6 = EAmean6[ich];
2051       item.rms_x6 = EArms6[ich];
2052       item.mean_x12 = EAmean12[ich];
2053       item.rms_x12 = EArms12[ich];
2054       if (ich < kEBChannels) {
2055         //  EBDetId ebdetid(fed, chan, EBDetId::SMCRYSTALMODE);
2056         EBDetId ebdetid = EBDetId::unhashIndex(ich);
2057         pedestal->insert(std::make_pair(ebdetid.rawId(), item));
2058       } else {
2059         //    EEDetId eedetid(iX, iY, iz);
2060         int iChannel = ich - kEBChannels;
2061         EEDetId eedetid = EEDetId::unhashIndex(iChannel);
2062         pedestal->insert(std::make_pair(eedetid.rawId(), item));
2063       }
2064     }  // end loop over all channels
2065     uint64_t iov = (uint64_t)firsttimeFED << 32;
2066     Time_t snc = (Time_t)iov;
2067     transfer++;
2068     fout << " entry " << entry << " transfer " << transfer << " iov " << iov << std::endl;
2069     m_to_transfer.push_back(std::make_pair((EcalPedestals*)pedestal, snc));  // time based IOV
2070     fout << "  m_to_transfer " << firsttimeFED << std::endl;
2071     runold = run;
2072     fillold = fill_num;
2073   }  // end loop over all entries
2074 
2075   edm::LogInfo("Ecal - > end of readPedestalTimestamp -----------\n");
2076 }