Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002  *  See header file for a description of this class.
0003  *
0004  *  \author C. Battilana - CIEMAT
0005  *
0006  *  threadsafe version (//-) oct/nov 2014 - WATWanAbdullah -ncpp-um-my
0007  *
0008  */
0009 
0010 // This class header
0011 #include "DQM/DTMonitorClient/src/DTLocalTriggerSynchTest.h"
0012 
0013 // Framework headers
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0016 #include "DQMServices/Core/interface/DQMStore.h"
0017 
0018 // Geometry
0019 #include "DQM/DTMonitorModule/interface/DTTrigGeomUtils.h"
0020 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0021 #include "Geometry/DTGeometry/interface/DTGeometry.h"
0022 
0023 // DB & Calib
0024 #include "CalibMuon/DTCalibration/interface/DTCalibDBUtils.h"
0025 #include "CondFormats/DataRecord/interface/DTStatusFlagRcd.h"
0026 #include "CondFormats/DTObjects/interface/DTStatusFlag.h"
0027 
0028 // Root
0029 #include "TF1.h"
0030 #include "TProfile.h"
0031 
0032 //C++ headers
0033 #include <iostream>
0034 #include <sstream>
0035 
0036 using namespace edm;
0037 using namespace std;
0038 
0039 DTLocalTriggerSynchTest::DTLocalTriggerSynchTest(const edm::ParameterSet& ps)
0040     : wPhaseMapToken_(esConsumes<edm::Transition::BeginRun>()) {
0041   setConfig(ps, "DTLocalTriggerSynch");
0042   baseFolderTM = "DT/90-LocalTriggerSynch/";
0043 
0044   bookingdone = false;
0045 }
0046 
0047 DTLocalTriggerSynchTest::~DTLocalTriggerSynchTest() {}
0048 
0049 void DTLocalTriggerSynchTest::beginRun(edm::Run const& run, edm::EventSetup const& c) {
0050   numHistoTag = parameters.getParameter<string>("numHistoTag");
0051   denHistoTag = parameters.getParameter<string>("denHistoTag");
0052   ratioHistoTag = parameters.getParameter<string>("ratioHistoTag");
0053   bxTime = parameters.getParameter<double>("bxTimeInterval");
0054   rangeInBX = parameters.getParameter<bool>("rangeWithinBX");
0055   nBXLow = parameters.getParameter<int>("nBXLow");
0056   nBXHigh = parameters.getParameter<int>("nBXHigh");
0057   minEntries = parameters.getParameter<int>("minEntries");
0058 
0059   DTLocalTriggerBaseTest::beginRun(run, c);
0060 }
0061 
0062 void DTLocalTriggerSynchTest::dqmEndLuminosityBlock(DQMStore::IBooker& ibooker,
0063                                                     DQMStore::IGetter& igetter,
0064                                                     edm::LuminosityBlock const& lumiSeg,
0065                                                     edm::EventSetup const& c) {
0066   if (bookingdone)
0067     return;
0068 
0069   vector<string>::const_iterator iTr = trigSources.begin();
0070   vector<string>::const_iterator trEnd = trigSources.end();
0071   vector<string>::const_iterator iHw = hwSources.begin();
0072   vector<string>::const_iterator hwEnd = hwSources.end();
0073 
0074   //Booking
0075   if (parameters.getUntrackedParameter<bool>("staticBooking", true)) {
0076     for (; iTr != trEnd; ++iTr) {
0077       trigSource = (*iTr);
0078       for (; iHw != hwEnd; ++iHw) {
0079         hwSource = (*iHw);
0080         std::vector<const DTChamber*>::const_iterator chambIt = muonGeom->chambers().begin();
0081         std::vector<const DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
0082         for (; chambIt != chambEnd; ++chambIt) {
0083           DTChamberId chId = ((*chambIt)->id());
0084           bookChambHistos(ibooker, chId, ratioHistoTag);
0085         }
0086       }
0087     }
0088   }
0089 
0090   LogVerbatim(category()) << "[" << testName << "Test]: book Histograms" << endl;
0091 
0092   if (parameters.getParameter<bool>("fineParamDiff")) {
0093     wPhaseMap = &c.getData(wPhaseMapToken_);
0094   }
0095 
0096   bookingdone = true;
0097 }
0098 
0099 void DTLocalTriggerSynchTest::runClientDiagnostic(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
0100   // Loop over Trig & Hw sources
0101   for (vector<string>::const_iterator iTr = trigSources.begin(); iTr != trigSources.end(); ++iTr) {
0102     trigSource = (*iTr);
0103     for (vector<string>::const_iterator iHw = hwSources.begin(); iHw != hwSources.end(); ++iHw) {
0104       hwSource = (*iHw);
0105       std::vector<const DTChamber*>::const_iterator chambIt = muonGeom->chambers().begin();
0106       std::vector<const DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
0107       for (; chambIt != chambEnd; ++chambIt) {
0108         DTChamberId chId = (*chambIt)->id();
0109         uint32_t indexCh = chId.rawId();
0110 
0111         // Perform peak finding
0112 
0113         TH1F* numH = getHisto<TH1F>(igetter.get(getMEName(numHistoTag, "", chId)));
0114         TH1F* denH = getHisto<TH1F>(igetter.get(getMEName(denHistoTag, "", chId)));
0115 
0116         if (numH && denH && numH->GetEntries() > minEntries && denH->GetEntries() > minEntries) {
0117           std::map<std::string, MonitorElement*> innerME = chambME[indexCh];
0118           MonitorElement* ratioH = innerME.find(fullName(ratioHistoTag))->second;
0119           makeRatioME(numH, denH, ratioH);
0120           try {
0121             //Need our own copy to avoid threading problems
0122             TF1 mypol8("mypol8", "pol8");
0123             getHisto<TH1F>(ratioH)->Fit(&mypol8, "CQO");
0124           } catch (cms::Exception& iException) {
0125             edm::LogPrint(category()) << "[" << testName << "Test]: Error fitting " << ratioH->getName()
0126                                       << " returned 0" << endl;
0127           }
0128         } else {
0129           if (!numH || !denH) {
0130             LogPrint(category()) << "[" << testName
0131                                  << "Test]: At least one of the required Histograms was not found for chamber " << chId
0132                                  << ". Peaks not computed" << endl;
0133           } else {
0134             LogPrint(category()) << "[" << testName << "Test]: Number of plots entries for " << chId
0135                                  << " is less than minEntries=" << minEntries << ".  Peaks not computed" << endl;
0136           }
0137         }
0138       }
0139     }
0140   }
0141 }
0142 
0143 void DTLocalTriggerSynchTest::dqmEndJob(DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
0144   DTLocalTriggerBaseTest::dqmEndJob(ibooker, igetter);
0145 
0146   if (parameters.getParameter<bool>("writeDB")) {
0147     LogVerbatim(category()) << "[" << testName << "Test]: writeDB flag set to true. Producing peak position database."
0148                             << endl;
0149 
0150     DTTPGParameters delayMap;
0151     hwSource = "TM";
0152     std::vector<const DTChamber*>::const_iterator chambIt = muonGeom->chambers().begin();
0153     std::vector<const DTChamber*>::const_iterator chambEnd = muonGeom->chambers().end();
0154     for (; chambIt != chambEnd; ++chambIt) {
0155       DTChamberId chId = (*chambIt)->id();
0156       float fineDelay = 0;
0157       int coarseDelay = static_cast<int>(
0158           (getFloatFromME(igetter, chId, "tTrig_SL1") + getFloatFromME(igetter, chId, "tTrig_SL3")) * 0.5 / bxTime);
0159 
0160       bool fineDiff = parameters.getParameter<bool>("fineParamDiff");
0161       bool coarseDiff = parameters.getParameter<bool>("coarseParamDiff");
0162 
0163       TH1F* ratioH = getHisto<TH1F>(igetter.get(getMEName(ratioHistoTag, "", chId)));
0164       if (ratioH->GetEntries() > minEntries) {
0165         TF1* fitF = ratioH->GetFunction("mypol8");
0166         if (fitF) {
0167           fineDelay = fitF->GetMaximumX(0, bxTime);
0168         }
0169       } else {
0170         LogInfo(category()) << "[" << testName << "Test]: Ratio histogram for chamber " << chId
0171                             << " is empty. Worst Phase value set to 0." << endl;
0172       }
0173 
0174       if (fineDiff || coarseDiff) {
0175         float wFine;
0176         int wCoarse;
0177         wPhaseMap->get(chId, wCoarse, wFine, DTTimeUnits::ns);
0178         if (fineDiff) {
0179           fineDelay = wFine - fineDelay;
0180         }
0181         if (coarseDiff) {
0182           coarseDelay = wCoarse - coarseDelay;
0183         }
0184       }
0185       delayMap.set(chId, coarseDelay, fineDelay, DTTimeUnits::ns);
0186     }
0187 
0188     std::vector<std::pair<DTTPGParametersId, DTTPGParametersData> >::const_iterator dbIt = delayMap.begin();
0189     std::vector<std::pair<DTTPGParametersId, DTTPGParametersData> >::const_iterator dbEnd = delayMap.end();
0190     for (; dbIt != dbEnd; ++dbIt) {
0191       LogVerbatim(category()) << "[" << testName << "Test]: DB entry for Wh " << (*dbIt).first.wheelId << " Sec "
0192                               << (*dbIt).first.sectorId << " St " << (*dbIt).first.stationId << " has coarse "
0193                               << (*dbIt).second.nClock << " and phase " << (*dbIt).second.tPhase << std::endl;
0194     }
0195 
0196     string delayRecord = "DTTPGParametersRcd";
0197     DTCalibDBUtils::writeToDB(delayRecord, delayMap);
0198   }
0199 }
0200 
0201 void DTLocalTriggerSynchTest::makeRatioME(TH1F* numerator, TH1F* denominator, MonitorElement* result) {
0202   TH1F* efficiency = result->getTH1F();
0203   efficiency->Divide(numerator, denominator, 1, 1, "");
0204 }
0205 
0206 float DTLocalTriggerSynchTest::getFloatFromME(DQMStore::IGetter& igetter, DTChamberId chId, std::string meType) {
0207   stringstream wheel;
0208   wheel << chId.wheel();
0209   stringstream station;
0210   station << chId.station();
0211   stringstream sector;
0212   sector << chId.sector();
0213 
0214   string folderName = topFolder() + "Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str() + "/";
0215 
0216   string histoname =
0217       sourceFolder + folderName + meType + "_W" + wheel.str() + "_Sec" + sector.str() + "_St" + station.str();
0218 
0219   MonitorElement* me = igetter.get(histoname);
0220   if (me) {
0221     return me->getFloatValue();
0222   } else {
0223     LogProblem(category()) << "[" << testName << "Test]: " << histoname << " is not a valid ME. 0 returned"
0224                            << std::endl;
0225   }
0226 
0227   return 0;
0228 }
0229 
0230 void DTLocalTriggerSynchTest::bookChambHistos(DQMStore::IBooker& ibooker,
0231                                               DTChamberId chambId,
0232                                               string htype,
0233                                               string subfolder) {
0234   stringstream wheel;
0235   wheel << chambId.wheel();
0236   stringstream station;
0237   station << chambId.station();
0238   stringstream sector;
0239   sector << chambId.sector();
0240 
0241   string fullType = fullName(htype);
0242   string HistoName = fullType + "_W" + wheel.str() + "_Sec" + sector.str() + "_St" + station.str();
0243 
0244   string folder = topFolder() + "Wheel" + wheel.str() + "/Sector" + sector.str() + "/Station" + station.str();
0245   if (!subfolder.empty()) {
0246     folder += "7" + subfolder;
0247   }
0248 
0249   ibooker.setCurrentFolder(folder);
0250 
0251   LogPrint(category()) << "[" << testName << "Test]: booking " << folder << "/" << HistoName;
0252 
0253   uint32_t indexChId = chambId.rawId();
0254   float min = rangeInBX ? 0 : nBXLow * bxTime;
0255   float max = rangeInBX ? bxTime : nBXHigh * bxTime;
0256   int nbins = static_cast<int>(ceil(rangeInBX ? bxTime : (nBXHigh - nBXLow) * bxTime));
0257 
0258   chambME[indexChId][fullType] =
0259       ibooker.book1D(HistoName.c_str(), "All/HH ratio vs Muon Arrival Time", nbins, min, max);
0260 }