Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "CondTools/Ecal/interface/EcalPulseShapesHandler.h"
0002 
0003 #include <algorithm>
0004 #include <iostream>
0005 #include <fstream>
0006 
0007 popcon::EcalPulseShapesHandler::EcalPulseShapesHandler(const edm::ParameterSet& ps)
0008     : m_name(ps.getUntrackedParameter<std::string>("name", "EcalPulseShapesHandler")) {
0009   std::cout << "EcalPulseShape Source handler constructor\n" << std::endl;
0010   m_firstRun = static_cast<unsigned int>(atoi(ps.getParameter<std::string>("firstRun").c_str()));
0011   m_filename = ps.getParameter<std::string>("inputFileName");
0012   m_EBPulseShapeTemplate = ps.getParameter<std::vector<double> >("EBPulseShapeTemplate");
0013   m_EEPulseShapeTemplate = ps.getParameter<std::vector<double> >("EEPulseShapeTemplate");
0014 }
0015 
0016 popcon::EcalPulseShapesHandler::~EcalPulseShapesHandler() {}
0017 
0018 bool popcon::EcalPulseShapesHandler::checkPulseShape(EcalPulseShapes::Item* item) {
0019   // true means all is standard and OK
0020   bool result = true;
0021   for (int s = 0; s < EcalPulseShape::TEMPLATESAMPLES; ++s) {
0022     if (item->pdfval[s] > 1 || item->pdfval[s] < 0)
0023       result = false;
0024   }
0025   return result;
0026 }
0027 
0028 void popcon::EcalPulseShapesHandler::fillSimPulseShape(EcalPulseShapes::Item* item, bool isbarrel) {
0029   for (int s = 0; s < EcalPulseShape::TEMPLATESAMPLES; ++s) {
0030     item->pdfval[s] = isbarrel ? m_EBPulseShapeTemplate[s] : m_EEPulseShapeTemplate[s];
0031   }
0032 }
0033 
0034 void popcon::EcalPulseShapesHandler::getNewObjects() {
0035   std::cout << "------- Ecal - > getNewObjects\n";
0036 
0037   // create the object pukse shapes
0038   EcalPulseShapes* pulseshapes = new EcalPulseShapes();
0039 
0040   // read the templates from a text file
0041   std::ifstream inputfile;
0042   inputfile.open(m_filename.c_str());
0043   float templatevals[EcalPulseShape::TEMPLATESAMPLES];
0044   unsigned int rawId;
0045   int isbarrel;
0046   std::string line;
0047 
0048   // keep track of bad crystals
0049   int nEBbad(0), nEEbad(0);
0050   int nEBgood(0), nEEgood(0);
0051   std::vector<EBDetId> ebgood;
0052   std::vector<EEDetId> eegood;
0053 
0054   // fill with the measured shapes only for data
0055   if (m_firstRun > 1) {
0056     while (std::getline(inputfile, line)) {
0057       std::istringstream linereader(line);
0058       linereader >> isbarrel >> rawId;
0059       // std::cout << "Inserting template for crystal with rawId = " << rawId << " (isbarrel = " << isbarrel << ") " << std::endl;
0060       for (int s = 0; s < EcalPulseShape::TEMPLATESAMPLES; ++s) {
0061         linereader >> templatevals[s];
0062         // std::cout << templatevals[s] << "\t";
0063       }
0064       // std::cout << std::endl;
0065 
0066       if (!linereader) {
0067         std::cout << "Wrong format of the text file. Exit." << std::endl;
0068         return;
0069       }
0070       EcalPulseShapes::Item item;
0071       for (int s = 0; s < EcalPulseShape::TEMPLATESAMPLES; ++s)
0072         item.pdfval[s] = templatevals[s];
0073 
0074       if (isbarrel) {
0075         EBDetId ebdetid(rawId);
0076         if (!checkPulseShape(&item))
0077           nEBbad++;
0078         else {
0079           nEBgood++;
0080           ebgood.push_back(ebdetid);
0081           pulseshapes->insert(std::make_pair(ebdetid.rawId(), item));
0082         }
0083       } else {
0084         EEDetId eedetid(rawId);
0085         if (!checkPulseShape(&item))
0086           nEEbad++;
0087         else {
0088           nEEgood++;
0089           eegood.push_back(eedetid);
0090           pulseshapes->insert(std::make_pair(eedetid.rawId(), item));
0091         }
0092       }
0093     }
0094   }
0095 
0096   // now fill the bad crystals and simulation with the simulation values (from TB)
0097   std::cout << "Filled the DB with the good measured ECAL templates. Now filling the others with the TB values"
0098             << std::endl;
0099   for (int iEta = -EBDetId::MAX_IETA; iEta <= EBDetId::MAX_IETA; ++iEta) {
0100     if (iEta == 0)
0101       continue;
0102     for (int iPhi = EBDetId::MIN_IPHI; iPhi <= EBDetId::MAX_IPHI; ++iPhi) {
0103       if (EBDetId::validDetId(iEta, iPhi)) {
0104         EBDetId ebdetid(iEta, iPhi, EBDetId::ETAPHIMODE);
0105 
0106         std::vector<EBDetId>::iterator it = find(ebgood.begin(), ebgood.end(), ebdetid);
0107         if (it == ebgood.end()) {
0108           EcalPulseShapes::Item item;
0109           fillSimPulseShape(&item, true);
0110           pulseshapes->insert(std::make_pair(ebdetid.rawId(), item));
0111         }
0112       }
0113     }
0114   }
0115 
0116   for (int iZ = -1; iZ < 2; iZ += 2) {
0117     for (int iX = EEDetId::IX_MIN; iX <= EEDetId::IX_MAX; ++iX) {
0118       for (int iY = EEDetId::IY_MIN; iY <= EEDetId::IY_MAX; ++iY) {
0119         if (EEDetId::validDetId(iX, iY, iZ)) {
0120           EEDetId eedetid(iX, iY, iZ);
0121 
0122           std::vector<EEDetId>::iterator it = find(eegood.begin(), eegood.end(), eedetid);
0123           if (it == eegood.end()) {
0124             EcalPulseShapes::Item item;
0125             fillSimPulseShape(&item, false);
0126             pulseshapes->insert(std::make_pair(eedetid.rawId(), item));
0127           }
0128         }
0129       }
0130     }
0131   }
0132 
0133   std::cout << "Inserted the pulse shapes into the new item object" << std::endl;
0134 
0135   unsigned int irun = m_firstRun;
0136   Time_t snc = (Time_t)irun;
0137 
0138   m_to_transfer.push_back(std::make_pair((EcalPulseShapes*)pulseshapes, snc));
0139 
0140   std::cout << "Ecal - > end of getNewObjects -----------" << std::endl;
0141   std::cout << "N. bad shapes for EB = " << nEBbad << std::endl;
0142   std::cout << "N. bad shapes for EE = " << nEEbad << std::endl;
0143   std::cout << "Written the object" << std::endl;
0144 }