Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:57:50

0001 /* 
0002  *  \class EcalStatusAnalyzer
0003  *
0004  *  author: Julie Malcles - CEA/Saclay
0005  *  author: Gautier Hamel De Monchenault - CEA/Saclay
0006  */
0007 
0008 #include "EcalStatusAnalyzer.h"
0009 
0010 #include "TFile.h"
0011 #include "TTree.h"
0012 #include "TCut.h"
0013 #include "TPaveText.h"
0014 #include "TBranch.h"
0015 
0016 #include <fstream>
0017 #include <iostream>
0018 #include <iomanip>
0019 #include <sstream>
0020 #include <ctime>
0021 
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 #include <FWCore/Utilities/interface/Exception.h>
0024 
0025 #include <FWCore/Framework/interface/Event.h>
0026 #include <FWCore/Framework/interface/MakerMacros.h>
0027 #include <FWCore/ParameterSet/interface/ParameterSet.h>
0028 
0029 #include <DataFormats/EcalDetId/interface/EcalDetIdCollections.h>
0030 #include <DataFormats/Provenance/interface/Timestamp.h>
0031 
0032 //========================================================================
0033 EcalStatusAnalyzer::EcalStatusAnalyzer(const edm::ParameterSet& iConfig)
0034     //========================================================================
0035     : iEvent(0),
0036 
0037       // framework parameters with default values
0038       _dataType(iConfig.getUntrackedParameter<std::string>("dataType", "h4")),  // h4 or p5
0039       resdir_(iConfig.getUntrackedParameter<std::string>("resDir")),
0040       statusfile_(iConfig.getUntrackedParameter<std::string>("statusFile")),
0041       eventHeaderCollection_(iConfig.getParameter<std::string>("eventHeaderCollection")),
0042       eventHeaderProducer_(iConfig.getParameter<std::string>("eventHeaderProducer")),
0043       dccToken_(consumes<EcalRawDataCollection>(edm::InputTag(eventHeaderProducer_, eventHeaderCollection_))),
0044       headToken_(consumes<EcalTBEventHeader>(edm::InputTag(eventHeaderProducer_)))
0045 //========================================================================
0046 
0047 {
0048   //now do what ever initialization is needed
0049 }
0050 
0051 //========================================================================
0052 void EcalStatusAnalyzer::beginJob() {
0053   //========================================================================
0054 
0055   // Initializations
0056 
0057   nSM = 0;
0058   fedID = 0;
0059   runType = -999;
0060   runNum = -999;
0061   event = 0;
0062 }
0063 
0064 //========================================================================
0065 void EcalStatusAnalyzer::analyze(const edm::Event& e, const edm::EventSetup& c) {
0066   //========================================================================
0067 
0068   ++iEvent;
0069 
0070   // retrieving DCC header
0071   const edm::Handle<EcalRawDataCollection>& pDCCHeader = e.getHandle(dccToken_);
0072   const EcalRawDataCollection* DCCHeader = (pDCCHeader.isValid()) ? pDCCHeader.product() : nullptr;
0073   if (!pDCCHeader.isValid()) {
0074     edm::LogWarning("EcalStatusAnalyzer")
0075         << "Error! can't get the product  retrieving DCC header " << eventHeaderCollection_.c_str();
0076   }
0077 
0078   // retrieving TB event header
0079 
0080   const edm::Handle<EcalTBEventHeader>& pEventHeader = e.getHandle(headToken_);
0081   const EcalTBEventHeader* evtHeader = (pEventHeader.isValid()) ? pEventHeader.product() : nullptr;
0082   if (_dataType == "h4") {
0083     if (!pEventHeader.isValid()) {
0084       edm::LogWarning("EcalStatusAnalyzer") << "Error! can't get the product " << eventHeaderProducer_.c_str();
0085     }
0086     timeStampCur = evtHeader->begBurstTimeSec();
0087     nSM = evtHeader->smInBeam();
0088   }
0089 
0090   // Get Timestamp
0091 
0092   edm::Timestamp Time = e.time();
0093 
0094   if (_dataType != "h4") {
0095     timeStampCur = Time.value();
0096   }
0097 
0098   // ====================================
0099   // Decode Basic DCCHeader Information
0100   // ====================================
0101 
0102   for (EcalRawDataCollection::const_iterator headerItr = DCCHeader->begin(); headerItr != DCCHeader->end();
0103        ++headerItr) {
0104     // Get run type and run number
0105     runType = headerItr->getRunType();
0106     runNum = headerItr->getRunNumber();
0107     event = headerItr->getLV1();
0108     dccID = headerItr->getDccInTCCCommand();
0109     fedID = headerItr->fedId();
0110 
0111     short VFEGain = headerItr->getMgpaGain();
0112     short MEMGain = headerItr->getMemGain();
0113 
0114     // Retrieve laser color and event number
0115 
0116     EcalDCCHeaderBlock::EcalDCCEventSettings settings = headerItr->getEventSettings();
0117 
0118     int laser_color = settings.wavelength;
0119     int laser_power = settings.LaserPower;
0120     int laser_filter = settings.LaserFilter;
0121     int laser_delay = settings.delay;
0122     if (laser_color < 0)
0123       return;
0124     // int laser_ = settings.MEMVinj;
0125 
0126     bool isLas = false;
0127     bool isTP = false;
0128     bool isPed = false;
0129 
0130     if (runType == EcalDCCHeaderBlock::LASER_STD || runType == EcalDCCHeaderBlock::LASER_GAP ||
0131         runType == EcalDCCHeaderBlock::LASER_POWER_SCAN || runType == EcalDCCHeaderBlock::LASER_DELAY_SCAN)
0132       isLas = true;
0133 
0134     else if (runType == EcalDCCHeaderBlock::TESTPULSE_MGPA || runType == EcalDCCHeaderBlock::TESTPULSE_GAP ||
0135              runType == EcalDCCHeaderBlock::TESTPULSE_SCAN_MEM)
0136       isTP = true;
0137 
0138     else if (runType == EcalDCCHeaderBlock::PEDESTAL_STD || runType == EcalDCCHeaderBlock::PEDESTAL_OFFSET_SCAN ||
0139              runType == EcalDCCHeaderBlock::PEDESTAL_25NS_SCAN)
0140       isPed = true;
0141 
0142     // take event only if the fed corresponds to the DCC in TCC
0143     // and fill gain stuff with value of 1st event
0144 
0145     if (600 + dccID != fedID)
0146       continue;
0147 
0148     bool doesExist = false;
0149 
0150     if ((isFedLasCreated.count(fedID) == 1 && isLas) || (isFedTPCreated.count(fedID) == 1 && isTP) ||
0151         (isFedPedCreated.count(fedID) == 1 && isPed))
0152       doesExist = true;
0153     else if (isLas)
0154       isFedLasCreated[fedID] = 1;
0155     else if (isTP)
0156       isFedTPCreated[fedID] = 1;
0157     else if (isPed)
0158       isFedPedCreated[fedID] = 1;
0159 
0160     if (!doesExist) {
0161       // create new entry for laser
0162 
0163       if (isLas) {
0164         fedIDsLas.push_back(fedID);
0165         dccIDsLas.push_back(dccID);
0166 
0167         timeStampBegLas[fedID] = timeStampCur;
0168         timeStampEndLas[fedID] = timeStampCur;
0169 
0170         nEvtsLas[fedID] = 1;
0171         runTypeLas[fedID] = runType;
0172 
0173         if (laser_color == iBLUE) {
0174           nBlueLas[fedID] = 1;
0175           laserPowerBlue[fedID] = laser_power;
0176           laserFilterBlue[fedID] = laser_filter;
0177           laserDelayBlue[fedID] = laser_delay;
0178         } else if (laser_color == iIR) {
0179           nRedLas[fedID] = 1;
0180           laserPowerRed[fedID] = laser_power;
0181           laserFilterRed[fedID] = laser_filter;
0182           laserDelayRed[fedID] = laser_delay;
0183         }
0184 
0185         MGPAGainLas[fedID] = VFEGain;
0186         MEMGainLas[fedID] = MEMGain;
0187 
0188       }
0189 
0190       // or create new entry for test-pulse
0191       else if (isTP) {
0192         fedIDsTP.push_back(fedID);
0193         dccIDsTP.push_back(dccID);
0194 
0195         nEvtsTP[fedID] = 1;
0196         runTypeTP[fedID] = runType;
0197 
0198         timeStampBegTP[fedID] = timeStampCur;
0199         timeStampEndTP[fedID] = timeStampCur;
0200 
0201         MGPAGainTP[fedID] = VFEGain;
0202         MEMGainTP[fedID] = MEMGain;
0203 
0204         // or create new entry for pedestal
0205 
0206       } else if (isPed) {
0207         fedIDsPed.push_back(fedID);
0208         dccIDsPed.push_back(dccID);
0209 
0210         nEvtsPed[fedID] = 1;
0211         runTypePed[fedID] = runType;
0212 
0213         timeStampBegPed[fedID] = timeStampCur;
0214         timeStampEndPed[fedID] = timeStampCur;
0215 
0216         MGPAGainPed[fedID] = VFEGain;
0217         MEMGainPed[fedID] = MEMGain;
0218       }
0219 
0220     } else {
0221       if (isLas) {
0222         nEvtsLas[fedID]++;
0223         if (laser_color == iBLUE)
0224           nBlueLas[fedID]++;
0225         else if (laser_color == iIR)
0226           nRedLas[fedID]++;
0227 
0228         if (timeStampCur < timeStampBegLas[fedID])
0229           timeStampBegLas[fedID] = timeStampCur;
0230         if (timeStampCur > timeStampEndLas[fedID])
0231           timeStampEndLas[fedID] = timeStampCur;
0232 
0233       } else if (isTP) {
0234         nEvtsTP[fedID]++;
0235         if (timeStampCur < timeStampBegTP[fedID])
0236           timeStampBegTP[fedID] = timeStampCur;
0237         if (timeStampCur > timeStampEndTP[fedID])
0238           timeStampEndTP[fedID] = timeStampCur;
0239       } else if (isPed) {
0240         nEvtsPed[fedID]++;
0241         if (timeStampCur < timeStampBegPed[fedID])
0242           timeStampBegPed[fedID] = timeStampCur;
0243         if (timeStampCur > timeStampEndPed[fedID])
0244           timeStampEndPed[fedID] = timeStampCur;
0245       }
0246     }
0247   }
0248 
0249 }  // analyze
0250 
0251 //========================================================================
0252 void EcalStatusAnalyzer::endJob() {
0253   //========================================================================
0254 
0255   // Create output status file
0256 
0257   std::stringstream namefile;
0258   namefile << resdir_ << "/" << statusfile_;
0259 
0260   std::string statusfile = namefile.str();
0261 
0262   std::ofstream statusFile(statusfile.c_str(), std::ofstream::out);
0263 
0264   if (!fedIDsLas.empty() && fedIDsLas.size() == dccIDsLas.size()) {
0265     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0266     statusFile << "                LASER Events              " << std::endl;
0267     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0268 
0269     for (unsigned int i = 0; i < fedIDsLas.size(); i++) {
0270       statusFile << "RUNTYPE = " << runTypeLas[fedIDsLas.at(i)] << std::endl;
0271       statusFile << "FEDID = " << fedIDsLas.at(i) << std::endl;
0272       statusFile << "DCCID = " << dccIDsLas.at(i) << std::endl;
0273       statusFile << "TIMESTAMP_BEG = " << timeStampBegLas[fedIDsLas.at(i)] << std::endl;
0274       statusFile << "TIMESTAMP_END = " << timeStampEndLas[fedIDsLas.at(i)] << std::endl;
0275       statusFile << "MPGA_GAIN = " << MGPAGainLas[fedIDsLas.at(i)] << std::endl;
0276       statusFile << "MEM_GAIN  = " << MEMGainLas[fedIDsLas.at(i)] << std::endl;
0277       statusFile << "EVENTS = " << nEvtsLas[fedIDsLas.at(i)] << std::endl;
0278 
0279       if (nBlueLas[fedIDsLas.at(i)] > 0) {
0280         statusFile << "            blue laser events = " << nBlueLas[fedIDsLas.at(i)] << std::endl;
0281         statusFile << "            blue laser power  = " << laserPowerBlue[fedIDsLas.at(i)] << std::endl;
0282         statusFile << "            blue laser filter = " << laserFilterBlue[fedIDsLas.at(i)] << std::endl;
0283         statusFile << "            blue laser delay  = " << laserDelayBlue[fedIDsLas.at(i)] << std::endl;
0284       }
0285 
0286       if (nRedLas[fedIDsLas.at(i)] > 0) {
0287         statusFile << "            ired laser events = " << nRedLas[fedIDsLas.at(i)] << std::endl;
0288         statusFile << "            ired laser power  = " << laserPowerRed[fedIDsLas.at(i)] << std::endl;
0289         statusFile << "            ired laser filter = " << laserFilterRed[fedIDsLas.at(i)] << std::endl;
0290         statusFile << "            ired laser delay  = " << laserDelayRed[fedIDsLas.at(i)] << std::endl;
0291       }
0292 
0293       if (i < fedIDsLas.size() - 1)
0294         statusFile << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << std::endl;
0295       else
0296         statusFile << "  " << std::endl;
0297     }
0298   }
0299 
0300   if (!fedIDsTP.empty() && fedIDsTP.size() == dccIDsTP.size()) {
0301     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0302     statusFile << "             TESTPULSE Events            " << std::endl;
0303     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0304 
0305     for (unsigned int i = 0; i < fedIDsTP.size(); i++) {
0306       statusFile << "RUNTYPE = " << runTypeTP[fedIDsTP.at(i)] << std::endl;
0307       statusFile << "FEDID = " << fedIDsTP.at(i) << std::endl;
0308       statusFile << "DCCID = " << dccIDsTP.at(i) << std::endl;
0309       statusFile << "TIMESTAMP_BEG = " << timeStampBegTP[fedIDsTP.at(i)] << std::endl;
0310       statusFile << "TIMESTAMP_END = " << timeStampEndTP[fedIDsTP.at(i)] << std::endl;
0311       statusFile << "MPGA_GAIN = " << MGPAGainTP[fedIDsTP.at(i)] << std::endl;
0312       statusFile << "MEM_GAIN  = " << MEMGainTP[fedIDsTP.at(i)] << std::endl;
0313       statusFile << "EVENTS = " << nEvtsTP[fedIDsTP.at(i)] << std::endl;
0314       if (i < fedIDsTP.size() - 1)
0315         statusFile << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << std::endl;
0316       else
0317         statusFile << "  " << std::endl;
0318     }
0319   }
0320 
0321   if (!fedIDsPed.empty() && fedIDsPed.size() == dccIDsPed.size()) {
0322     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0323     statusFile << "               PEDESTAL Events              " << std::endl;
0324     statusFile << "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=" << std::endl;
0325 
0326     for (unsigned int i = 0; i < fedIDsPed.size(); i++) {
0327       statusFile << "RUNTYPE = " << runTypePed[fedIDsPed.at(i)] << std::endl;
0328       statusFile << "FEDID = " << fedIDsPed.at(i) << std::endl;
0329       statusFile << "DCCID = " << dccIDsPed.at(i) << std::endl;
0330       statusFile << "TIMESTAMP_BEG = " << timeStampBegPed[fedIDsPed.at(i)] << std::endl;
0331       statusFile << "TIMESTAMP_END = " << timeStampEndPed[fedIDsPed.at(i)] << std::endl;
0332       statusFile << "MPGA_GAIN = " << MGPAGainPed[fedIDsPed.at(i)] << std::endl;
0333       statusFile << "MEM_GAIN  = " << MEMGainPed[fedIDsPed.at(i)] << std::endl;
0334       statusFile << "EVENTS = " << nEvtsPed[fedIDsPed.at(i)] << std::endl;
0335       if (i < fedIDsPed.size() - 1)
0336         statusFile << "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << std::endl;
0337       else
0338         statusFile << "  " << std::endl;
0339     }
0340   }
0341   statusFile << " ... header done" << std::endl;
0342 
0343   statusFile.close();
0344 }
0345 
0346 DEFINE_FWK_MODULE(EcalStatusAnalyzer);