CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/DQM/L1TMonitor/src/L1TSync.cc

Go to the documentation of this file.
00001 /*
00002  * \file L1TSync.cc
00003  *
00004  * $Date: 2011/11/15 10:41:01 $
00005  * $Revision: 1.10 $
00006  * \author J. Pela, P. Musella
00007  *
00008  */
00009 
00010 // 
00011 #include "DQM/L1TMonitor/interface/L1TSync.h"
00012 
00013 #include "DQMServices/Core/interface/DQMStore.h"
00014 
00015 #include "DataFormats/Scalers/interface/LumiScalers.h"
00016 #include "DataFormats/Scalers/interface/Level1TriggerRates.h"
00017 #include "DataFormats/Scalers/interface/Level1TriggerScalers.h"
00018 
00019 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
00020 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerEvmReadoutRecord.h"
00021 
00022 #include "DataFormats/Common/interface/ConditionsInEdm.h" // Parameters associated to Run, LS and Event
00023 
00024 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenuFwd.h"
00025 #include "CondFormats/L1TObjects/interface/L1GtTriggerMenu.h"
00026 #include "CondFormats/L1TObjects/interface/L1GtPrescaleFactors.h"
00027 #include "CondFormats/DataRecord/interface/L1GtTriggerMenuRcd.h"
00028 #include "CondFormats/DataRecord/interface/L1GtPrescaleFactorsAlgoTrigRcd.h"
00029 #include "CondFormats/L1TObjects/interface/L1GtMuonTemplate.h"
00030 
00031 // Luminosity Information
00032 //#include "DataFormats/Luminosity/interface/LumiDetails.h"
00033 //#include "DataFormats/Luminosity/interface/LumiSummary.h"
00034 
00035 // L1TMonitor includes
00036 #include "DQM/L1TMonitor/interface/L1TMenuHelper.h"
00037 
00038 #include "TList.h"
00039 
00040 using namespace edm;
00041 using namespace std;
00042 
00043 //-------------------------------------------------------------------------------------
00044 //-------------------------------------------------------------------------------------
00045 L1TSync::L1TSync(const ParameterSet & pset){
00046 
00047   m_parameters = pset;
00048   
00049   // Mapping parameter input variables
00050   m_scalersSource       = pset.getParameter         <InputTag>("inputTagScalersResults");
00051   m_l1GtDataDaqInputTag = pset.getParameter         <InputTag>("inputTagL1GtDataDaq");
00052   m_l1GtEvmSource       = pset.getParameter         <InputTag>("inputTagtEvmSource");
00053   m_verbose             = pset.getUntrackedParameter<bool>    ("verbose",false);
00054   m_refPrescaleSet      = pset.getParameter         <int>     ("refPrescaleSet");  
00055 
00056   // Getting which categories to monitor
00057   ParameterSet Categories = pset.getParameter< ParameterSet >("Categories");
00058 
00059   bool forceGlobalParameters = Categories.getParameter<bool>("forceGlobalParameters");
00060   bool doGlobalAutoSelection = Categories.getParameter<bool>("doGlobalAutoSelection");
00061 
00062   ParameterSet CatBPTX    = Categories.getParameter<ParameterSet>("BPTX");
00063   ParameterSet CatMu      = Categories.getParameter<ParameterSet>("Mu");  
00064   ParameterSet CatEG      = Categories.getParameter<ParameterSet>("EG");  
00065   ParameterSet CatIsoEG   = Categories.getParameter<ParameterSet>("IsoEG");  
00066   ParameterSet CatJet     = Categories.getParameter<ParameterSet>("Jet");  
00067   ParameterSet CatCenJet  = Categories.getParameter<ParameterSet>("CenJet");  
00068   ParameterSet CatForJet  = Categories.getParameter<ParameterSet>("ForJet");  
00069   ParameterSet CatTauJet  = Categories.getParameter<ParameterSet>("TauJet");  
00070   ParameterSet CatETM     = Categories.getParameter<ParameterSet>("ETT");  
00071   ParameterSet CatETT     = Categories.getParameter<ParameterSet>("ETM");  
00072   ParameterSet CatHTT     = Categories.getParameter<ParameterSet>("HTT");  
00073   ParameterSet CatHTM     = Categories.getParameter<ParameterSet>("HTM");  
00074 
00075   // --> Setting parameters related to which algos to monitor
00076   // If global parameters are forced they take precedence over algo-by-algo parameters 
00077   if(forceGlobalParameters){
00078 
00079     // If global automatic selection if enable all categories set to be monitored will have
00080     // their algos auto selected (the lowest prescale algo will be selected) 
00081     if(doGlobalAutoSelection){
00082  
00083       if(CatMu    .getParameter<bool>("monitor")){m_algoAutoSelect["Mu"]     = true;}else{m_algoAutoSelect["Mu"]     = false;}
00084       if(CatEG    .getParameter<bool>("monitor")){m_algoAutoSelect["EG"]     = true;}else{m_algoAutoSelect["EG"]     = false;}
00085       if(CatIsoEG .getParameter<bool>("monitor")){m_algoAutoSelect["IsoEG"]  = true;}else{m_algoAutoSelect["IsoEG"]  = false;}
00086       if(CatJet   .getParameter<bool>("monitor")){m_algoAutoSelect["Jet"]    = true;}else{m_algoAutoSelect["Jet"]    = false;}
00087       if(CatCenJet.getParameter<bool>("monitor")){m_algoAutoSelect["CenJet"] = true;}else{m_algoAutoSelect["CenJet"] = false;}
00088       if(CatForJet.getParameter<bool>("monitor")){m_algoAutoSelect["ForJet"] = true;}else{m_algoAutoSelect["ForJet"] = false;}
00089       if(CatTauJet.getParameter<bool>("monitor")){m_algoAutoSelect["TauJet"] = true;}else{m_algoAutoSelect["TauJet"] = false;}
00090       if(CatETM   .getParameter<bool>("monitor")){m_algoAutoSelect["ETM"]    = true;}else{m_algoAutoSelect["ETM"]    = false;}
00091       if(CatETT   .getParameter<bool>("monitor")){m_algoAutoSelect["ETT"]    = true;}else{m_algoAutoSelect["ETT"]    = false;}
00092       if(CatHTM   .getParameter<bool>("monitor")){m_algoAutoSelect["HTM"]    = true;}else{m_algoAutoSelect["HTM"]    = false;}
00093       if(CatHTT   .getParameter<bool>("monitor")){m_algoAutoSelect["HTT"]    = true;}else{m_algoAutoSelect["HTT"]    = false;}
00094 
00095     // If global non-automatic selection is enable all categories set to be monitored will use
00096     // user defined algos
00097     }else{
00098 
00099       m_algoAutoSelect["Mu"]     = false;
00100       m_algoAutoSelect["EG"]     = false;
00101       m_algoAutoSelect["IsoEG"]  = false;
00102       m_algoAutoSelect["Jet"]    = false;
00103       m_algoAutoSelect["CenJet"] = false;
00104       m_algoAutoSelect["ForJet"] = false;
00105       m_algoAutoSelect["TauJet"] = false;
00106       m_algoAutoSelect["ETM"]    = false;
00107       m_algoAutoSelect["ETT"]    = false;
00108       m_algoAutoSelect["HTM"]    = false;
00109       m_algoAutoSelect["HTT"]    = false;   
00110 
00111       if(CatMu    .getParameter<bool>("monitor")){m_selectedTriggers["Mu"]        = CatMu    .getParameter<string>("algo");}
00112       if(CatEG    .getParameter<bool>("monitor")){m_selectedTriggers["EG"]        = CatEG    .getParameter<string>("algo");}
00113       if(CatIsoEG .getParameter<bool>("monitor")){m_selectedTriggers["IsoEG"]     = CatIsoEG .getParameter<string>("algo");}
00114       if(CatJet   .getParameter<bool>("monitor")){m_selectedTriggers["Jet"]       = CatJet   .getParameter<string>("algo");}
00115       if(CatCenJet.getParameter<bool>("monitor")){m_selectedTriggers["CenJet"]    = CatCenJet.getParameter<string>("algo");}
00116       if(CatForJet.getParameter<bool>("monitor")){m_selectedTriggers["CatForJet"] = CatForJet.getParameter<string>("algo");}
00117       if(CatTauJet.getParameter<bool>("monitor")){m_selectedTriggers["TauJet"]    = CatTauJet.getParameter<string>("algo");}
00118       if(CatETM   .getParameter<bool>("monitor")){m_selectedTriggers["ETM"]       = CatETM   .getParameter<string>("algo");}
00119       if(CatETT   .getParameter<bool>("monitor")){m_selectedTriggers["ETT"]       = CatETT   .getParameter<string>("algo");}
00120       if(CatHTM   .getParameter<bool>("monitor")){m_selectedTriggers["HTM"]       = CatHTM   .getParameter<string>("algo");}
00121       if(CatHTT   .getParameter<bool>("monitor")){m_selectedTriggers["HTT"]       = CatHTT   .getParameter<string>("algo");}
00122 
00123     }
00124 
00125   // If we are using algo-by-algo parametes we get them and set what is needed
00126   }else{
00127 
00128     if(CatBPTX.getParameter<bool>("monitor")){
00129       m_selectedTriggers["BPTX"] = CatBPTX.getParameter<string>("algo");
00130     }
00131 
00132     if(CatMu.getParameter<bool>("monitor")){
00133       m_algoAutoSelect["Mu"] = CatMu.getParameter<bool>("doAutoSelection");
00134       if(!m_algoAutoSelect["Mu"]){m_selectedTriggers["Mu"] = CatMu.getParameter<string>("algo");}
00135     }else{m_algoAutoSelect["Mu"] = false;}
00136 
00137     if(CatEG.getParameter<bool>("monitor")){
00138       m_algoAutoSelect["EG"] = CatEG.getParameter<bool>("doAutoSelection");
00139       if(!m_algoAutoSelect["EG"]){m_selectedTriggers["EG"] = CatEG.getParameter<string>("algo");}
00140     }else{m_algoAutoSelect["EG"] = false;}
00141 
00142     if(CatIsoEG.getParameter<bool>("monitor")){
00143       m_algoAutoSelect["IsoEG"] = CatIsoEG.getParameter<bool>("doAutoSelection");
00144       if(!m_algoAutoSelect["IsoEG"]){m_selectedTriggers["IsoEG"] = CatIsoEG.getParameter<string>("algo");}
00145     }else{m_algoAutoSelect["IsoEG"] = false;}
00146 
00147     if(CatJet.getParameter<bool>("monitor")){
00148       m_algoAutoSelect["Jet"] = CatJet.getParameter<bool>("doAutoSelection");
00149       if(!m_algoAutoSelect["Jet"]){m_selectedTriggers["Jet"] = CatJet.getParameter<string>("algo");}
00150     }else{m_algoAutoSelect["Jet"] = false;}
00151 
00152     if(CatCenJet.getParameter<bool>("monitor")){
00153       m_algoAutoSelect["CenJet"] = CatCenJet.getParameter<bool>("doAutoSelection");
00154       if(!m_algoAutoSelect["CenJet"]){m_selectedTriggers["CenJet"] = CatCenJet.getParameter<string>("algo");}
00155     }else{m_algoAutoSelect["CenJet"] = false;}
00156 
00157     if(CatForJet.getParameter<bool>("monitor")){
00158       m_algoAutoSelect["CatForJet"] = CatForJet.getParameter<bool>("doAutoSelection");
00159       if(!m_algoAutoSelect["CatForJet"]){m_selectedTriggers["CatForJet"] = CatForJet.getParameter<string>("algo");}
00160     }else{m_algoAutoSelect["CatForJet"] = false;}
00161 
00162     if(CatTauJet.getParameter<bool>("monitor")){
00163       m_algoAutoSelect["TauJet"] = CatTauJet.getParameter<bool>("doAutoSelection");
00164       if(!m_algoAutoSelect["TauJet"]){m_selectedTriggers["TauJet"] = CatTauJet.getParameter<string>("algo");}
00165     }else{m_algoAutoSelect["TauJet"] = false;}
00166 
00167     if(CatETM.getParameter<bool>("monitor")){
00168       m_algoAutoSelect["ETM"] = CatETM.getParameter<bool>("doAutoSelection");
00169       if(!m_algoAutoSelect["ETM"]){m_selectedTriggers["ETM"] = CatETM.getParameter<string>("algo");}
00170     }else{m_algoAutoSelect["ETM"] = false;}
00171 
00172     if(CatETT.getParameter<bool>("monitor")){
00173       m_algoAutoSelect["ETT"] = CatETT.getParameter<bool>("doAutoSelection");
00174       if(!m_algoAutoSelect["ETT"]){m_selectedTriggers["ETT"] = CatETT.getParameter<string>("algo");}
00175     }else{m_algoAutoSelect["ETT"] = false;}
00176 
00177     if(CatHTM.getParameter<bool>("monitor")){
00178       m_algoAutoSelect["HTM"] = CatHTM.getParameter<bool>("doAutoSelection");
00179       if(!m_algoAutoSelect["HTM"]){m_selectedTriggers["HTM"] = CatHTM.getParameter<string>("algo");}
00180     }else{m_algoAutoSelect["HTM"] = false;}
00181 
00182     if(CatHTT.getParameter<bool>("monitor")){
00183       m_algoAutoSelect["HTT"] = CatHTT.getParameter<bool>("doAutoSelection");
00184       if(!m_algoAutoSelect["HTT"]){m_selectedTriggers["HTT"] = CatHTT.getParameter<string>("algo");}
00185     }else{m_algoAutoSelect["HTT"] = false;}
00186 
00187   }
00188 
00189 
00190   if (pset.getUntrackedParameter < bool > ("dqmStore", false)) {
00191     dbe = Service < DQMStore > ().operator->();
00192     dbe->setVerbose(0);
00193   }
00194 
00195   m_outputFile = pset.getUntrackedParameter < std::string > ("outputFile","");
00196 
00197   if (m_outputFile.size() != 0) {
00198     std::cout << "L1T Monitoring histograms will be saved to " <<       m_outputFile.c_str() << std::endl;
00199   }
00200 
00201   bool disable = pset.getUntrackedParameter < bool > ("disableROOToutput", false);
00202   if (disable) {m_outputFile = "";}
00203 
00204   if (dbe != NULL) {dbe->setCurrentFolder("L1T/L1TSync");}
00205 
00206 }
00207 
00208 //-------------------------------------------------------------------------------------
00209 //-------------------------------------------------------------------------------------
00210 L1TSync::~L1TSync(){}
00211 
00212 //-------------------------------------------------------------------------------------
00213 //-------------------------------------------------------------------------------------
00214 void L1TSync::beginJob(void){
00215 
00216   if (m_verbose){cout << "[L1TSync] Called beginJob." << endl;}
00217 
00218   // get hold of back-end interface
00219   DQMStore *dbe = 0;
00220   dbe = Service < DQMStore > ().operator->();
00221 
00222   if (dbe) {
00223     dbe->setCurrentFolder("L1T/L1TSync");
00224     dbe->rmdir("L1T/L1TSync");
00225   }
00226  
00227 }
00228 
00229 //-------------------------------------------------------------------------------------
00230 //-------------------------------------------------------------------------------------
00231 void L1TSync::endJob(void){
00232 
00233   if (m_verbose){cout << "[L1TSync] Called endJob." << endl;}
00234 
00235   if (m_outputFile.size() != 0 && dbe)
00236     dbe->save(m_outputFile);
00237 
00238   return;
00239 
00240 }
00241 
00242 //-------------------------------------------------------------------------------------
00244 //-------------------------------------------------------------------------------------
00245 void L1TSync::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup){
00246 
00247   if (m_verbose){cout << "[L1TSync] Called beginRun." << endl;}
00248 
00249   // Initializing variables
00250   int maxNbins = 2501;
00251 
00252   // Reseting run dependent variables
00253   m_lhcFill     = 0; 
00254   m_currentLS   = 0;
00255   m_certFirstLS.clear();
00256   m_certLastLS .clear();
00257   
00258   // Getting Trigger menu from GT
00259   ESHandle<L1GtTriggerMenu> menuRcd;
00260   iSetup.get<L1GtTriggerMenuRcd>().get(menuRcd);
00261   const L1GtTriggerMenu* menu = menuRcd.product();
00262 
00263   // Filling Alias-Bit Map
00264   for (CItAlgo algo = menu->gtAlgorithmAliasMap().begin(); algo!=menu->gtAlgorithmAliasMap().end(); ++algo){
00265     m_algoBit[(algo->second).algoAlias()] = (algo->second).algoBitNumber();
00266   }
00267 
00268   // Getting fill number for this run
00269   //Handle<ConditionsInRunBlock> runConditions;
00270   //iRun.getByType(runConditions);
00271   //int lhcFillNumber = runConditions->lhcFillNumber;
00272   //
00273   //ESHandle<L1GtPrescaleFactors> l1GtPfAlgo;
00274   //iSetup.get<L1GtPrescaleFactorsAlgoTrigRcd>().get(l1GtPfAlgo);
00275   //const L1GtPrescaleFactors* m_l1GtPfAlgo = l1GtPfAlgo.product();
00276 
00277   L1TMenuHelper myMenuHelper = L1TMenuHelper(iSetup);  
00278          
00279   m_selectedTriggers = myMenuHelper.testAlgos(m_selectedTriggers);
00280 
00281   map<string,string> tAutoSelTrig = myMenuHelper.getLUSOTrigger(m_algoAutoSelect,m_refPrescaleSet);
00282   m_selectedTriggers.insert(tAutoSelTrig.begin(),tAutoSelTrig.end());
00283 
00284   // Initializing DQM Monitor Elements
00285   dbe->setCurrentFolder("L1T/L1TSync");
00286   m_ErrorMonitor = dbe->book1D("ErrorMonitor","ErrorMonitor",7,0,7);
00287   m_ErrorMonitor->setBinLabel(UNKNOWN                      ,"UNKNOWN");
00288   m_ErrorMonitor->setBinLabel(WARNING_DB_CONN_FAILED       ,"WARNING_DB_CONN_FAILED");        // Errors from L1TOMDSHelper
00289   m_ErrorMonitor->setBinLabel(WARNING_DB_QUERY_FAILED      ,"WARNING_DB_QUERY_FAILED");       // Errors from L1TOMDSHelper
00290   m_ErrorMonitor->setBinLabel(WARNING_DB_INCORRECT_NBUNCHES,"WARNING_DB_INCORRECT_NBUNCHES"); // Errors from L1TOMDSHelper
00291   m_ErrorMonitor->setBinLabel(ERROR_UNABLE_RETRIVE_PRODUCT ,"ERROR_UNABLE_RETRIVE_PRODUCT"); 
00292   m_ErrorMonitor->setBinLabel(ERROR_TRIGGERALIAS_NOTVALID  ,"ERROR_TRIGGERALIAS_NOTVALID"); 
00293   m_ErrorMonitor->setBinLabel(ERROR_LSBLOCK_NOTVALID       ,"ERROR_LSBLOCK_NOTVALID"); 
00294 
00295   // Looping over selected triggers
00296   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00297 
00298     string tCategory = (*i).first;
00299     string tTrigger  = (*i).second;
00300 
00301     // Initializing LS blocks for certification
00302     m_certFirstLS[(*i).second] = 0;
00303     m_certLastLS [(*i).second] = 0;
00304 
00305     // Initializing DQM Monitors 
00306     dbe->setCurrentFolder("L1T/L1TSync/AlgoVsBunchStructure/");
00307     m_algoVsBunchStructure[tTrigger] = dbe->book2D(tCategory,"min #Delta("+tTrigger+",Bunch)",maxNbins,-0.5,double(maxNbins)-0.5,5,-2.5,2.5);
00308     m_algoVsBunchStructure[tTrigger] ->setAxisTitle("Lumi Section" ,1);
00309     
00310     dbe->setCurrentFolder("L1T/L1TSync/Certification/");
00311     m_algoCertification[tTrigger] = dbe->book1D(tCategory, "fraction of in sync: "+tTrigger,maxNbins,-0.5,double(maxNbins)-0.5);
00312     m_algoCertification[tTrigger] ->setAxisTitle("Lumi Section" ,1);
00313 
00314  }   
00315 
00316 }
00317 
00318 //_____________________________________________________________________
00319 // Function: beginLuminosityBlock
00320 //_____________________________________________________________________
00321 void L1TSync::beginLuminosityBlock(LuminosityBlock const& lumiBlock, EventSetup const& c) {
00322 
00323   if (m_verbose){cout << "[L1TSync] Called beginLuminosityBlock." << endl;}
00324 
00325   // If this LS is not the next to last one force closing of current LS blocks
00326   // for certification
00327   if(m_currentLS !=0 && m_currentLS+1 != lumiBlock.id().luminosityBlock()){
00328 
00329     if (m_verbose){
00330       cout << "[L1TSync] None consecutive: doFractionInSync() - LAST=" 
00331            << m_currentLS << " CURRENT=" << lumiBlock.id().luminosityBlock() << endl;
00332     }
00333 
00334     doFractionInSync(true,false);    
00335 
00336   }
00337     
00338   // Updating current LS number
00339   m_currentLS = lumiBlock.id().luminosityBlock();
00340 
00341   // If this is the fist valid LS update first LS for certification 
00342   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00343 
00344     string theTriggerAlias = (*i).second;
00345     if(m_certFirstLS[theTriggerAlias]==0){m_certFirstLS[theTriggerAlias] = m_currentLS;}
00346 
00347   }
00348 
00349   // A LS will be valid if:
00350   //   * BeamMode == STABLE for all events monitored
00351   m_currentLSValid = true; 
00352 
00353 }
00354 
00355 //_____________________________________________________________________
00356 // Function: endLuminosityBlock
00357 // * Fills LS by LS ration of trigger out of sync
00358 //_____________________________________________________________________
00359 void L1TSync::endLuminosityBlock(LuminosityBlock const& lumiBlock, EventSetup const& c) {
00360 
00361   if(m_verbose){cout << "[L1TSync] Called endLuminosityBlock." << endl;}
00362 
00363   if(m_verbose){
00364     cout << "[L1TSync] m_currentLSValid      : " << m_currentLSValid       << endl;
00365     cout << "[L1TSync] m_beamConfig.isValid(): " << m_beamConfig.isValid() << endl;
00366   }  
00367 
00368   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00369 
00370     // Update last LS for certification      
00371     string theTriggerAlias = (*i).second;
00372     m_certLastLS[theTriggerAlias] = m_currentLS;
00373 
00374   }
00375 
00376   // If this LS is valid (i.e. all events recorded with stable beams)
00377   if(m_currentLSValid && m_beamConfig.isValid()){
00378 
00379     if(m_verbose){cout << "[L1TSync] Regular call: doFractionInSync()" << endl;}
00380     doFractionInSync(false,false);
00381     
00382   }
00383   // If this LS is not valid it can be in the following context:
00384   //  * We still hadn't stable beam (no need to certify nothing
00385   //  * Beam just got unstable or dumped (we may have a complete block of data do certify)  
00386   else{
00387     
00388     //-> First we close all blocks from certFirstLS[] to m_currentLS-1
00389     for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00390 
00391       string theTriggerAlias = (*i).second;
00392       
00393       int fLs = m_certFirstLS[theTriggerAlias];
00394       int lLS = m_certLastLS [theTriggerAlias];
00395 
00396       // If this is a single LS block we do nothing (in this step)
00397       if(fLs == lLS){
00398         m_certFirstLS[theTriggerAlias] = 0;
00399         m_certLastLS [theTriggerAlias] = 0;
00400       }
00401       // If block is multi LS then we remove the current LS 
00402       else{
00403         m_certLastLS [theTriggerAlias] = m_currentLS-1;
00404       }
00405 
00406     }
00407     doFractionInSync(true,false);
00408 
00409     //-> Second we mark this single LS bad for all triggers
00410     for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00411       string theTriggerAlias = (*i).second;
00412       m_certFirstLS[theTriggerAlias] = m_currentLS;
00413       m_certLastLS [theTriggerAlias] = m_currentLS;
00414     }
00415     doFractionInSync(true,true);
00416 
00417     if(m_verbose){cout << "[L1TSync] Error call: doFractionInSync()" << endl;}  
00418 
00419   }
00420 
00421 }
00422 
00423 //_____________________________________________________________________
00424 void L1TSync::endRun(const edm::Run& run, const edm::EventSetup& iSetup){
00425   
00426   if(m_verbose){cout << "[L1TSync] Called endRun." << endl;}
00427   
00428   // When the run end for closing of the LS certification blocks and evaluation
00429   // of synchronization for that block
00430   doFractionInSync(true,false);    
00431 
00432 }
00433 
00434 //_____________________________________________________________________
00435 void L1TSync::analyze(const Event & iEvent, const EventSetup & eventSetup){
00436  
00437   if(m_verbose){cout << "[L1TSync] Called analyze." << endl;}
00438 
00439   // We only start analyzing if current LS is still valid
00440   if(m_currentLSValid){
00441   
00442     if(m_verbose){cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;}
00443     
00444     // Retriving information from GT
00445     edm::Handle<L1GlobalTriggerEvmReadoutRecord> gtEvmReadoutRecord;
00446     iEvent.getByLabel(m_l1GtEvmSource, gtEvmReadoutRecord);
00447 
00448     // Determining beam mode and fill number
00449     if(gtEvmReadoutRecord.isValid()){
00450 
00451       const L1GtfeExtWord& gtfeEvmWord = gtEvmReadoutRecord->gtfeWord();
00452       unsigned int lhcBeamMode         = gtfeEvmWord.beamMode();          // Updating beam mode
00453 
00454       if(m_lhcFill == 0){
00455         m_lhcFill = gtfeEvmWord.lhcFillNumber(); // Getting LHC Fill Number from GT
00456         getBeamConfOMDS();                       // Getting Beam Configuration from OMDS
00457       }
00458 
00459       if(lhcBeamMode != STABLE){m_currentLSValid = false;} // If Beams are not stable we invalidate this LS
00460 
00461     }else{
00462       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
00463       eCount++;
00464       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT,eCount);
00465     }
00466   }else{
00467     if(m_verbose){cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;}
00468   }
00469 
00470   //------------------------------------------------------------------------------
00471   // If current LS is valid and Beam Configuration is Valid we analyse this event
00472   //------------------------------------------------------------------------------
00473   if(m_currentLSValid && m_beamConfig.isValid()){
00474 
00475     // Getting Final Decision Logic (FDL) Data from GT
00476     edm::Handle<L1GlobalTriggerReadoutRecord> gtReadoutRecordData;
00477     iEvent.getByLabel(m_l1GtDataDaqInputTag, gtReadoutRecordData);
00478 
00479     if(gtReadoutRecordData.isValid()){
00480 
00481       const vector<L1GtFdlWord>& gtFdlVectorData = gtReadoutRecordData->gtFdlVector();
00482 
00483       // Running over selected triggers
00484       for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00485 
00486         string tTrigger = (*i).second;
00487 
00488         // Analyse only defined triggers
00489         if(tTrigger != "Undefined" && tTrigger != "Undefined (Wrong Name)"){
00490 
00491           bool beamSingleConfig = false; // Single beam configured for this event
00492           bool firedAlgo        = false; // Algo fired in this event
00493           int  eventBx          = -1;      
00494 
00495           // Running over FDL results to get which bits fired
00496           for(unsigned int a=0 ; a<gtFdlVectorData.size() ; a++){
00497    
00498             // Selecting the FDL that triggered
00499             if(gtFdlVectorData[a].bxInEvent() == 0){
00500               eventBx = gtFdlVectorData[a].localBxNr();
00501               if(gtFdlVectorData[a].gtDecisionWord()[ m_algoBit[tTrigger] ]){firedAlgo = true;}
00502             }
00503           }
00504 
00505           // Checking beam configuration
00506           if( m_beamConfig.beam1[eventBx] && !m_beamConfig.beam2[eventBx]){beamSingleConfig = true;}
00507           if(!m_beamConfig.beam1[eventBx] &&  m_beamConfig.beam2[eventBx]){beamSingleConfig = true;}
00508 
00509           // Analyse only if this trigger fired in this event
00510           // NOTE: Veto cases where a single beam is configured since
00511           //       for this cases this could be a real-satelite bunch collision
00512           // -> Calculate the minimum bx diference between this event and a configured bx
00513           if(firedAlgo && !beamSingleConfig){
00514 
00515             int DifAlgoVsBunchStructure = 9999; // Majorated
00516 
00517             for(unsigned int a=0 ; a<gtFdlVectorData.size() ; a++){
00518 
00519               int bxFDL     = gtFdlVectorData[a].localBxNr();
00520               int bxInEvent = gtFdlVectorData[a].bxInEvent();
00521 
00522               if(m_beamConfig.bxConfig(bxFDL) && abs(bxInEvent)<abs(DifAlgoVsBunchStructure)){
00523                 DifAlgoVsBunchStructure = -1*bxInEvent;
00524               }
00525             }
00526 
00527             m_algoVsBunchStructure[tTrigger]->Fill(m_currentLS,DifAlgoVsBunchStructure);
00528 
00529           }
00530         }
00531       }
00532     }
00533     else{
00534       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
00535       eCount++;
00536       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT,eCount);
00537     }
00538 
00539   }
00540 }
00541 
00542 //_____________________________________________________________________
00543 // Method: getBunchStructureOMDS
00544 // Description: Attempt to retrive Beam Configuration from OMDS and if
00545 //              we find error handle it
00546 //_____________________________________________________________________
00547 void L1TSync::getBeamConfOMDS(){
00548 
00549   //Getting connection paremeters
00550   string oracleDB   = m_parameters.getParameter<string>("oracleDB");
00551   string pathCondDB = m_parameters.getParameter<string>("pathCondDB");
00552 
00553   // Connecting to OMDS 
00554   L1TOMDSHelper myOMDSHelper = L1TOMDSHelper();
00555   int conError;
00556   myOMDSHelper.connect(oracleDB,pathCondDB,conError);
00557 
00558   if(conError == L1TOMDSHelper::NO_ERROR){
00559 
00560     int errorRetrive;
00561     m_beamConfig = myOMDSHelper.getBeamConfiguration(m_lhcFill,errorRetrive);
00562 
00563     if(conError == L1TOMDSHelper::WARNING_DB_CONN_FAILED){
00564       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_INCORRECT_NBUNCHES);
00565       eCount++;
00566       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_INCORRECT_NBUNCHES,eCount);
00567     }
00568     else if(conError == L1TOMDSHelper::WARNING_DB_QUERY_FAILED){
00569       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_QUERY_FAILED);
00570       eCount++;
00571       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_QUERY_FAILED,eCount);
00572     }
00573     else{
00574       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
00575       eCount++;
00576       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN,eCount);
00577     }
00578 
00579   }else{
00580 
00581     if(conError == L1TOMDSHelper::WARNING_DB_CONN_FAILED){
00582       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_CONN_FAILED);
00583       eCount++;
00584       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_CONN_FAILED,eCount);
00585     }else{
00586       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
00587       eCount++;
00588       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN,eCount);
00589     }
00590 
00591   }
00592 
00593 }
00594 
00595 //_____________________________________________________________________
00596 // Method: doFractionInSync
00597 // Description: Produce plot with the fraction of in sync trigger for
00598 //              LS blocks with enough statistics.
00599 // Variable: iForce - Forces closing of all blocks and calculation of 
00600 //                    the respective fractions
00601 // Variable: iBad   - (Only works with iForce=true) Forces the current 
00602 //                    all current blocks to be marked as bad 
00603 //_____________________________________________________________________
00604 void L1TSync::doFractionInSync(bool iForce,bool iBad){
00605   
00606   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00607 
00608     string theCategory     = (*i).first;
00609     string theTriggerAlias = (*i).second;
00610 
00611     // Caching frequently used values from maps
00612     unsigned int fLS = m_certFirstLS[theTriggerAlias];
00613     unsigned int lLS = m_certLastLS [theTriggerAlias];
00614     
00615     // Checking validity of the trigger alias and of the LS block
00616     bool triggerAlias_isValid = theTriggerAlias != "Undefined" && theTriggerAlias != "Undefined (Wrong Name)";
00617     bool lsBlock_exists       = !(fLS == 0 && lLS == 0);
00618     bool lsBlock_isValid      = fLS <= lLS && fLS > 0 && lLS > 0; 
00619 
00620     if(triggerAlias_isValid && lsBlock_exists && lsBlock_isValid){
00621 
00622       // If we are forced to close blocks and mark them bad
00623       if(iForce && iBad){
00624         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00625         m_certFirstLS[theTriggerAlias] = 0;
00626         m_certLastLS [theTriggerAlias] = 0;
00627       }
00628 
00629       // If we are not forced to mark bad, we check if we have enough statistics
00630       else{
00631 
00632         // Getting events with 0 bx difference between BPTX and Algo for current LS
00633         double CountSync = 0;
00634         double CountAll  = 0;
00635   
00636         // Adding all entries for current LS block
00637         for(unsigned int ls=fLS ; ls<=lLS ; ls++){
00638 
00639           CountSync += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls+1,3);
00640           for(int a=1 ; a<6 ; a++){
00641             CountAll  += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls+1,a);
00642           }
00643         }
00644 
00645         if(m_verbose){
00646           cout << "Alias = " << theTriggerAlias 
00647                << " InitLS=" << fLS 
00648                << " EndLS=" <<  lLS 
00649                << " Events=" << CountAll ;
00650         } 
00651 
00652         if(iForce ||
00653            CountAll >= m_parameters.getParameter<ParameterSet>("Categories")
00654                                    .getParameter<ParameterSet>(theCategory)
00655                                    .getParameter<int>("CertMinEvents")){
00656 
00657           if(m_verbose){cout << " <--------------- Enough Statistics: ";}
00658 
00659         
00660           // Calculating fraction of in time 
00661           double fraction = 0;
00662           if(CountAll >0){fraction = CountSync/CountAll;}
00663 
00664           // This is to avoid having an entry equal to zero and thus
00665           // disregarded by the automatic tests
00666           if(fraction==0){fraction=0.000001;}
00667         
00668           certifyLSBlock(theTriggerAlias,fLS,lLS,fraction);
00669           m_certFirstLS[theTriggerAlias] = 0;
00670           m_certLastLS [theTriggerAlias] = 0;
00671         }
00672 
00673         if(m_verbose){cout << endl;}
00674 
00675       }
00676     }
00677 
00678     // A problem was found. We report it and set a not physical vale (-1) to the certification plot
00679     else{
00680 
00681       // If trigger alias is not valid report it to m_ErrorMonitor
00682       if(!triggerAlias_isValid){
00683         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_TRIGGERALIAS_NOTVALID);
00684         eCount++;
00685         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_TRIGGERALIAS_NOTVALID,eCount);
00686         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00687         m_certFirstLS[theTriggerAlias] = 0;
00688         m_certLastLS [theTriggerAlias] = 0;
00689       }
00690 
00691       // If LS Block is not valid report it to m_ErrorMonitor
00692       if(lsBlock_exists && !lsBlock_isValid){
00693         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_LSBLOCK_NOTVALID);
00694         eCount++;
00695         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_LSBLOCK_NOTVALID,eCount);
00696         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00697         m_certFirstLS[theTriggerAlias] = 0;
00698         m_certLastLS [theTriggerAlias] = 0;
00699       }
00700 
00701     }
00702   }
00703 
00704 }
00705 
00706 
00707 //_____________________________________________________________________
00708 // Method: certifyLSBlock
00709 // Description: Fill the trigger certification plot by blocks
00710 // Variable: iTrigger - Which trigger to certify
00711 // Variable: iInitLs  - Blocks initial LS
00712 // Variable: iEndLs   - Blocks end LS
00713 // Variable: iValue   - Value to be used to fill
00714 //_____________________________________________________________________
00715 void L1TSync::certifyLSBlock(string iTrigger, int iInitLs, int iEndLs ,float iValue){
00716   
00717   // Finding correct bins in the histogram for this block
00718   int binInit = m_algoCertification[iTrigger]->getTH1()->FindBin(iInitLs);
00719   int binEnd  = m_algoCertification[iTrigger]->getTH1()->FindBin(iEndLs);
00720 
00721   for(int ls=binInit ; ls<=binEnd ; ls++){
00722     m_algoCertification[iTrigger]->setBinContent(ls,iValue);
00723   }
00724 
00725 }
00726 
00727 //define this as a plug-in
00728 DEFINE_FWK_MODULE(L1TSync);