Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-03 23:51:40

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