CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/DQM/L1TMonitor/src/L1TSync.cc

Go to the documentation of this file.
00001 /*
00002  * \file L1TSync.cc
00003  *
00004  * $Date: 2012/08/28 10:35:38 $
00005  * $Revision: 1.11 $
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     doFractionInSync(true,false);    
00334   }
00335     
00336   // Updating current LS number
00337   m_currentLS = lumiBlock.id().luminosityBlock();
00338 
00339   // If this is the fist valid LS update first LS for certification 
00340   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00341 
00342     string theTriggerAlias = (*i).second;
00343     if(m_certFirstLS[theTriggerAlias]==0){m_certFirstLS[theTriggerAlias] = m_currentLS;}
00344 
00345   }
00346 
00347   // A LS will be valid if:
00348   //   * BeamMode == STABLE for all events monitored
00349   m_currentLSValid = true; 
00350 
00351 }
00352 
00353 //_____________________________________________________________________
00354 // Function: endLuminosityBlock
00355 // * Fills LS by LS ration of trigger out of sync
00356 //_____________________________________________________________________
00357 void L1TSync::endLuminosityBlock(LuminosityBlock const& lumiBlock, EventSetup const& c) {
00358 
00359   if(m_verbose){cout << "[L1TSync] Called endLuminosityBlock." << endl;}
00360 
00361   if(m_verbose){
00362     cout << "[L1TSync] m_currentLSValid      : " << m_currentLSValid       << endl;
00363     cout << "[L1TSync] m_beamConfig.isValid(): " << m_beamConfig.isValid() << endl;
00364   }  
00365 
00366   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00367 
00368     // Update last LS for certification      
00369     string theTriggerAlias = (*i).second;
00370     m_certLastLS[theTriggerAlias] = m_currentLS;
00371 
00372   }
00373 
00374   // If this LS is valid (i.e. all events recorded with stable beams)
00375   if(m_currentLSValid && m_beamConfig.isValid()){
00376 
00377     if(m_verbose){cout << "[L1TSync] Regular call: doFractionInSync()" << endl;}
00378     doFractionInSync(false,false);
00379     
00380   }
00381   // If this LS is not valid it can be in the following context:
00382   //  * We still hadn't stable beam (no need to certify nothing
00383   //  * Beam just got unstable or dumped (we may have a complete block of data do certify)  
00384   else{
00385     
00386     //-> First we close all blocks from certFirstLS[] to m_currentLS-1
00387     for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00388 
00389       string theTriggerAlias = (*i).second;
00390       
00391       int fLs = m_certFirstLS[theTriggerAlias];
00392       int lLS = m_certLastLS [theTriggerAlias];
00393 
00394       // If this is a single LS block we do nothing (in this step)
00395       if(fLs == lLS){
00396         m_certFirstLS[theTriggerAlias] = 0;
00397         m_certLastLS [theTriggerAlias] = 0;
00398       }
00399       // If block is multi LS then we remove the current LS 
00400       else{
00401         m_certLastLS [theTriggerAlias] = m_currentLS-1;
00402       }
00403 
00404     }
00405     doFractionInSync(true,false);
00406 
00407     //-> Second we mark this single LS bad for all triggers
00408     for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00409       string theTriggerAlias = (*i).second;
00410       m_certFirstLS[theTriggerAlias] = m_currentLS;
00411       m_certLastLS [theTriggerAlias] = m_currentLS;
00412     }
00413     doFractionInSync(true,true);
00414 
00415     if(m_verbose){cout << "[L1TSync] Error call: doFractionInSync()" << endl;}  
00416 
00417   }
00418 
00419 }
00420 
00421 //_____________________________________________________________________
00422 void L1TSync::endRun(const edm::Run& run, const edm::EventSetup& iSetup){
00423   
00424   if(m_verbose){cout << "[L1TSync] Called endRun." << endl;}
00425   
00426   // When the run end for closing of the LS certification blocks and evaluation
00427   // of synchronization for that block
00428   doFractionInSync(true,false);    
00429 
00430 }
00431 
00432 //_____________________________________________________________________
00433 void L1TSync::analyze(const Event & iEvent, const EventSetup & eventSetup){
00434  
00435   if(m_verbose){cout << "[L1TSync] Called analyze." << endl;}
00436 
00437   // We only start analyzing if current LS is still valid
00438   if(m_currentLSValid){
00439   
00440     if(m_verbose){cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;}
00441     
00442     // Retriving information from GT
00443     edm::Handle<L1GlobalTriggerEvmReadoutRecord> gtEvmReadoutRecord;
00444     iEvent.getByLabel(m_l1GtEvmSource, gtEvmReadoutRecord);
00445 
00446     // Determining beam mode and fill number
00447     if(gtEvmReadoutRecord.isValid()){
00448 
00449       const L1GtfeExtWord& gtfeEvmWord = gtEvmReadoutRecord->gtfeWord();
00450       unsigned int lhcBeamMode         = gtfeEvmWord.beamMode();          // Updating beam mode
00451 
00452       if(m_lhcFill == 0){
00453         m_lhcFill = gtfeEvmWord.lhcFillNumber(); // Getting LHC Fill Number from GT
00454         getBeamConfOMDS();                       // Getting Beam Configuration from OMDS
00455       }
00456 
00457       if(lhcBeamMode != STABLE){m_currentLSValid = false;} // If Beams are not stable we invalidate this LS
00458 
00459     }else{
00460       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
00461       eCount++;
00462       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT,eCount);
00463     }
00464   }else{
00465     if(m_verbose){cout << "[L1TSync] -> m_currentLSValid=" << m_currentLSValid << endl;}
00466   }
00467 
00468   //------------------------------------------------------------------------------
00469   // If current LS is valid and Beam Configuration is Valid we analyse this event
00470   //------------------------------------------------------------------------------
00471   if(m_currentLSValid && m_beamConfig.isValid()){
00472 
00473     // Getting Final Decision Logic (FDL) Data from GT
00474     edm::Handle<L1GlobalTriggerReadoutRecord> gtReadoutRecordData;
00475     iEvent.getByLabel(m_l1GtDataDaqInputTag, gtReadoutRecordData);
00476 
00477     if(gtReadoutRecordData.isValid()){
00478 
00479       const vector<L1GtFdlWord>& gtFdlVectorData = gtReadoutRecordData->gtFdlVector();
00480 
00481       // Running over selected triggers
00482       for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00483 
00484         string tTrigger = (*i).second;
00485 
00486         // Analyse only defined triggers
00487         if(tTrigger != "Undefined" && tTrigger != "Undefined (Wrong Name)"){
00488 
00489           bool beamSingleConfig = false; // Single beam configured for this event
00490           bool firedAlgo        = false; // Algo fired in this event
00491           int  eventBx          = -1;      
00492 
00493           // Running over FDL results to get which bits fired
00494           for(unsigned int a=0 ; a<gtFdlVectorData.size() ; a++){
00495    
00496             // Selecting the FDL that triggered
00497             if(gtFdlVectorData[a].bxInEvent() == 0){
00498               eventBx = gtFdlVectorData[a].localBxNr();
00499               if(gtFdlVectorData[a].gtDecisionWord()[ m_algoBit[tTrigger] ]){firedAlgo = true;}
00500             }
00501           }
00502 
00503           // Checking beam configuration
00504           if( m_beamConfig.beam1[eventBx] && !m_beamConfig.beam2[eventBx]){beamSingleConfig = true;}
00505           if(!m_beamConfig.beam1[eventBx] &&  m_beamConfig.beam2[eventBx]){beamSingleConfig = true;}
00506 
00507           // Analyse only if this trigger fired in this event
00508           // NOTE: Veto cases where a single beam is configured since
00509           //       for this cases this could be a real-satelite bunch collision
00510           // -> Calculate the minimum bx diference between this event and a configured bx
00511           if(firedAlgo && !beamSingleConfig){
00512 
00513             int DifAlgoVsBunchStructure = 9999; // Majorated
00514 
00515             for(unsigned int a=0 ; a<gtFdlVectorData.size() ; a++){
00516 
00517               int bxFDL     = gtFdlVectorData[a].localBxNr();
00518               int bxInEvent = gtFdlVectorData[a].bxInEvent();
00519 
00520               if(m_beamConfig.bxConfig(bxFDL) && abs(bxInEvent)<abs(DifAlgoVsBunchStructure)){
00521                 DifAlgoVsBunchStructure = -1*bxInEvent;
00522               }
00523             }
00524 
00525             m_algoVsBunchStructure[tTrigger]->Fill(m_currentLS,DifAlgoVsBunchStructure);
00526 
00527           }
00528         }
00529       }
00530     }
00531     else{
00532       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT);
00533       eCount++;
00534       m_ErrorMonitor->getTH1()->SetBinContent(ERROR_UNABLE_RETRIVE_PRODUCT,eCount);
00535     }
00536 
00537   }
00538 }
00539 
00540 //_____________________________________________________________________
00541 // Method: getBunchStructureOMDS
00542 // Description: Attempt to retrive Beam Configuration from OMDS and if
00543 //              we find error handle it
00544 //_____________________________________________________________________
00545 void L1TSync::getBeamConfOMDS(){
00546 
00547   //Getting connection paremeters
00548   string oracleDB   = m_parameters.getParameter<string>("oracleDB");
00549   string pathCondDB = m_parameters.getParameter<string>("pathCondDB");
00550 
00551   // Connecting to OMDS 
00552   L1TOMDSHelper myOMDSHelper = L1TOMDSHelper();
00553   int conError;
00554   myOMDSHelper.connect(oracleDB,pathCondDB,conError);
00555 
00556   if(conError == L1TOMDSHelper::NO_ERROR){
00557 
00558     if(m_verbose){cout << "[L1TSync] Connected to DB with no error." << endl;}
00559     
00560     int errorRetrive;
00561     m_beamConfig = myOMDSHelper.getBeamConfiguration(m_lhcFill,errorRetrive);
00562 
00563     if(errorRetrive == L1TOMDSHelper::NO_ERROR){
00564       if(m_verbose){
00565         cout << "[L1TSync] Retriving LHC Bunch Structure: NO_ERROR" << endl;
00566         cout << "[L1TSync] -> LHC Bunch Structure valid=" << m_beamConfig.m_valid << " nBunches=" << m_beamConfig.nCollidingBunches << endl;
00567       }
00568     }
00569     else if(errorRetrive == L1TOMDSHelper::WARNING_DB_CONN_FAILED){
00570       
00571       if(m_verbose){cout << "[L1TSync] Retriving LHC Bunch Structure: WARNING_DB_CONN_FAILED" << endl;}
00572       
00573       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_INCORRECT_NBUNCHES);
00574       eCount++;
00575       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_INCORRECT_NBUNCHES,eCount);
00576     }
00577     else if(errorRetrive == L1TOMDSHelper::WARNING_DB_QUERY_FAILED){
00578       
00579       if(m_verbose){cout << "[L1TSync] Retriving LHC Bunch Structure: WARNING_DB_QUERY_FAILED" << endl;}
00580       
00581       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_QUERY_FAILED);
00582       eCount++;
00583       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_QUERY_FAILED,eCount);
00584     }
00585     else{
00586 
00587       if(m_verbose){cout << "[L1TSync] Retriving LHC Bunch Structure: UNKNOWN" << endl;}
00588       
00589       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
00590       eCount++;
00591       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN,eCount);
00592     }
00593 
00594   }else{
00595 
00596     if(conError == L1TOMDSHelper::WARNING_DB_CONN_FAILED){
00597       
00598       if(m_verbose){cout << "[L1TSync] Connect to DB: WARNING_DB_CONN_FAILED" << endl;}
00599        
00600       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(WARNING_DB_CONN_FAILED);
00601       eCount++;
00602       m_ErrorMonitor->getTH1()->SetBinContent(WARNING_DB_CONN_FAILED,eCount);
00603     }else{
00604       
00605       if(m_verbose){cout << "[L1TSync] Connect to DB: UNKNOWN" << endl;}
00606       
00607       int eCount = m_ErrorMonitor->getTH1()->GetBinContent(UNKNOWN);
00608       eCount++;
00609       m_ErrorMonitor->getTH1()->SetBinContent(UNKNOWN,eCount);
00610     }
00611 
00612   }
00613 
00614 }
00615 
00616 //_____________________________________________________________________
00617 // Method: doFractionInSync
00618 // Description: Produce plot with the fraction of in sync trigger for
00619 //              LS blocks with enough statistics.
00620 // Variable: iForce - Forces closing of all blocks and calculation of 
00621 //                    the respective fractions
00622 // Variable: iBad   - (Only works with iForce=true) Forces the current 
00623 //                    all current blocks to be marked as bad 
00624 //_____________________________________________________________________
00625 void L1TSync::doFractionInSync(bool iForce,bool iBad){
00626   
00627   for(map<string,string>::const_iterator i=m_selectedTriggers.begin() ; i!=m_selectedTriggers.end() ; i++){
00628 
00629     string theCategory     = (*i).first;
00630     string theTriggerAlias = (*i).second;
00631 
00632     // Caching frequently used values from maps
00633     unsigned int fLS = m_certFirstLS[theTriggerAlias];
00634     unsigned int lLS = m_certLastLS [theTriggerAlias];
00635     
00636     // Checking validity of the trigger alias and of the LS block
00637     bool triggerAlias_isValid = theTriggerAlias != "Undefined" && theTriggerAlias != "Undefined (Wrong Name)";
00638     bool lsBlock_exists       = !(fLS == 0 && lLS == 0);
00639     bool lsBlock_isValid      = fLS <= lLS && fLS > 0 && lLS > 0; 
00640 
00641     if(triggerAlias_isValid && lsBlock_exists && lsBlock_isValid){
00642 
00643       // If we are forced to close blocks and mark them bad
00644       if(iForce && iBad){
00645         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00646         m_certFirstLS[theTriggerAlias] = 0;
00647         m_certLastLS [theTriggerAlias] = 0;
00648       }
00649 
00650       // If we are not forced to mark bad, we check if we have enough statistics
00651       else{
00652 
00653         // Getting events with 0 bx difference between BPTX and Algo for current LS
00654         double CountSync = 0;
00655         double CountAll  = 0;
00656   
00657         // Adding all entries for current LS block
00658         for(unsigned int ls=fLS ; ls<=lLS ; ls++){
00659 
00660           CountSync += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls+1,3);
00661           for(int a=1 ; a<6 ; a++){
00662             CountAll  += m_algoVsBunchStructure[theTriggerAlias]->getBinContent(ls+1,a);
00663           }
00664         }
00665 
00666         if(m_verbose){
00667           cout << "Alias = " << theTriggerAlias 
00668                << " InitLS=" << fLS 
00669                << " EndLS=" <<  lLS 
00670                << " Events=" << CountAll ;
00671         } 
00672 
00673         if(iForce ||
00674            CountAll >= m_parameters.getParameter<ParameterSet>("Categories")
00675                                    .getParameter<ParameterSet>(theCategory)
00676                                    .getParameter<int>("CertMinEvents")){
00677 
00678           if(m_verbose){cout << " <--------------- Enough Statistics: ";}
00679 
00680         
00681           // Calculating fraction of in time 
00682           double fraction = 0;
00683           if(CountAll >0){fraction = CountSync/CountAll;}
00684 
00685           // This is to avoid having an entry equal to zero and thus
00686           // disregarded by the automatic tests
00687           if(fraction==0){fraction=0.000001;}
00688         
00689           certifyLSBlock(theTriggerAlias,fLS,lLS,fraction);
00690           m_certFirstLS[theTriggerAlias] = 0;
00691           m_certLastLS [theTriggerAlias] = 0;
00692         }
00693 
00694         if(m_verbose){cout << endl;}
00695 
00696       }
00697     }
00698 
00699     // A problem was found. We report it and set a not physical vale (-1) to the certification plot
00700     else{
00701 
00702       // If trigger alias is not valid report it to m_ErrorMonitor
00703       if(!triggerAlias_isValid){
00704         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_TRIGGERALIAS_NOTVALID);
00705         eCount++;
00706         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_TRIGGERALIAS_NOTVALID,eCount);
00707         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00708         m_certFirstLS[theTriggerAlias] = 0;
00709         m_certLastLS [theTriggerAlias] = 0;
00710       }
00711 
00712       // If LS Block is not valid report it to m_ErrorMonitor
00713       if(lsBlock_exists && !lsBlock_isValid){
00714         int eCount = m_ErrorMonitor->getTH1()->GetBinContent(ERROR_LSBLOCK_NOTVALID);
00715         eCount++;
00716         m_ErrorMonitor->getTH1()->SetBinContent(ERROR_LSBLOCK_NOTVALID,eCount);
00717         certifyLSBlock(theTriggerAlias,fLS,lLS,-1);
00718         m_certFirstLS[theTriggerAlias] = 0;
00719         m_certLastLS [theTriggerAlias] = 0;
00720       }
00721 
00722     }
00723   }
00724 
00725 }
00726 
00727 
00728 //_____________________________________________________________________
00729 // Method: certifyLSBlock
00730 // Description: Fill the trigger certification plot by blocks
00731 // Variable: iTrigger - Which trigger to certify
00732 // Variable: iInitLs  - Blocks initial LS
00733 // Variable: iEndLs   - Blocks end LS
00734 // Variable: iValue   - Value to be used to fill
00735 //_____________________________________________________________________
00736 void L1TSync::certifyLSBlock(string iTrigger, int iInitLs, int iEndLs ,float iValue){
00737   
00738   // Finding correct bins in the histogram for this block
00739   int binInit = m_algoCertification[iTrigger]->getTH1()->FindBin(iInitLs);
00740   int binEnd  = m_algoCertification[iTrigger]->getTH1()->FindBin(iEndLs);
00741 
00742   for(int ls=binInit ; ls<=binEnd ; ls++){
00743     m_algoCertification[iTrigger]->setBinContent(ls,iValue);
00744   }
00745 
00746 }
00747 
00748 //define this as a plug-in
00749 DEFINE_FWK_MODULE(L1TSync);