Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:56

0001 #include "CondTools/Ecal/interface/EcalADCToGeVHandler.h"
0002 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
0003 #include "CondFormats/EcalObjects/interface/EcalIntercalibConstants.h"
0004 #include "CondTools/Ecal/interface/EcalADCToGeVXMLTranslator.h"
0005 
0006 #include <iostream>
0007 
0008 popcon::EcalADCToGeVHandler::EcalADCToGeVHandler(const edm::ParameterSet& ps)
0009     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalADCToGeVHandler")) {
0010   std::cout << "EcalADCToGeV Source handler constructor\n" << std::endl;
0011   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0012   m_lastRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("lastRun").c_str()));
0013   m_sid = ps.getParameter<std::string>("OnlineDBSID");
0014   m_user = ps.getParameter<std::string>("OnlineDBUser");
0015   m_pass = ps.getParameter<std::string>("OnlineDBPassword");
0016   m_locationsource = ps.getParameter<std::string>("LocationSource");
0017   m_location = ps.getParameter<std::string>("Location");
0018   m_gentag = ps.getParameter<std::string>("GenTag");
0019   m_file_lowfield = ps.getParameter<std::string>("FileLowField");
0020   m_file_highfield = ps.getParameter<std::string>("FileHighField");
0021 
0022   std::cout << m_sid << "/" << m_user << "/" << m_location << "/" << m_gentag << std::endl;
0023 }
0024 
0025 popcon::EcalADCToGeVHandler::~EcalADCToGeVHandler() {}
0026 
0027 void popcon::EcalADCToGeVHandler::getNewObjects() {
0028   std::cout << "------- Ecal - > getNewObjects\n";
0029 
0030   std::ostringstream ss;
0031   ss << "ECAL ";
0032 
0033   unsigned int max_since = 0;
0034   max_since = static_cast<unsigned int>(tagInfo().lastInterval.since);
0035   std::cout << "max_since : " << max_since << std::endl;
0036   bool magnet_high = false;
0037   bool something_to_transfer = false;
0038   if (tagInfo().size) {
0039     Ref ped_db = lastPayload();
0040 
0041     // we parse the last record in the DB and check if it is low or high field
0042 
0043     std::cout << "retrieved last payload " << std::endl;
0044 
0045     EcalADCToGeVConstant the_cal;
0046 
0047     //unused    float adc_eb=ped_db->getEBValue();
0048     float adc_ee = ped_db->getEEValue();
0049 
0050     //  float the_value_high_eb=0.03894;
0051     //      float the_value_high_ee=0.06285;
0052     // float the_value_high_eb=0.03894;
0053     // float the_value_high_ee=0.06378;
0054 
0055     // bool magnet_high=true;
0056     // if(adc_eb!= the_value_high_eb || adc_ee!= the_value_high_ee ) magnet_high=false;
0057 
0058     //unused    float the_value_low_eb=0.03894;
0059     // Run 1 :  float the_value_low_ee=0.05678;
0060     float the_value_low_ee = 0.0590975;
0061     if (adc_ee > the_value_low_ee)
0062       magnet_high = true;
0063   }  // check if there is already a payload
0064   else
0065     something_to_transfer = true;
0066 
0067   // here we connect to the online DB to check the value of the magnetic field
0068   std::cout << "Connecting to ONLINE DB ... " << std::endl;
0069   econn = new EcalCondDBInterface(m_sid, m_user, m_pass);
0070   std::cout << "Connection done" << std::endl;
0071 
0072   std::cout << "Retrieving last run from ONLINE DB ... " << std::endl;
0073   std::map<EcalLogicID, RunDat> rundat;
0074   RunIOV rp;
0075   run_t runmax = 10000000;
0076   std::string location_p5 = "P5_Co";
0077   econn->fetchValidDataSet(&rundat, &rp, location_p5, runmax);
0078 
0079   unsigned long long irun = (unsigned long long)rp.getRunNumber();
0080 
0081   std::cout << "retrieved run number " << irun << std::endl;
0082 
0083   if (irun > max_since) {
0084     // retrieve from last value data record
0085     // always call this method at first run
0086 
0087     std::map<EcalLogicID, RunDCSMagnetDat> dataset;
0088 
0089     econn->fetchDataSet(&dataset, &rp);
0090 
0091     if (dataset.empty()) {
0092       throw(std::runtime_error("Zero rows read back"));
0093     } else {
0094       std::cout << "retrieved magnet current" << std::endl;
0095     }
0096 
0097     float mag_cur = 0;
0098 
0099     std::map<EcalLogicID, RunDCSMagnetDat>::iterator it;
0100     for (it = dataset.begin(); it != dataset.end(); ++it) {
0101       RunDCSMagnetDat a_mag = (*it).second;
0102       mag_cur = a_mag.getMagnetCurrent();
0103     }
0104 
0105     std::string file_ = m_file_highfield;
0106     if (tagInfo().size) {
0107       if (mag_cur > 7000. && magnet_high) {
0108         std::cout << " the magnet is ON and the constants are for magnet ON " << std::endl;
0109 
0110       } else if (mag_cur > 7000. && !magnet_high) {
0111         something_to_transfer = true;
0112         std::cout << " the magnet is ON and the constants are for magnet OFF " << std::endl;
0113         std::cout << " I transfer the ON constants " << std::endl;
0114         file_ = m_file_highfield;
0115 
0116       } else if (mag_cur < 6000. && magnet_high) {
0117         something_to_transfer = true;
0118         std::cout << " the magnet is OFF and the constants are for magnet ON " << std::endl;
0119         std::cout << " I transfer the OFF constants " << std::endl;
0120         file_ = m_file_lowfield;
0121 
0122       } else if (mag_cur < 6000. && !magnet_high) {
0123         std::cout << " the magnet is OFF and the constants are for magnet OFF " << std::endl;
0124         file_ = m_file_lowfield;
0125 
0126       } else {
0127         std::cout << " the magnet is in a strange situation I do nothing ... just be patient " << std::endl;
0128       }
0129     } else {
0130       if (mag_cur > 7000.)
0131         std::cout << " first payload, the magnet is ON " << std::endl;
0132       else if (mag_cur < 6000.) {
0133         std::cout << " first payload, the magnet is OFF " << std::endl;
0134         file_ = m_file_lowfield;
0135       } else
0136         std::cout << " the magnet is in a strange situation I do nothing ... just be patient " << std::endl;
0137     }
0138 
0139     if (something_to_transfer) {
0140       std::cout << "Generating popcon record for run " << irun << "  going to open file " << file_ << "\n"
0141                 << std::flush;
0142       EcalADCToGeVConstant* payload = new EcalADCToGeVConstant;
0143       //      EcalCondHeader   header;   // DBv1
0144       //        EcalADCToGeVXMLTranslator::readXML(file_,header,*payload);   // DBv1
0145       // DBv2 poor xml file :-(
0146       std::ifstream fxml;
0147       fxml.open(file_.c_str());
0148       std::string line, bid, bid2;
0149       float val;
0150       for (int il = 0; il < 4; il++) {
0151         std::getline(fxml, line);
0152         //  std::cout << line << "\n";
0153       }
0154       for (int iPart = 0; iPart < 2; iPart++) {  //  EB, EE
0155         fxml >> bid;
0156         std::size_t begin = bid.find_first_of('>');
0157         std::size_t end = bid.find_last_of('<');
0158         begin++;
0159         std::string str2 = bid.substr(begin, end - begin);
0160         std::size_t endmantissa = str2.find('e');
0161         std::string mantissa = str2.substr(0, endmantissa);
0162         std::size_t string_size = str2.size();
0163         std::string exponent = str2.substr(endmantissa + 1, string_size);
0164         std::istringstream is(mantissa);
0165         is >> val;
0166         float mult;
0167         std::istringstream ise(exponent);
0168         ise >> mult;
0169         val = val * pow(10, mult);
0170         std::cout << " Partition " << iPart << " ADCToGeV " << val << "\n";
0171         if (iPart < 1)
0172           payload->setEBValue(val);
0173         else
0174           payload->setEEValue(val);
0175       }
0176       // end change for DBv2
0177 
0178       Time_t snc = (Time_t)irun;
0179       popcon::PopConSourceHandler<EcalADCToGeVConstant>::m_to_transfer.push_back(std::make_pair(payload, snc));
0180       ss << "Run=" << irun << "_Magnet_changed_" << std::endl;
0181       m_userTextLog = ss.str() + ";";
0182     } else {
0183       std::cout << "Run " << irun << " nothing sent to the DB" << std::endl;
0184       ss << "Run=" << irun << "_Magnet_NOT_changed_" << std::endl;
0185       m_userTextLog = ss.str() + ";";
0186     }
0187 
0188     delete econn;
0189   }  // irun > max_since
0190   else {
0191     std::cout << "Run " << irun << " nothing sent to the DB" << std::endl;
0192     ss << "Run=" << irun << "_no_new_runs_" << std::endl;
0193     m_userTextLog = ss.str() + ";";
0194   }
0195   std::cout << "Ecal - > end of getNewObjects -----------\n";
0196 }