Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:58

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