CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/DQM/DTMonitorClient/src/DTLocalTriggerSynchTest.cc

Go to the documentation of this file.
00001 /*
00002  *  See header file for a description of this class.
00003  *
00004  *  $Date: 2010/01/07 10:51:14 $
00005  *  $Revision: 1.4 $
00006  *  \author C. Battilana - CIEMAT
00007  */
00008 
00009 
00010 // This class header
00011 #include "DQM/DTMonitorClient/src/DTLocalTriggerSynchTest.h"
00012 
00013 // Framework headers
00014 #include "FWCore/Framework/interface/EventSetup.h"
00015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00016 #include "DQMServices/Core/interface/MonitorElement.h"
00017 #include "DQMServices/Core/interface/DQMStore.h"
00018 
00019 // Geometry
00020 #include "DQM/DTMonitorModule/interface/DTTrigGeomUtils.h"
00021 #include "Geometry/Records/interface/MuonGeometryRecord.h"
00022 #include "Geometry/DTGeometry/interface/DTGeometry.h"
00023 
00024 // DB & Calib
00025 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
00026 #include "CondFormats/DataRecord/interface/DTTPGParametersRcd.h"
00027 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
00028 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
00029 
00030 
00031 // Root
00032 #include "TF1.h"
00033 #include "TProfile.h"
00034 
00035 
00036 //C++ headers
00037 #include <iostream>
00038 #include <sstream>
00039 
00040 using namespace edm;
00041 using namespace std;
00042 
00043 
00044 DTLocalTriggerSynchTest::DTLocalTriggerSynchTest(const edm::ParameterSet& ps) {
00045 
00046   setConfig(ps,"DTLocalTriggerSynch");
00047   baseFolderDCC = "DT/90-LocalTriggerSynch/";
00048   baseFolderDDU = "DT/90-LocalTriggerSynch/";
00049 
00050 }
00051 
00052 
00053 DTLocalTriggerSynchTest::~DTLocalTriggerSynchTest(){
00054 
00055 }
00056 
00057 
00058 void DTLocalTriggerSynchTest::beginJob(){
00059   
00060   numHistoTag   = parameters.getParameter<string>("numHistoTag");
00061   denHistoTag   = parameters.getParameter<string>("denHistoTag");
00062   ratioHistoTag = parameters.getParameter<string>("ratioHistoTag");
00063   bxTime        = parameters.getParameter<double>("bxTimeInterval");
00064   rangeInBX     = parameters.getParameter<bool>("rangeWithinBX");
00065   nBXLow        = parameters.getParameter<int>("nBXLow");
00066   nBXHigh       = parameters.getParameter<int>("nBXHigh");
00067   minEntries    = parameters.getParameter<int>("minEntries");
00068 
00069 }
00070 
00071 void DTLocalTriggerSynchTest::beginRun(const Run& run, const EventSetup& c) {
00072 
00073   DTLocalTriggerBaseTest::beginRun(run,c);
00074 
00075   vector<string>::const_iterator iTr   = trigSources.begin();
00076   vector<string>::const_iterator trEnd = trigSources.end();
00077   vector<string>::const_iterator iHw   = hwSources.begin();
00078   vector<string>::const_iterator hwEnd = hwSources.end();
00079 
00080   //Booking
00081   if(parameters.getUntrackedParameter<bool>("staticBooking", true)){
00082     for (; iTr != trEnd; ++iTr){
00083       trigSource = (*iTr);
00084       for (; iHw != hwEnd; ++iHw){
00085         hwSource = (*iHw);
00086         std::vector<DTChamber*>::const_iterator chambIt  = muonGeom->chambers().begin();
00087         std::vector<DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
00088         for (; chambIt!=chambEnd; ++chambIt) { 
00089           DTChamberId chId = ((*chambIt)->id());
00090           bookChambHistos(chId,ratioHistoTag);
00091         }
00092       }
00093     }
00094   }
00095 
00096   LogVerbatim(category()) << "[" << testName << "Test]: beginRun" << endl;
00097 
00098   if (parameters.getParameter<bool>("fineParamDiff")) {
00099     ESHandle<DTTPGParameters> wPhaseHandle;
00100     c.get<DTTPGParametersRcd>().get(wPhaseHandle);
00101     wPhaseMap = (*wPhaseHandle);
00102   }
00103 
00104 }
00105   
00106 
00107 
00108 void DTLocalTriggerSynchTest::runClientDiagnostic() {
00109 
00110   // Loop over Trig & Hw sources
00111   for (vector<string>::const_iterator iTr = trigSources.begin(); iTr != trigSources.end(); ++iTr){
00112     trigSource = (*iTr);
00113     for (vector<string>::const_iterator iHw = hwSources.begin(); iHw != hwSources.end(); ++iHw){
00114       hwSource = (*iHw);
00115       std::vector<DTChamber*>::const_iterator chambIt  = muonGeom->chambers().begin();
00116       std::vector<DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
00117       for (; chambIt!=chambEnd; ++chambIt) { 
00118         DTChamberId chId = (*chambIt)->id();
00119         uint32_t indexCh = chId.rawId();
00120 
00121         // Perform peak finding
00122         TH1F *numH     = getHisto<TH1F>(dbe->get(getMEName(numHistoTag,"", chId)));
00123         TH1F *denH     = getHisto<TH1F>(dbe->get(getMEName(denHistoTag,"", chId)));
00124             
00125         if (numH && denH && numH->GetEntries()>minEntries && denH->GetEntries()>minEntries) {         
00126           std::map<std::string,MonitorElement*> innerME = chambME[indexCh];
00127           MonitorElement* ratioH = innerME.find(fullName(ratioHistoTag))->second;
00128           makeRatioME(numH,denH,ratioH);
00129           try {
00130             getHisto<TH1F>(ratioH)->Fit("pol8","CQO");
00131           } catch (...) {
00132             edm::LogPrint(category()) << "[" << testName 
00133                                      << "Test]: Error fitting " 
00134                                      << ratioH->getName() << " returned 0" << endl;
00135           }
00136         } else { 
00137           if (!numH || !denH) {
00138             LogPrint(category()) << "[" << testName 
00139                                 << "Test]: At least one of the required Histograms was not found for chamber " 
00140                                 << chId << ". Peaks not computed" << endl;
00141           } else {
00142             LogPrint(category()) << "[" << testName 
00143                                 << "Test]: Number of plots entries for " 
00144                                 << chId << " is less than minEntries=" 
00145                                 << minEntries <<".  Peaks not computed" << endl;
00146           }
00147         }
00148 
00149       }
00150     }
00151   }     
00152 
00153 }
00154 
00155 void DTLocalTriggerSynchTest::endJob(){
00156 
00157   DTLocalTriggerBaseTest::endJob();
00158 
00159   if ( parameters.getParameter<bool>("writeDB")) {
00160     LogVerbatim(category()) << "[" << testName 
00161                             << "Test]: writeDB flag set to true. Producing peak position database." << endl;
00162 
00163     DTTPGParameters* delayMap = new DTTPGParameters();
00164     hwSource =  parameters.getParameter<bool>("dbFromDCC") ? "DCC" : "DDU";
00165     std::vector<DTChamber*>::const_iterator chambIt  = muonGeom->chambers().begin();
00166     std::vector<DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
00167       for (; chambIt!=chambEnd; ++chambIt) { 
00168 
00169         DTChamberId chId = (*chambIt)->id();
00170         float fineDelay = 0;
00171         int coarseDelay = static_cast<int>((getFloatFromME(chId,"tTrig_SL1") + getFloatFromME(chId,"tTrig_SL3"))*0.5/bxTime);
00172 
00173         bool fineDiff   = parameters.getParameter<bool>("fineParamDiff");
00174         bool coarseDiff = parameters.getParameter<bool>("coarseParamDiff");
00175 
00176 
00177         TH1F *ratioH     = getHisto<TH1F>(dbe->get(getMEName(ratioHistoTag,"", chId)));    
00178         if (ratioH->GetEntries()>minEntries) {        
00179           TF1 *fitF=ratioH->GetFunction("pol8");
00180           if (fitF) { fineDelay=fitF->GetMaximumX(0,bxTime); }
00181         } else {
00182           LogInfo(category()) << "[" << testName 
00183                               << "Test]: Ratio histogram for chamber " << chId
00184                               << " is empty. Worst Phase value set to 0." << endl;
00185         }
00186 
00187         if (fineDiff || coarseDiff) {
00188           float wFine;
00189           int wCoarse;
00190           wPhaseMap.get(chId,wCoarse,wFine,DTTimeUnits::ns);
00191           if (fineDiff)   { fineDelay = wFine - fineDelay; }
00192           if (coarseDiff) { coarseDelay = wCoarse - coarseDelay; }
00193         } 
00194         delayMap->set(chId,coarseDelay,fineDelay,DTTimeUnits::ns);
00195       }
00196 
00197       std::vector< std::pair<DTTPGParametersId,DTTPGParametersData> >::const_iterator dbIt  = delayMap->begin();
00198       std::vector< std::pair<DTTPGParametersId,DTTPGParametersData> >::const_iterator dbEnd = delayMap->end();
00199       for (; dbIt!=dbEnd; ++dbIt) {
00200         LogVerbatim(category()) << "[" << testName << "Test]: DB entry for Wh " << (*dbIt).first.wheelId 
00201                                 << " Sec " << (*dbIt).first.sectorId 
00202                                 << " St " << (*dbIt).first.stationId 
00203                                 << " has coarse " << (*dbIt).second.nClock
00204                                 << " and phase " << (*dbIt).second.tPhase << std::endl;
00205       }
00206       
00207       string delayRecord = "DTTPGParametersRcd";
00208       DTCalibDBUtils::writeToDB(delayRecord,delayMap);
00209       
00210   }
00211 
00212 }
00213 
00214 
00215 
00216 void DTLocalTriggerSynchTest::makeRatioME(TH1F* numerator, TH1F* denominator, MonitorElement* result){
00217   
00218   TH1F* efficiency = result->getTH1F();
00219   efficiency->Divide(numerator,denominator,1,1,"");
00220   
00221 }
00222 
00223 float DTLocalTriggerSynchTest::getFloatFromME(DTChamberId chId, std::string meType) {
00224    
00225    stringstream wheel; wheel << chId.wheel();
00226    stringstream station; station << chId.station();
00227    stringstream sector; sector << chId.sector();
00228 
00229    string folderName = topFolder(hwSource=="DCC") + "Wheel" +  wheel.str() +
00230      "/Sector" + sector.str() + "/Station" + station.str() + "/" ; 
00231 
00232    string histoname = sourceFolder + folderName 
00233      + meType
00234      + "_W" + wheel.str()  
00235      + "_Sec" + sector.str()
00236      + "_St" + station.str();
00237 
00238    MonitorElement* me = dbe->get(histoname);
00239    if (me) { 
00240      return me->getFloatValue(); 
00241    }
00242    else { 
00243      LogProblem(category()) << "[" << testName << "Test]: " << histoname << " is not a valid ME. 0 returned" << std::endl;
00244    }
00245    
00246    return 0;
00247 
00248  }
00249 
00250 void DTLocalTriggerSynchTest::bookChambHistos(DTChamberId chambId, string htype, string subfolder) {
00251   
00252   stringstream wheel; wheel << chambId.wheel();
00253   stringstream station; station << chambId.station();   
00254   stringstream sector; sector << chambId.sector();
00255 
00256   string fullType  = fullName(htype);
00257   bool isDCC = hwSource=="DCC" ;
00258   string HistoName = fullType + "_W" + wheel.str() + "_Sec" + sector.str() + "_St" + station.str();
00259 
00260   string folder = topFolder(isDCC) + "Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str();
00261   if ( subfolder!="") { folder += "7" + subfolder; }
00262 
00263   dbe->setCurrentFolder(folder);
00264 
00265   LogPrint(category()) << "[" << testName << "Test]: booking " << folder << "/" <<HistoName;
00266 
00267   
00268   uint32_t indexChId = chambId.rawId();
00269   float min = rangeInBX ?      0 : nBXLow*bxTime;
00270   float max = rangeInBX ? bxTime : nBXHigh*bxTime;
00271   int nbins = static_cast<int>(ceil( rangeInBX ? bxTime : (nBXHigh-nBXLow)*bxTime));
00272 
00273   chambME[indexChId][fullType] = dbe->book1D(HistoName.c_str(),"All/HH ratio vs Muon Arrival Time",nbins,min,max);
00274 
00275 }