Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-27 04:18:03

0001 /** \class GEMTriggerPrimitivesAnalyzer
0002  *
0003  * Basic analyzer class accesses GEM TPs and plot various quantities. This analyzer
0004  * can currently load a DQM file and plot the data vs emulation of GEM TPs.
0005  *
0006  * \author Sven Dildick (Rice University)
0007  *
0008  */
0009 
0010 #include "FWCore/Utilities/interface/EDGetToken.h"
0011 #include "FWCore/Framework/interface/Frameworkfwd.h"
0012 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Framework/interface/MakerMacros.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0020 
0021 #include "TH1F.h"
0022 #include "TPostScript.h"
0023 #include "TCanvas.h"
0024 #include "TFile.h"
0025 #include "TText.h"
0026 #include "TPaveLabel.h"
0027 #include "TLegend.h"
0028 #include "TStyle.h"
0029 #include "TROOT.h"
0030 
0031 #include <iostream>
0032 #include <string>
0033 
0034 class GEMTriggerPrimitivesAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0035 public:
0036   /// Constructor
0037   explicit GEMTriggerPrimitivesAnalyzer(const edm::ParameterSet &conf);
0038 
0039   /// Destructor
0040   ~GEMTriggerPrimitivesAnalyzer() override {}
0041 
0042   /// Does the job
0043   void analyze(const edm::Event &event, const edm::EventSetup &setup) override;
0044 
0045   /// Write to ROOT file, make plots, etc.
0046   void endJob() override;
0047 
0048 private:
0049   void makePlot(TH1F *dataMon,
0050                 TH1F *emulMon,
0051                 TH1F *diffMon,
0052                 TString lcts,
0053                 TString lct,
0054                 TString var,
0055                 TString chamber,
0056                 TPostScript *ps,
0057                 TCanvas *c1) const;
0058 
0059   // plots of data vs emulator
0060   std::string rootFileName_;
0061   unsigned runNumber_;
0062   std::string monitorDir_;
0063   std::vector<std::string> chambers_;
0064   std::vector<std::string> clusterVars_;
0065   bool dataVsEmulatorPlots_;
0066   void makeDataVsEmulatorPlots();
0067 
0068   /*
0069     When set to True, we assume that the data comes from
0070     the Building 904 GEM test-stand. This test-stand is a single
0071     ME1/1 chamber.
0072   */
0073   bool B904Setup_;
0074   // label only relevant for B904 local runs
0075   std::string B904RunNumber_;
0076 };
0077 
0078 GEMTriggerPrimitivesAnalyzer::GEMTriggerPrimitivesAnalyzer(const edm::ParameterSet &conf)
0079     : rootFileName_(conf.getParameter<std::string>("rootFileName")),
0080       runNumber_(conf.getParameter<unsigned>("runNumber")),
0081       monitorDir_(conf.getParameter<std::string>("monitorDir")),
0082       chambers_(conf.getParameter<std::vector<std::string>>("chambers")),
0083       clusterVars_(conf.getParameter<std::vector<std::string>>("clusterVars")),
0084       dataVsEmulatorPlots_(conf.getParameter<bool>("dataVsEmulatorPlots")),
0085       B904Setup_(conf.getParameter<bool>("B904Setup")),
0086       B904RunNumber_(conf.getParameter<std::string>("B904RunNumber")) {
0087   usesResource("TFileService");
0088 }
0089 
0090 void GEMTriggerPrimitivesAnalyzer::analyze(const edm::Event &ev, const edm::EventSetup &setup) {}
0091 
0092 void GEMTriggerPrimitivesAnalyzer::endJob() {
0093   if (dataVsEmulatorPlots_)
0094     makeDataVsEmulatorPlots();
0095 }
0096 
0097 void GEMTriggerPrimitivesAnalyzer::makeDataVsEmulatorPlots() {
0098   // data vs emulator plots are created here
0099   edm::Service<TFileService> fs;
0100 
0101   // split monitorDir_ into two substrings
0102   std::string delimiter = "/";
0103   int pos = monitorDir_.find(delimiter);
0104   std::string superDir = monitorDir_.substr(0, pos);
0105   monitorDir_.erase(0, pos + delimiter.length());
0106   std::string subDir = monitorDir_;
0107   std::string path = "DQMData/Run " + std::to_string(runNumber_) + "/" + superDir + "/Run summary/" + subDir + "/";
0108 
0109   TFile *theFile = new TFile(rootFileName_.c_str());
0110   if (!theFile) {
0111     edm::LogError("GEMTriggerPrimitivesAnalyzer") << "Unable to load DQM ROOT file: " << rootFileName_;
0112     return;
0113   }
0114 
0115   TDirectory *directory = theFile->GetDirectory(path.c_str());
0116   if (!directory) {
0117     edm::LogError("GEMTriggerPrimitivesAnalyzer") << "Unable to navigate to directory: " << path;
0118     return;
0119   }
0120 
0121   TString runTitle = "CMS_Run_" + std::to_string(runNumber_);
0122   if (B904Setup_)
0123     runTitle = "B904_Cosmic_Run_" + TString(B904RunNumber_);
0124 
0125   TPostScript *ps = new TPostScript("GEM_dataVsEmul_" + runTitle + ".ps", 111);
0126   TCanvas *c1 = new TCanvas("c1", "", 800, 800);
0127   c1->Clear();
0128   c1->Divide(1, 2);
0129 
0130   // alct variable
0131   for (unsigned iVar = 0; iVar < clusterVars_.size(); iVar++) {
0132     // chamber type
0133     for (unsigned iType = 0; iType < chambers_.size(); iType++) {
0134       const std::string key("cluster_" + clusterVars_[iVar]);
0135       const std::string histData(key + "_data_" + chambers_[iType]);
0136       const std::string histEmul(key + "_emul_" + chambers_[iType]);
0137       const std::string histDiff(key + "_diff_" + chambers_[iType]);
0138 
0139       TH1F *dataMon = (TH1F *)directory->Get(histData.c_str());
0140       TH1F *emulMon = (TH1F *)directory->Get(histEmul.c_str());
0141       TH1F *diffMon = (TH1F *)directory->Get(histDiff.c_str());
0142 
0143       // when all histograms are found, make a new canvas and add it to
0144       // the collection
0145       if (dataMon && emulMon && diffMon) {
0146         makePlot(dataMon,
0147                  emulMon,
0148                  diffMon,
0149                  "GEM Cluster",
0150                  "gemcluster_",
0151                  TString(clusterVars_[iVar]),
0152                  TString(chambers_[iType]),
0153                  ps,
0154                  c1);
0155       }
0156     }
0157   }
0158 
0159   ps->Close();
0160   // close the DQM file
0161   theFile->Close();
0162   delete c1;
0163   delete ps;
0164 }
0165 
0166 void GEMTriggerPrimitivesAnalyzer::makePlot(TH1F *dataMon,
0167                                             TH1F *emulMon,
0168                                             TH1F *diffMon,
0169                                             TString lcts,
0170                                             TString lct,
0171                                             TString var,
0172                                             TString chamber,
0173                                             TPostScript *ps,
0174                                             TCanvas *c1) const {
0175   ps->NewPage();
0176 
0177   TString runTitle = "(CMS Run " + std::to_string(runNumber_) + ")";
0178   if (B904Setup_)
0179     runTitle = "(B904 Cosmic Run " + TString(B904RunNumber_) + ")";
0180   const TString title(chamber + " " + lcts + " " + var + " " + runTitle);
0181   c1->cd(1);
0182   gPad->SetGridx();
0183   gPad->SetGridy();
0184   gStyle->SetOptStat(1111);
0185   dataMon->SetTitle(title);
0186   dataMon->GetXaxis()->SetTitle(lcts + " " + var);
0187   dataMon->GetYaxis()->SetTitle("Entries");
0188   dataMon->SetMarkerColor(kBlack);
0189   dataMon->SetMarkerStyle(kPlus);
0190   dataMon->SetMarkerSize(3);
0191   // add 50% to make sure the legend does not overlap with the histograms
0192   dataMon->SetMaximum(dataMon->GetBinContent(dataMon->GetMaximumBin()) * 1.6);
0193   dataMon->Draw("histp");
0194   dataMon->GetXaxis()->SetLabelSize(0.05);
0195   dataMon->GetYaxis()->SetLabelSize(0.05);
0196   dataMon->GetXaxis()->SetTitleSize(0.05);
0197   dataMon->GetYaxis()->SetTitleSize(0.05);
0198   emulMon->SetLineColor(kRed);
0199   emulMon->Draw("histsame");
0200   auto legend = new TLegend(0.6, 0.7, 0.9, 0.9);
0201   legend->AddEntry(dataMon, TString("Data (" + std::to_string((int)dataMon->GetEntries()) + ")"), "p");
0202   legend->AddEntry(emulMon, TString("Emulator (" + std::to_string((int)emulMon->GetEntries()) + ")"), "l");
0203   legend->Draw();
0204 
0205   c1->cd(2);
0206   gPad->SetGridx();
0207   gPad->SetGridy();
0208   gStyle->SetOptStat(0);
0209   diffMon->SetLineColor(kBlack);
0210   diffMon->SetTitle(title);
0211   diffMon->GetXaxis()->SetTitle(lcts + " " + var);
0212   diffMon->GetYaxis()->SetTitle("Emul - Data");
0213   diffMon->GetXaxis()->SetLabelSize(0.05);
0214   diffMon->GetYaxis()->SetLabelSize(0.05);
0215   diffMon->GetXaxis()->SetTitleSize(0.05);
0216   diffMon->GetYaxis()->SetTitleSize(0.05);
0217   diffMon->Draw("ep");
0218   c1->Update();
0219 }
0220 
0221 DEFINE_FWK_MODULE(GEMTriggerPrimitivesAnalyzer);