Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-03 23:52:11

0001 /** \class CSCTriggerPrimitivesAnalyzer
0002  *
0003  * Basic analyzer class which accesses ALCTs, CLCTs, and correlated LCTs
0004  * and plot various quantities. This analyzer can currently load a DQM file
0005  * and plot the data vs emulation of ALCTs, CLCTs, and correlated LCT properties.
0006  *
0007  * \author Sven Dildick (Rice University)
0008  *
0009  */
0010 
0011 #include "FWCore/Utilities/interface/EDGetToken.h"
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0014 #include "FWCore/Framework/interface/Event.h"
0015 #include "FWCore/Framework/interface/EventSetup.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019 #include "FWCore/ServiceRegistry/interface/Service.h"
0020 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0021 
0022 #include "TH1F.h"
0023 #include "TH2F.h"
0024 #include "TPostScript.h"
0025 #include "TCanvas.h"
0026 #include "TFile.h"
0027 #include "TText.h"
0028 #include "TPaveLabel.h"
0029 #include "TLegend.h"
0030 #include "TStyle.h"
0031 #include "TROOT.h"
0032 
0033 #include <iostream>
0034 #include <string>
0035 
0036 class CSCTriggerPrimitivesAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0037 public:
0038   /// Constructor
0039   explicit CSCTriggerPrimitivesAnalyzer(const edm::ParameterSet &conf);
0040 
0041   /// Destructor
0042   ~CSCTriggerPrimitivesAnalyzer() override {}
0043 
0044   /// Does the job
0045   void analyze(const edm::Event &event, const edm::EventSetup &setup) override;
0046 
0047   /// Write to ROOT file, make plots, etc.
0048   void endJob() override;
0049 
0050 private:
0051   void makePlot(TH1F *dataMon,
0052                 TH1F *emulMon,
0053                 TH1F *diffMon,
0054                 TString lcts,
0055                 TString lct,
0056                 TString var,
0057                 TString chamber,
0058                 TPostScript *ps,
0059                 TCanvas *c1) const;
0060 
0061   void make2DPlot(TH2F *effMon, TPostScript *ps, TCanvas *c1) const;
0062 
0063   // plots of data vs emulator
0064   std::string rootFileName_;
0065   unsigned runNumber_;
0066   std::string monitorDir_;
0067   std::vector<std::string> chambers_;
0068   std::vector<std::string> alctVars_;
0069   std::vector<std::string> clctVars_;
0070   std::vector<std::string> lctVars_;
0071   bool dataVsEmulatorPlots_;
0072   void makeDataVsEmulatorPlots();
0073 
0074   /*
0075     When set to True, we assume that the data comes from
0076     the Building 904 CSC test-stand. This test-stand is a single
0077     ME1/1 chamber.
0078   */
0079   bool useB904_;
0080   bool useB904ME11_;
0081   bool useB904ME21_;
0082   bool useB904ME234s2_;
0083   // label only relevant for B904 local runs
0084   std::string B904RunNumber_;
0085 };
0086 
0087 CSCTriggerPrimitivesAnalyzer::CSCTriggerPrimitivesAnalyzer(const edm::ParameterSet &conf)
0088     : rootFileName_(conf.getParameter<std::string>("rootFileName")),
0089       runNumber_(conf.getParameter<unsigned>("runNumber")),
0090       monitorDir_(conf.getParameter<std::string>("monitorDir")),
0091       chambers_(conf.getParameter<std::vector<std::string>>("chambers")),
0092       alctVars_(conf.getParameter<std::vector<std::string>>("alctVars")),
0093       clctVars_(conf.getParameter<std::vector<std::string>>("clctVars")),
0094       lctVars_(conf.getParameter<std::vector<std::string>>("lctVars")),
0095       dataVsEmulatorPlots_(conf.getParameter<bool>("dataVsEmulatorPlots")),
0096       useB904ME11_(conf.getParameter<bool>("useB904ME11")),
0097       useB904ME21_(conf.getParameter<bool>("useB904ME21")),
0098       useB904ME234s2_(conf.getParameter<bool>("useB904ME234s2")),
0099       B904RunNumber_(conf.getParameter<std::string>("B904RunNumber")) {
0100   usesResource("TFileService");
0101   useB904_ = useB904ME11_ or useB904ME21_ or useB904ME234s2_;
0102 }
0103 
0104 void CSCTriggerPrimitivesAnalyzer::analyze(const edm::Event &ev, const edm::EventSetup &setup) {}
0105 
0106 void CSCTriggerPrimitivesAnalyzer::endJob() {
0107   if (dataVsEmulatorPlots_)
0108     makeDataVsEmulatorPlots();
0109 }
0110 
0111 void CSCTriggerPrimitivesAnalyzer::makeDataVsEmulatorPlots() {
0112   // data vs emulator plots are created here
0113   edm::Service<TFileService> fs;
0114 
0115   // split monitorDir_ into two substrings
0116   std::string delimiter = "/";
0117   int pos = monitorDir_.find(delimiter);
0118   std::string superDir = monitorDir_.substr(0, pos);
0119   monitorDir_.erase(0, pos + delimiter.length());
0120   std::string subDir = monitorDir_;
0121   std::string path = "DQMData/Run " + std::to_string(runNumber_) + "/" + superDir + "/Run summary/" + subDir + "/";
0122 
0123   TFile *theFile = new TFile(rootFileName_.c_str());
0124   if (!theFile) {
0125     edm::LogError("CSCTriggerPrimitivesAnalyzer") << "Unable to load DQM ROOT file: " << rootFileName_;
0126     return;
0127   }
0128 
0129   TDirectory *directory = theFile->GetDirectory(path.c_str());
0130   if (!directory) {
0131     edm::LogError("CSCTriggerPrimitivesAnalyzer") << "Unable to navigate to directory: " << path;
0132     return;
0133   }
0134 
0135   TString runTitle = "CMS_Run_" + std::to_string(runNumber_);
0136   if (useB904_)
0137     runTitle = "B904_Cosmic_Run_" + TString(B904RunNumber_);
0138 
0139   TPostScript *ps = new TPostScript("CSC_dataVsEmul_" + runTitle + ".ps", 111);
0140   TCanvas *c1 = new TCanvas("c1", "", 800, 800);
0141   c1->Clear();
0142   c1->Divide(1, 2);
0143 
0144   // alct variable
0145   for (unsigned iVar = 0; iVar < alctVars_.size(); iVar++) {
0146     // chamber type
0147     for (unsigned iType = 0; iType < chambers_.size(); iType++) {
0148       const std::string key("alct_" + alctVars_[iVar]);
0149       const std::string histData(key + "_data_" + chambers_[iType]);
0150       const std::string histEmul(key + "_emul_" + chambers_[iType]);
0151       const std::string histDiff(key + "_diff_" + chambers_[iType]);
0152 
0153       TH1F *dataMon = (TH1F *)directory->Get(histData.c_str());
0154       TH1F *emulMon = (TH1F *)directory->Get(histEmul.c_str());
0155       TH1F *diffMon = (TH1F *)directory->Get(histDiff.c_str());
0156 
0157       // when all histograms are found, make a new canvas and add it to
0158       // the collection
0159       if (dataMon && emulMon && diffMon) {
0160         makePlot(
0161             dataMon, emulMon, diffMon, "ALCT", "alct_", TString(alctVars_[iVar]), TString(chambers_[iType]), ps, c1);
0162       }
0163     }
0164   }
0165 
0166   // clct variable
0167   for (unsigned iVar = 0; iVar < clctVars_.size(); iVar++) {
0168     // chamber type
0169     for (unsigned iType = 0; iType < chambers_.size(); iType++) {
0170       const std::string key("clct_" + clctVars_[iVar]);
0171       const std::string histData(key + "_data_" + chambers_[iType]);
0172       const std::string histEmul(key + "_emul_" + chambers_[iType]);
0173       const std::string histDiff(key + "_diff_" + chambers_[iType]);
0174 
0175       TH1F *dataMon = (TH1F *)directory->Get(histData.c_str());
0176       TH1F *emulMon = (TH1F *)directory->Get(histEmul.c_str());
0177       TH1F *diffMon = (TH1F *)directory->Get(histDiff.c_str());
0178 
0179       // when all histograms are found, make a new canvas and add it to
0180       // the collection
0181       if (dataMon && emulMon && diffMon) {
0182         makePlot(
0183             dataMon, emulMon, diffMon, "CLCT", "clct_", TString(clctVars_[iVar]), TString(chambers_[iType]), ps, c1);
0184       }
0185     }
0186   }
0187 
0188   // lct variable
0189   for (unsigned iVar = 0; iVar < lctVars_.size(); iVar++) {
0190     // chamber type
0191     for (unsigned iType = 0; iType < chambers_.size(); iType++) {
0192       const std::string key("lct_" + lctVars_[iVar]);
0193       const std::string histData(key + "_data_" + chambers_[iType]);
0194       const std::string histEmul(key + "_emul_" + chambers_[iType]);
0195       const std::string histDiff(key + "_diff_" + chambers_[iType]);
0196 
0197       TH1F *dataMon = (TH1F *)directory->Get(histData.c_str());
0198       TH1F *emulMon = (TH1F *)directory->Get(histEmul.c_str());
0199       TH1F *diffMon = (TH1F *)directory->Get(histDiff.c_str());
0200 
0201       // when all histograms are found, make a new canvas and add it to
0202       // the collection
0203       if (dataMon && emulMon && diffMon) {
0204         makePlot(dataMon, emulMon, diffMon, "LCT", "lct_", TString(lctVars_[iVar]), TString(chambers_[iType]), ps, c1);
0205       }
0206     }
0207   }
0208 
0209   if (!useB904_) {
0210     TH2F *h_lctDataSummary_eff = (TH2F *)directory->Get("lct_csctp_data_summary_eff");
0211     make2DPlot(h_lctDataSummary_eff, ps, c1);
0212     TH2F *h_alctDataSummary_eff = (TH2F *)directory->Get("alct_csctp_data_summary_eff");
0213     make2DPlot(h_alctDataSummary_eff, ps, c1);
0214     TH2F *h_clctDataSummary_eff = (TH2F *)directory->Get("clct_csctp_data_summary_eff");
0215     make2DPlot(h_clctDataSummary_eff, ps, c1);
0216 
0217     TH2F *h_lctEmulSummary_eff = (TH2F *)directory->Get("lct_csctp_emul_summary_eff");
0218     make2DPlot(h_lctEmulSummary_eff, ps, c1);
0219     TH2F *h_alctEmulSummary_eff = (TH2F *)directory->Get("alct_csctp_emul_summary_eff");
0220     make2DPlot(h_alctEmulSummary_eff, ps, c1);
0221     TH2F *h_clctEmulSummary_eff = (TH2F *)directory->Get("clct_csctp_emul_summary_eff");
0222     make2DPlot(h_clctEmulSummary_eff, ps, c1);
0223   }
0224 
0225   ps->Close();
0226   // close the DQM file
0227   theFile->Close();
0228   delete c1;
0229   delete ps;
0230 }
0231 
0232 void CSCTriggerPrimitivesAnalyzer::makePlot(TH1F *dataMon,
0233                                             TH1F *emulMon,
0234                                             TH1F *diffMon,
0235                                             TString lcts,
0236                                             TString lct,
0237                                             TString var,
0238                                             TString chamber,
0239                                             TPostScript *ps,
0240                                             TCanvas *c1) const {
0241   ps->NewPage();
0242 
0243   TString runTitle = "(CMS Run " + std::to_string(runNumber_) + ")";
0244   if (useB904_)
0245     runTitle = "(B904 Cosmic Run " + TString(B904RunNumber_) + ")";
0246   const TString title(chamber + " " + lcts + " " + var + " " + runTitle);
0247   c1->cd(1);
0248   gPad->SetGridx();
0249   gPad->SetGridy();
0250   gStyle->SetOptStat(1111);
0251   dataMon->SetTitle(title);
0252   dataMon->GetXaxis()->SetTitle(lcts + " " + var);
0253   dataMon->GetYaxis()->SetTitle("Entries");
0254   dataMon->SetMarkerColor(kBlack);
0255   dataMon->SetMarkerStyle(kPlus);
0256   dataMon->SetMarkerSize(3);
0257   // add 50% to make sure the legend does not overlap with the histograms
0258   dataMon->SetMaximum(dataMon->GetBinContent(dataMon->GetMaximumBin()) * 1.6);
0259   dataMon->Draw("histp");
0260   dataMon->GetXaxis()->SetLabelSize(0.05);
0261   dataMon->GetYaxis()->SetLabelSize(0.05);
0262   dataMon->GetXaxis()->SetTitleSize(0.05);
0263   dataMon->GetYaxis()->SetTitleSize(0.05);
0264   emulMon->SetLineColor(kRed);
0265   emulMon->Draw("histsame");
0266   auto legend = new TLegend(0.6, 0.7, 0.9, 0.9);
0267   legend->AddEntry(dataMon, TString("Data (" + std::to_string((int)dataMon->GetEntries()) + ")"), "p");
0268   legend->AddEntry(emulMon, TString("Emulator (" + std::to_string((int)emulMon->GetEntries()) + ")"), "l");
0269   legend->Draw();
0270 
0271   c1->cd(2);
0272   gPad->SetGridx();
0273   gPad->SetGridy();
0274   gStyle->SetOptStat(0);
0275   diffMon->SetLineColor(kBlack);
0276   diffMon->SetTitle(title);
0277   diffMon->GetXaxis()->SetTitle(lcts + " " + var);
0278   diffMon->GetYaxis()->SetTitle("Emul - Data");
0279   diffMon->GetXaxis()->SetLabelSize(0.05);
0280   diffMon->GetYaxis()->SetLabelSize(0.05);
0281   diffMon->GetXaxis()->SetTitleSize(0.05);
0282   diffMon->GetYaxis()->SetTitleSize(0.05);
0283   diffMon->Draw("ep");
0284   c1->Update();
0285 }
0286 
0287 void CSCTriggerPrimitivesAnalyzer::make2DPlot(TH2F *effMon, TPostScript *ps, TCanvas *c1) const {
0288   ps->NewPage();
0289 
0290   TString runTitle = "(CMS Run " + std::to_string(runNumber_) + ")";
0291   if (useB904_)
0292     runTitle = "(B904 Cosmic Run " + TString(B904RunNumber_) + ")";
0293   gStyle->SetOptStat(0);
0294   effMon->SetTitle(effMon->GetTitle() + runTitle);
0295   effMon->Draw("colz");
0296   c1->Update();
0297 }
0298 
0299 DEFINE_FWK_MODULE(CSCTriggerPrimitivesAnalyzer);