Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:07:56

0001 /*
0002  * \file L1TSync.cc
0003  *
0004  * \author J. Pela, P. Musella
0005  *
0006  */
0007 
0008 //
0009 #include "DQM/L1TMonitor/interface/L1TSync.h"
0010 
0011 #include "DQMServices/Core/interface/DQMStore.h"
0012 
0013 #include "DataFormats/Scalers/interface/LumiScalers.h"
0014 #include "DataFormats/Scalers/interface/Level1TriggerRates.h"
0015 #include "DataFormats/Scalers/interface/Level1TriggerScalers.h"
0016 
0017 #include "DataFormats/Common/interface/ConditionsInEdm.h"  // Parameters associated to Run, LS and Event
0018 
0019 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenuFwd.h"
0020 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
0021 #include "CondFormats/L1TObjects/interface/L1GtPrescaleFactors.h"
0022 #include "CondFormats/DataRecord/interface/L1GtTriggerMenuRcd.h"
0023 #include "CondFormats/DataRecord/interface/L1GtPrescaleFactorsAlgoTrigRcd.h"
0024 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
0025 
0026 // Luminosity Information
0027 //#include "DataFormats/Luminosity/interface/LumiDetails.h"
0028 //#include "DataFormats/Luminosity/interface/LumiSummary.h"
0029 
0030 #include "TList.h"
0031 
0032 using namespace edm;
0033 using namespace std;
0034 
0035 //-------------------------------------------------------------------------------------
0036 //-------------------------------------------------------------------------------------
0037 L1TSync::L1TSync(const ParameterSet& pset)
0038     : m_menuToken(esConsumes<edm::Transition::BeginRun>()),
0039       m_helperTokens(L1TMenuHelper::consumes<edm::Transition::BeginRun>(consumesCollector())),
0040       m_l1GtUtils(pset, consumesCollector(), false, *this) {
0041   m_parameters = pset;
0042 
0043   // Mapping parameter input variables
0044   m_l1GtDataDaqInputTag = consumes<L1GlobalTriggerReadoutRecord>(pset.getParameter<InputTag>("inputTagL1GtDataDaq"));
0045   m_l1GtEvmSource = consumes<L1GlobalTriggerEvmReadoutRecord>(pset.getParameter<InputTag>("inputTagtEvmSource"));
0046   m_verbose = pset.getUntrackedParameter<bool>("verbose", false);
0047   m_refPrescaleSet = pset.getParameter<int>("refPrescaleSet");
0048 
0049   // Getting which categories to monitor
0050   ParameterSet Categories = pset.getParameter<ParameterSet>("Categories");
0051 
0052   bool forceGlobalParameters = Categories.getParameter<bool>("forceGlobalParameters");
0053   bool doGlobalAutoSelection = Categories.getParameter<bool>("doGlobalAutoSelection");
0054 
0055   ParameterSet CatBPTX = Categories.getParameter<ParameterSet>("BPTX");
0056   ParameterSet CatMu = Categories.getParameter<ParameterSet>("Mu");
0057   ParameterSet CatEG = Categories.getParameter<ParameterSet>("EG");
0058   ParameterSet CatIsoEG = Categories.getParameter<ParameterSet>("IsoEG");
0059   ParameterSet CatJet = Categories.getParameter<ParameterSet>("Jet");
0060   ParameterSet CatCenJet = Categories.getParameter<ParameterSet>("CenJet");
0061   ParameterSet CatForJet = Categories.getParameter<ParameterSet>("ForJet");
0062   ParameterSet CatTauJet = Categories.getParameter<ParameterSet>("TauJet");
0063   ParameterSet CatETM = Categories.getParameter<ParameterSet>("ETT");
0064   ParameterSet CatETT = Categories.getParameter<ParameterSet>("ETM");
0065   ParameterSet CatHTT = Categories.getParameter<ParameterSet>("HTT");
0066   ParameterSet CatHTM = Categories.getParameter<ParameterSet>("HTM");
0067 
0068   // --> Setting parameters related to which algos to monitor
0069   // If global parameters are forced they take precedence over algo-by-algo parameters
0070   if (forceGlobalParameters) {
0071     // If global automatic selection if enable all categories set to be monitored will have
0072     // their algos auto selected (the lowest prescale algo will be selected)
0073     if (doGlobalAutoSelection) {
0074       if (CatMu.getParameter<bool>("monitor")) {
0075         m_algoAutoSelect["Mu"] = true;
0076       } else {
0077         m_algoAutoSelect["Mu"] = false;
0078       }
0079       if (CatEG.getParameter<bool>("monitor")) {
0080         m_algoAutoSelect["EG"] = true;
0081       } else {
0082         m_algoAutoSelect["EG"] = false;
0083       }
0084       if (CatIsoEG.getParameter<bool>("monitor")) {
0085         m_algoAutoSelect["IsoEG"] = true;
0086       } else {
0087         m_algoAutoSelect["IsoEG"] = false;
0088       }
0089       if (CatJet.getParameter<bool>("monitor")) {
0090         m_algoAutoSelect["Jet"] = true;
0091       } else {
0092         m_algoAutoSelect["Jet"] = false;
0093       }
0094       if (CatCenJet.getParameter<bool>("monitor")) {
0095         m_algoAutoSelect["CenJet"] = true;
0096       } else {
0097         m_algoAutoSelect["CenJet"] = false;
0098       }
0099       if (CatForJet.getParameter<bool>("monitor")) {
0100         m_algoAutoSelect["ForJet"] = true;
0101       } else {
0102         m_algoAutoSelect["ForJet"] = false;
0103       }
0104       if (CatTauJet.getParameter<bool>("monitor")) {
0105         m_algoAutoSelect["TauJet"] = true;
0106       } else {
0107         m_algoAutoSelect["TauJet"] = false;
0108       }
0109       if (CatETM.getParameter<bool>("monitor")) {
0110         m_algoAutoSelect["ETM"] = true;
0111       } else {
0112         m_algoAutoSelect["ETM"] = false;
0113       }
0114       if (CatETT.getParameter<bool>("monitor")) {
0115         m_algoAutoSelect["ETT"] = true;
0116       } else {
0117         m_algoAutoSelect["ETT"] = false;
0118       }
0119       if (CatHTM.getParameter<bool>("monitor")) {
0120         m_algoAutoSelect["HTM"] = true;
0121       } else {
0122         m_algoAutoSelect["HTM"] = false;
0123       }
0124       if (CatHTT.getParameter<bool>("monitor")) {
0125         m_algoAutoSelect["HTT"] = true;
0126       } else {
0127         m_algoAutoSelect["HTT"] = false;
0128       }
0129 
0130       // If global non-automatic selection is enable all categories set to be monitored will use
0131       // user defined algos
0132     } else {
0133       m_algoAutoSelect["Mu"] = false;
0134       m_algoAutoSelect["EG"] = false;
0135       m_algoAutoSelect["IsoEG"] = false;
0136       m_algoAutoSelect["Jet"] = false;
0137       m_algoAutoSelect["CenJet"] = false;
0138       m_algoAutoSelect["ForJet"] = false;
0139       m_algoAutoSelect["TauJet"] = false;
0140       m_algoAutoSelect["ETM"] = false;
0141       m_algoAutoSelect["ETT"] = false;
0142       m_algoAutoSelect["HTM"] = false;
0143       m_algoAutoSelect["HTT"] = false;
0144 
0145       if (CatMu.getParameter<bool>("monitor")) {
0146         m_selectedTriggers["Mu"] = CatMu.getParameter<string>("algo");
0147       }
0148       if (CatEG.getParameter<bool>("monitor")) {
0149         m_selectedTriggers["EG"] = CatEG.getParameter<string>("algo");
0150       }
0151       if (CatIsoEG.getParameter<bool>("monitor")) {
0152         m_selectedTriggers["IsoEG"] = CatIsoEG.getParameter<string>("algo");
0153       }
0154       if (CatJet.getParameter<bool>("monitor")) {
0155         m_selectedTriggers["Jet"] = CatJet.getParameter<string>("algo");
0156       }
0157       if (CatCenJet.getParameter<bool>("monitor")) {
0158         m_selectedTriggers["CenJet"] = CatCenJet.getParameter<string>("algo");
0159       }
0160       if (CatForJet.getParameter<bool>("monitor")) {
0161         m_selectedTriggers["CatForJet"] = CatForJet.getParameter<string>("algo");
0162       }
0163       if (CatTauJet.getParameter<bool>("monitor")) {
0164         m_selectedTriggers["TauJet"] = CatTauJet.getParameter<string>("algo");
0165       }
0166       if (CatETM.getParameter<bool>("monitor")) {
0167         m_selectedTriggers["ETM"] = CatETM.getParameter<string>("algo");
0168       }
0169       if (CatETT.getParameter<bool>("monitor")) {
0170         m_selectedTriggers["ETT"] = CatETT.getParameter<string>("algo");
0171       }
0172       if (CatHTM.getParameter<bool>("monitor")) {
0173         m_selectedTriggers["HTM"] = CatHTM.getParameter<string>("algo");
0174       }
0175       if (CatHTT.getParameter<bool>("monitor")) {
0176         m_selectedTriggers["HTT"] = CatHTT.getParameter<string>("algo");
0177       }
0178     }
0179 
0180     // If we are using algo-by-algo parametes we get them and set what is needed
0181   } else {
0182     if (CatBPTX.getParameter<bool>("monitor")) {
0183       m_selectedTriggers["BPTX"] = CatBPTX.getParameter<string>("algo");
0184     }
0185 
0186     if (CatMu.getParameter<bool>("monitor")) {
0187       m_algoAutoSelect["Mu"] = CatMu.getParameter<bool>("doAutoSelection");
0188       if (!m_algoAutoSelect["Mu"]) {
0189         m_selectedTriggers["Mu"] = CatMu.getParameter<string>("algo");
0190       }
0191     } else {
0192       m_algoAutoSelect["Mu"] = false;
0193     }
0194 
0195     if (CatEG.getParameter<bool>("monitor")) {
0196       m_algoAutoSelect["EG"] = CatEG.getParameter<bool>("doAutoSelection");
0197       if (!m_algoAutoSelect["EG"]) {
0198         m_selectedTriggers["EG"] = CatEG.getParameter<string>("algo");
0199       }
0200     } else {
0201       m_algoAutoSelect["EG"] = false;
0202     }
0203 
0204     if (CatIsoEG.getParameter<bool>("monitor")) {
0205       m_algoAutoSelect["IsoEG"] = CatIsoEG.getParameter<bool>("doAutoSelection");
0206       if (!m_algoAutoSelect["IsoEG"]) {
0207         m_selectedTriggers["IsoEG"] = CatIsoEG.getParameter<string>("algo");
0208       }
0209     } else {
0210       m_algoAutoSelect["IsoEG"] = false;
0211     }
0212 
0213     if (CatJet.getParameter<bool>("monitor")) {
0214       m_algoAutoSelect["Jet"] = CatJet.getParameter<bool>("doAutoSelection");
0215       if (!m_algoAutoSelect["Jet"]) {
0216         m_selectedTriggers["Jet"] = CatJet.getParameter<string>("algo");
0217       }
0218     } else {
0219       m_algoAutoSelect["Jet"] = false;
0220     }
0221 
0222     if (CatCenJet.getParameter<bool>("monitor")) {
0223       m_algoAutoSelect["CenJet"] = CatCenJet.getParameter<bool>("doAutoSelection");
0224       if (!m_algoAutoSelect["CenJet"]) {
0225         m_selectedTriggers["CenJet"] = CatCenJet.getParameter<string>("algo");
0226       }
0227     } else {
0228       m_algoAutoSelect["CenJet"] = false;
0229     }
0230 
0231     if (CatForJet.getParameter<bool>("monitor")) {
0232       m_algoAutoSelect["CatForJet"] = CatForJet.getParameter<bool>("doAutoSelection");
0233       if (!m_algoAutoSelect["CatForJet"]) {
0234         m_selectedTriggers["CatForJet"] = CatForJet.getParameter<string>("algo");
0235       }
0236     } else {
0237       m_algoAutoSelect["CatForJet"] = false;
0238     }
0239 
0240     if (CatTauJet.getParameter<bool>("monitor")) {
0241       m_algoAutoSelect["TauJet"] = CatTauJet.getParameter<bool>("doAutoSelection");
0242       if (!m_algoAutoSelect["TauJet"]) {
0243         m_selectedTriggers["TauJet"] = CatTauJet.getParameter<string>("algo");
0244       }
0245     } else {
0246       m_algoAutoSelect["TauJet"] = false;
0247     }
0248 
0249     if (CatETM.getParameter<bool>("monitor")) {
0250       m_algoAutoSelect["ETM"] = CatETM.getParameter<bool>("doAutoSelection");
0251       if (!m_algoAutoSelect["ETM"]) {
0252         m_selectedTriggers["ETM"] = CatETM.getParameter<string>("algo");
0253       }
0254     } else {
0255       m_algoAutoSelect["ETM"] = false;
0256     }
0257 
0258     if (CatETT.getParameter<bool>("monitor")) {
0259       m_algoAutoSelect["ETT"] = CatETT.getParameter<bool>("doAutoSelection");
0260       if (!m_algoAutoSelect["ETT"]) {
0261         m_selectedTriggers["ETT"] = CatETT.getParameter<string>("algo");
0262       }
0263     } else {
0264       m_algoAutoSelect["ETT"] = false;
0265     }
0266 
0267     if (CatHTM.getParameter<bool>("monitor")) {
0268       m_algoAutoSelect["HTM"] = CatHTM.getParameter<bool>("doAutoSelection");
0269       if (!m_algoAutoSelect["HTM"]) {
0270         m_selectedTriggers["HTM"] = CatHTM.getParameter<string>("algo");
0271       }
0272     } else {
0273       m_algoAutoSelect["HTM"] = false;
0274     }
0275 
0276     if (CatHTT.getParameter<bool>("monitor")) {
0277       m_algoAutoSelect["HTT"] = CatHTT.getParameter<bool>("doAutoSelection");
0278       if (!m_algoAutoSelect["HTT"]) {
0279         m_selectedTriggers["HTT"] = CatHTT.getParameter<string>("algo");
0280       }
0281     } else {
0282       m_algoAutoSelect["HTT"] = false;
0283     }
0284   }
0285 
0286   m_outputFile = pset.getUntrackedParameter<std::string>("outputFile", "");
0287 
0288   if (!m_outputFile.empty()) {
0289     std::cout << "L1T Monitoring histograms will be saved to " << m_outputFile.c_str() << std::endl;
0290   }
0291 
0292   bool disable = pset.getUntrackedParameter<bool>("disableROOToutput", false);
0293   if (disable) {
0294     m_outputFile = "";
0295   }
0296 }
0297 
0298 //-------------------------------------------------------------------------------------
0299 //-------------------------------------------------------------------------------------
0300 L1TSync::~L1TSync() {}
0301 
0302 //-------------------------------------------------------------------------------------
0303 /// BeginRun
0304 void L1TSync::dqmBeginRun(edm::Run const&, edm::EventSetup const&) {
0305   //
0306   if (m_verbose) {
0307     cout << "[L1TSync] Called beginRun." << endl;
0308   }
0309 }
0310 //-------------------------------------------------------------------------------------
0311 void L1TSync::bookHistograms(DQMStore::IBooker& ibooker, const edm::Run&, const edm::EventSetup& iSetup) {
0312   // Initializing variables
0313   int maxNbins = 2501;
0314 
0315   // Reseting run dependent variables
0316   m_lhcFill = 0;
0317   m_currentLS = 0;
0318   m_certFirstLS.clear();
0319   m_certLastLS.clear();
0320 
0321   // Getting Trigger menu from GT
0322   const L1GtTriggerMenu& menu = iSetup.getData(m_menuToken);
0323 
0324   // Filling Alias-Bit Map
0325   for (const auto& algo : menu.gtAlgorithmAliasMap()) {
0326     m_algoBit[algo.second.algoAlias()] = algo.second.algoBitNumber();
0327   }
0328 
0329   // Getting fill number for this run
0330   //Handle<ConditionsInRunBlock> runConditions;
0331   //iRun.getByType(runConditions);
0332   //int lhcFillNumber = runConditions->lhcFillNumber;
0333 
0334   //ESHandle<L1GtPrescaleFactors> l1GtPfAlgo;
0335   //iSetup.get<L1GtPrescaleFactorsAlgoTrigRcd>().get(l1GtPfAlgo);
0336   //const L1GtPrescaleFactors* m_l1GtPfAlgo = l1GtPfAlgo.product();
0337 
0338   L1TMenuHelper myMenuHelper = L1TMenuHelper(iSetup, m_helperTokens);
0339 
0340   m_selectedTriggers = myMenuHelper.testAlgos(m_selectedTriggers);
0341 
0342   m_l1GtUtils.retrieveL1EventSetup(iSetup);
0343   map<string, string> tAutoSelTrig = myMenuHelper.getLUSOTrigger(m_algoAutoSelect, m_refPrescaleSet, m_l1GtUtils);
0344   m_selectedTriggers.insert(tAutoSelTrig.begin(), tAutoSelTrig.end());
0345 
0346   // Initializing DQM Monitor Elements
0347   ibooker.setCurrentFolder("L1T/L1TSync");
0348   m_ErrorMonitor = ibooker.book1D("ErrorMonitor", "ErrorMonitor", 7, 0, 7);
0349   m_ErrorMonitor->setBinLabel(UNKNOWN, "UNKNOWN");
0350   m_ErrorMonitor->setBinLabel(WARNING_DB_CONN_FAILED, "WARNING_DB_CONN_FAILED");    // Errors from L1TOMDSHelper
0351   m_ErrorMonitor->setBinLabel(WARNING_DB_QUERY_FAILED, "WARNING_DB_QUERY_FAILED");  // Errors from L1TOMDSHelper
0352   m_ErrorMonitor->setBinLabel(WARNING_DB_INCORRECT_NBUNCHES,
0353                               "WARNING_DB_INCORRECT_NBUNCHES");  // Errors from L1TOMDSHelper
0354   m_ErrorMonitor->setBinLabel(ERROR_UNABLE_RETRIVE_PRODUCT, "ERROR_UNABLE_RETRIVE_PRODUCT");
0355   m_ErrorMonitor->setBinLabel(ERROR_TRIGGERALIAS_NOTVALID, "ERROR_TRIGGERALIAS_NOTVALID");
0356   m_ErrorMonitor->setBinLabel(ERROR_LSBLOCK_NOTVALID, "ERROR_LSBLOCK_NOTVALID");
0357 
0358   // Looping over selected triggers
0359   for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0360     string tCategory = (*i).first;
0361     string tTrigger = (*i).second;
0362 
0363     // Initializing LS blocks for certification
0364     m_certFirstLS[(*i).second] = 0;
0365     m_certLastLS[(*i).second] = 0;
0366 
0367     // Initializing DQM Monitors
0368     ibooker.setCurrentFolder("L1T/L1TSync/AlgoVsBunchStructure/");
0369     m_algoVsBunchStructure[tTrigger] = ibooker.book2D(
0370         tCategory, "min #Delta(" + tTrigger + ",Bunch)", maxNbins, -0.5, double(maxNbins) - 0.5, 5, -2.5, 2.5);
0371     m_algoVsBunchStructure[tTrigger]->setAxisTitle("Lumi Section", 1);
0372 
0373     ibooker.setCurrentFolder("L1T/L1TSync/Certification/");
0374     m_algoCertification[tTrigger] =
0375         ibooker.book1D(tCategory, "fraction of in sync: " + tTrigger, maxNbins, -0.5, double(maxNbins) - 0.5);
0376     m_algoCertification[tTrigger]->setAxisTitle("Lumi Section", 1);
0377   }
0378 }
0379 
0380 //_____________________________________________________________________
0381 // Function: beginLuminosityBlock
0382 //_____________________________________________________________________
0383 void L1TSync::beginLuminosityBlock(LuminosityBlock const& lumiBlock, EventSetup const& c) {
0384   if (m_verbose) {
0385     cout << "[L1TSync] Called beginLuminosityBlock." << endl;
0386   }
0387 
0388   // If this LS is not the next to last one force closing of current LS blocks
0389   // for certification
0390   if (m_currentLS != 0 && m_currentLS + 1 != lumiBlock.id().luminosityBlock()) {
0391     if (m_verbose) {
0392       cout << "[L1TSync] None consecutive: doFractionInSync() - LAST=" << m_currentLS
0393            << " CURRENT=" << lumiBlock.id().luminosityBlock() << endl;
0394     }
0395     doFractionInSync(true, false);
0396   }
0397 
0398   // Updating current LS number
0399   m_currentLS = lumiBlock.id().luminosityBlock();
0400 
0401   // If this is the fist valid LS update first LS for certification
0402   for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0403     string theTriggerAlias = (*i).second;
0404     if (m_certFirstLS[theTriggerAlias] == 0) {
0405       m_certFirstLS[theTriggerAlias] = m_currentLS;
0406     }
0407   }
0408 
0409   // A LS will be valid if:
0410   //   * BeamMode == STABLE for all events monitored
0411   m_currentLSValid = true;
0412 }
0413 
0414 //_____________________________________________________________________
0415 // Function: endLuminosityBlock
0416 // * Fills LS by LS ration of trigger out of sync
0417 //_____________________________________________________________________
0418 void L1TSync::endLuminosityBlock(LuminosityBlock const& lumiBlock, EventSetup const& c) {
0419   if (m_verbose) {
0420     cout << "[L1TSync] Called endLuminosityBlock." << endl;
0421   }
0422 
0423   if (m_verbose) {
0424     cout << "[L1TSync] m_currentLSValid      : " << m_currentLSValid << endl;
0425     cout << "[L1TSync] m_beamConfig.isValid(): " << m_beamConfig.isValid() << endl;
0426   }
0427 
0428   for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0429     // Update last LS for certification
0430     string theTriggerAlias = (*i).second;
0431     m_certLastLS[theTriggerAlias] = m_currentLS;
0432   }
0433 
0434   // If this LS is valid (i.e. all events recorded with stable beams)
0435   if (m_currentLSValid && m_beamConfig.isValid()) {
0436     if (m_verbose) {
0437       cout << "[L1TSync] Regular call: doFractionInSync()" << endl;
0438     }
0439     doFractionInSync(false, false);
0440 
0441   }
0442   // If this LS is not valid it can be in the following context:
0443   //  * We still hadn't stable beam (no need to certify nothing
0444   //  * Beam just got unstable or dumped (we may have a complete block of data do certify)
0445   else {
0446     //-> First we close all blocks from certFirstLS[] to m_currentLS-1
0447     for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0448       string theTriggerAlias = (*i).second;
0449 
0450       int fLs = m_certFirstLS[theTriggerAlias];
0451       int lLS = m_certLastLS[theTriggerAlias];
0452 
0453       // If this is a single LS block we do nothing (in this step)
0454       if (fLs == lLS) {
0455         m_certFirstLS[theTriggerAlias] = 0;
0456         m_certLastLS[theTriggerAlias] = 0;
0457       }
0458       // If block is multi LS then we remove the current LS
0459       else {
0460         m_certLastLS[theTriggerAlias] = m_currentLS - 1;
0461       }
0462     }
0463     doFractionInSync(true, false);
0464 
0465     //-> Second we mark this single LS bad for all triggers
0466     for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0467       string theTriggerAlias = (*i).second;
0468       m_certFirstLS[theTriggerAlias] = m_currentLS;
0469       m_certLastLS[theTriggerAlias] = m_currentLS;
0470     }
0471     doFractionInSync(true, true);
0472 
0473     if (m_verbose) {
0474       cout << "[L1TSync] Error call: doFractionInSync()" << endl;
0475     }
0476   }
0477 }
0478 
0479 //_____________________________________________________________________
0480 void L1TSync::analyze(const Event& iEvent, const EventSetup& eventSetup) {
0481   if (m_verbose) {
0482     cout << "[L1TSync] Called analyze." << endl;
0483   }
0484 
0485   // We only start analyzing if current LS is still valid
0486   if (m_currentLSValid) {
0487     if (m_verbose) {
0488       cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;
0489     }
0490 
0491     // Retriving information from GT
0492     edm::Handle<L1GlobalTriggerEvmReadoutRecord> gtEvmReadoutRecord;
0493     iEvent.getByToken(m_l1GtEvmSource, gtEvmReadoutRecord);
0494 
0495     // Determining beam mode and fill number
0496     if (gtEvmReadoutRecord.isValid()) {
0497       const L1GtfeExtWord& gtfeEvmWord = gtEvmReadoutRecord->gtfeWord();
0498       unsigned int lhcBeamMode = gtfeEvmWord.beamMode();  // Updating beam mode
0499 
0500       if (m_lhcFill == 0) {
0501         m_lhcFill = gtfeEvmWord.lhcFillNumber();  // Getting LHC Fill Number from GT
0502         getBeamConfOMDS();                        // Getting Beam Configuration from OMDS
0503       }
0504 
0505       if (lhcBeamMode != STABLE) {
0506         m_currentLSValid = false;
0507       }  // If Beams are not stable we invalidate this LS
0508 
0509     } else {
0510       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
0511       eCount++;
0512       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT, eCount);
0513     }
0514   } else {
0515     if (m_verbose) {
0516       cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;
0517     }
0518   }
0519 
0520   //------------------------------------------------------------------------------
0521   // If current LS is valid and Beam Configuration is Valid we analyse this event
0522   //------------------------------------------------------------------------------
0523   if (m_currentLSValid && m_beamConfig.isValid()) {
0524     // Getting Final Decision Logic (FDL) Data from GT
0525     edm::Handle<L1GlobalTriggerReadoutRecord> gtReadoutRecordData;
0526     iEvent.getByToken(m_l1GtDataDaqInputTag, gtReadoutRecordData);
0527 
0528     if (gtReadoutRecordData.isValid()) {
0529       const vector<L1GtFdlWord>& gtFdlVectorData = gtReadoutRecordData->gtFdlVector();
0530 
0531       // Running over selected triggers
0532       for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0533         string tTrigger = (*i).second;
0534 
0535         // Analyse only defined triggers
0536         if (tTrigger != "Undefined" && tTrigger != "Undefined (Wrong Name)") {
0537           bool beamSingleConfig = false;  // Single beam configured for this event
0538           bool firedAlgo = false;         // Algo fired in this event
0539           int eventBx = -1;
0540 
0541           // Running over FDL results to get which bits fired
0542           for (unsigned int a = 0; a < gtFdlVectorData.size(); a++) {
0543             // Selecting the FDL that triggered
0544             if (gtFdlVectorData[a].bxInEvent() == 0) {
0545               eventBx = gtFdlVectorData[a].localBxNr();
0546               if (gtFdlVectorData[a].gtDecisionWord()[m_algoBit[tTrigger]]) {
0547                 firedAlgo = true;
0548               }
0549             }
0550           }
0551 
0552           // Checking beam configuration
0553           if (m_beamConfig.beam1[eventBx] && !m_beamConfig.beam2[eventBx]) {
0554             beamSingleConfig = true;
0555           }
0556           if (!m_beamConfig.beam1[eventBx] && m_beamConfig.beam2[eventBx]) {
0557             beamSingleConfig = true;
0558           }
0559 
0560           // Analyse only if this trigger fired in this event
0561           // NOTE: Veto cases where a single beam is configured since
0562           //       for this cases this could be a real-satelite bunch collision
0563           // -> Calculate the minimum bx diference between this event and a configured bx
0564           if (firedAlgo && !beamSingleConfig) {
0565             int DifAlgoVsBunchStructure = 9999;  // Majorated
0566 
0567             for (unsigned int a = 0; a < gtFdlVectorData.size(); a++) {
0568               int bxFDL = gtFdlVectorData[a].localBxNr();
0569               int bxInEvent = gtFdlVectorData[a].bxInEvent();
0570 
0571               if (m_beamConfig.bxConfig(bxFDL) && abs(bxInEvent) < abs(DifAlgoVsBunchStructure)) {
0572                 DifAlgoVsBunchStructure = -1 * bxInEvent;
0573               }
0574             }
0575 
0576             m_algoVsBunchStructure[tTrigger]->Fill(m_currentLS, DifAlgoVsBunchStructure);
0577           }
0578         }
0579       }
0580     } else {
0581       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
0582       eCount++;
0583       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT, eCount);
0584     }
0585   }
0586 }
0587 
0588 //_____________________________________________________________________
0589 // Method: getBunchStructureOMDS
0590 // Description: Attempt to retrive Beam Configuration from OMDS and if
0591 //              we find error handle it
0592 //_____________________________________________________________________
0593 void L1TSync::getBeamConfOMDS() {
0594   //Getting connection paremeters
0595   string oracleDB = m_parameters.getParameter<string>("oracleDB");
0596   string pathCondDB = m_parameters.getParameter<string>("pathCondDB");
0597 
0598   // Connecting to OMDS
0599   L1TOMDSHelper myOMDSHelper = L1TOMDSHelper();
0600   int conError;
0601   myOMDSHelper.connect(oracleDB, pathCondDB, conError);
0602 
0603   if (conError == L1TOMDSHelper::NO_ERROR) {
0604     if (m_verbose) {
0605       cout << "[L1TSync] Connected to DB with no error." << endl;
0606     }
0607 
0608     int errorRetrive;
0609     m_beamConfig = myOMDSHelper.getBeamConfiguration(m_lhcFill, errorRetrive);
0610 
0611     if (errorRetrive == L1TOMDSHelper::NO_ERROR) {
0612       if (m_verbose) {
0613         cout << "[L1TSync] Retriving LHC Bunch Structure: NO_ERROR" << endl;
0614         cout << "[L1TSync] -> LHC Bunch Structure valid=" << m_beamConfig.m_valid
0615              << " nBunches=" << m_beamConfig.nCollidingBunches << endl;
0616       }
0617     } else if (errorRetrive == L1TOMDSHelper::WARNING_DB_CONN_FAILED) {
0618       if (m_verbose) {
0619         cout << "[L1TSync] Retriving LHC Bunch Structure: WARNING_DB_CONN_FAILED" << endl;
0620       }
0621 
0622       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_INCORRECT_NBUNCHES);
0623       eCount++;
0624       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_INCORRECT_NBUNCHES, eCount);
0625     } else if (errorRetrive == L1TOMDSHelper::WARNING_DB_QUERY_FAILED) {
0626       if (m_verbose) {
0627         cout << "[L1TSync] Retriving LHC Bunch Structure: WARNING_DB_QUERY_FAILED" << endl;
0628       }
0629 
0630       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_QUERY_FAILED);
0631       eCount++;
0632       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_QUERY_FAILED, eCount);
0633     } else {
0634       if (m_verbose) {
0635         cout << "[L1TSync] Retriving LHC Bunch Structure: UNKNOWN" << endl;
0636       }
0637 
0638       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
0639       eCount++;
0640       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN, eCount);
0641     }
0642 
0643   } else {
0644     if (conError == L1TOMDSHelper::WARNING_DB_CONN_FAILED) {
0645       if (m_verbose) {
0646         cout << "[L1TSync] Connect to DB: WARNING_DB_CONN_FAILED" << endl;
0647       }
0648 
0649       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_CONN_FAILED);
0650       eCount++;
0651       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_CONN_FAILED, eCount);
0652     } else {
0653       if (m_verbose) {
0654         cout << "[L1TSync] Connect to DB: UNKNOWN" << endl;
0655       }
0656 
0657       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
0658       eCount++;
0659       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN, eCount);
0660     }
0661   }
0662 }
0663 
0664 //_____________________________________________________________________
0665 // Method: doFractionInSync
0666 // Description: Produce plot with the fraction of in sync trigger for
0667 //              LS blocks with enough statistics.
0668 // Variable: iForce - Forces closing of all blocks and calculation of
0669 //                    the respective fractions
0670 // Variable: iBad   - (Only works with iForce=true) Forces the current
0671 //                    all current blocks to be marked as bad
0672 //_____________________________________________________________________
0673 void L1TSync::doFractionInSync(bool iForce, bool iBad) {
0674   for (map<string, string>::const_iterator i = m_selectedTriggers.begin(); i != m_selectedTriggers.end(); i++) {
0675     string theCategory = (*i).first;
0676     string theTriggerAlias = (*i).second;
0677 
0678     // Caching frequently used values from maps
0679     unsigned int fLS = m_certFirstLS[theTriggerAlias];
0680     unsigned int lLS = m_certLastLS[theTriggerAlias];
0681 
0682     // Checking validity of the trigger alias and of the LS block
0683     bool triggerAlias_isValid = theTriggerAlias != "Undefined" && theTriggerAlias != "Undefined (Wrong Name)";
0684     bool lsBlock_exists = !(fLS == 0 && lLS == 0);
0685     bool lsBlock_isValid = fLS <= lLS && fLS > 0 && lLS > 0;
0686 
0687     if (triggerAlias_isValid && lsBlock_exists && lsBlock_isValid) {
0688       // If we are forced to close blocks and mark them bad
0689       if (iForce && iBad) {
0690         certifyLSBlock(theTriggerAlias, fLS, lLS, -1);
0691         m_certFirstLS[theTriggerAlias] = 0;
0692         m_certLastLS[theTriggerAlias] = 0;
0693       }
0694 
0695       // If we are not forced to mark bad, we check if we have enough statistics
0696       else {
0697         // Getting events with 0 bx difference between BPTX and Algo for current LS
0698         double CountSync = 0;
0699         double CountAll = 0;
0700 
0701         // Adding all entries for current LS block
0702         for (unsigned int ls = fLS; ls <= lLS; ls++) {
0703           CountSync += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls + 1, 3);
0704           for (int a = 1; a < 6; a++) {
0705             CountAll += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls + 1, a);
0706           }
0707         }
0708 
0709         if (m_verbose) {
0710           cout << "Alias = " << theTriggerAlias << " InitLS=" << fLS << " EndLS=" << lLS << " Events=" << CountAll;
0711         }
0712 
0713         if (iForce || CountAll >= m_parameters.getParameter<ParameterSet>("Categories")
0714                                       .getParameter<ParameterSet>(theCategory)
0715                                       .getParameter<int>("CertMinEvents")) {
0716           if (m_verbose) {
0717             cout << " <--------------- Enough Statistics: ";
0718           }
0719 
0720           // Calculating fraction of in time
0721           double fraction = 0;
0722           if (CountAll > 0) {
0723             fraction = CountSync / CountAll;
0724           }
0725 
0726           // This is to avoid having an entry equal to zero and thus
0727           // disregarded by the automatic tests
0728           if (fraction == 0) {
0729             fraction = 0.000001;
0730           }
0731 
0732           certifyLSBlock(theTriggerAlias, fLS, lLS, fraction);
0733           m_certFirstLS[theTriggerAlias] = 0;
0734           m_certLastLS[theTriggerAlias] = 0;
0735         }
0736 
0737         if (m_verbose) {
0738           cout << endl;
0739         }
0740       }
0741     }
0742 
0743     // A problem was found. We report it and set a not physical vale (-1) to the certification plot
0744     else {
0745       // If trigger alias is not valid report it to m_ErrorMonitor
0746       if (!triggerAlias_isValid) {
0747         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_TRIGGERALIAS_NOTVALID);
0748         eCount++;
0749         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_TRIGGERALIAS_NOTVALID, eCount);
0750         certifyLSBlock(theTriggerAlias, fLS, lLS, -1);
0751         m_certFirstLS[theTriggerAlias] = 0;
0752         m_certLastLS[theTriggerAlias] = 0;
0753       }
0754 
0755       // If LS Block is not valid report it to m_ErrorMonitor
0756       if (lsBlock_exists && !lsBlock_isValid) {
0757         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_LSBLOCK_NOTVALID);
0758         eCount++;
0759         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_LSBLOCK_NOTVALID, eCount);
0760         certifyLSBlock(theTriggerAlias, fLS, lLS, -1);
0761         m_certFirstLS[theTriggerAlias] = 0;
0762         m_certLastLS[theTriggerAlias] = 0;
0763       }
0764     }
0765   }
0766 }
0767 
0768 //_____________________________________________________________________
0769 // Method: certifyLSBlock
0770 // Description: Fill the trigger certification plot by blocks
0771 // Variable: iTrigger - Which trigger to certify
0772 // Variable: iInitLs  - Blocks initial LS
0773 // Variable: iEndLs   - Blocks end LS
0774 // Variable: iValue   - Value to be used to fill
0775 //_____________________________________________________________________
0776 void L1TSync::certifyLSBlock(string iTrigger, int iInitLs, int iEndLs, float iValue) {
0777   // Finding correct bins in the histogram for this block
0778   int binInit = m_algoCertification[iTrigger]->getTH1()->FindBin(iInitLs);
0779   int binEnd = m_algoCertification[iTrigger]->getTH1()->FindBin(iEndLs);
0780 
0781   for (int ls = binInit; ls <= binEnd; ls++) {
0782     m_algoCertification[iTrigger]->setBinContent(ls, iValue);
0783   }
0784 }