Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:33:21

0001 #include <iostream>
0002 #include "TCanvas.h"
0003 #include "TDirectoryFile.h"
0004 #include "TFile.h"
0005 #include "TH1F.h"
0006 #include "TH2F.h"
0007 #include "TIterator.h"
0008 #include "TKey.h"
0009 #include "TLegend.h"
0010 #include "TList.h"
0011 #include "TObjString.h"
0012 #include "TPaveStats.h"
0013 #include "TPRegexp.h"
0014 #include "TRegexp.h"
0015 #include "TROOT.h"
0016 #include "TString.h"
0017 #include "TStyle.h"
0018 
0019 void muonIdVal(char* filename1, char* filename2 = 0, bool make2DPlots = true, bool printPng = true, bool printHtml = true, bool printEps = false) {
0020    TFile* f1 = TFile::Open(filename1);
0021    if (! f1) {
0022       std::cout << "Error: unable to open " << filename1 << std::endl;
0023       return;
0024    }
0025    TDirectoryFile* d1 = (TDirectoryFile*)f1->FindObjectAny("MuonIdVal");
0026    if (! d1)
0027       if (f1->cd("/DQMData/Run 1/MuonIdentificationV/Run summary"))
0028          d1 = (TDirectoryFile*)gDirectory;
0029    if (! d1)
0030       if (f1->cd("/DQMData/Run 1/Muons/Run summary/MuonIdentificationV"))
0031          d1 = (TDirectoryFile*)gDirectory;
0032    if (! d1) {
0033       std::cout << "Error: MuonIdVal/MuonIdentificationV not found in " << filename1 << std::endl;
0034       return;
0035    }
0036 
0037    TFile* f2 = 0;
0038    TDirectoryFile* d2 = 0;
0039    if (filename2) {
0040       f2 = TFile::Open(filename2);
0041       if (! f2) {
0042          std::cout << "Error: unable to open " << filename2 << std::endl;
0043          return;
0044       }
0045       d2 = (TDirectoryFile*)f2->FindObjectAny("MuonIdVal");
0046       if (! d2)
0047          if (f2->cd("/DQMData/Run 1/MuonIdentificationV/Run summary"))
0048             d2 = (TDirectoryFile*)gDirectory;
0049       if (! d2)
0050          if (f2->cd("/DQMData/Run 1/Muons/Run summary/MuonIdentificationV"))
0051             d2 = (TDirectoryFile*)gDirectory;
0052       if (! d2) {
0053          std::cout << "Error: MuonIdVal/MuonIdentificationV not found in " << filename2 << std::endl;
0054          return;
0055       }
0056    }
0057 
0058    TList* list = 0;
0059    TIterator* iter = 0;
0060    TKey* key = 0;
0061    TObject* obj1 = 0;
0062    TObject* obj2 = 0;
0063 
0064    gROOT->ForceStyle();
0065    gROOT->SetStyle("Plain");
0066    gStyle->SetOptStat(111111);
0067    gStyle->SetOptFit(1);
0068 
0069    // Retrieve CMSSW versions for TLegend
0070    TString version1 = ((TObjString*)f1->GetListOfKeys()->At(0))->GetString().Remove(TString::kLeading, '"').Remove(TString::kTrailing, '"');
0071    TString version2 = "";
0072    if (f2)
0073       version2 = ((TObjString*)f2->GetListOfKeys()->At(0))->GetString().Remove(TString::kLeading, '"').Remove(TString::kTrailing, '"');
0074    //std::cout << "version1: " << version1.Data() << std::endl;
0075    //std::cout << "version2: " << version2.Data() << std::endl;
0076 
0077    TDirectoryFile* tmd1 = (TDirectoryFile*)d1->Get("TrackerMuons");
0078    TDirectoryFile* gmd1 = (TDirectoryFile*)d1->Get("GlobalMuons");
0079    TDirectoryFile* tmngmd1 = (TDirectoryFile*)d1->Get("TrackerMuonsNotGlobalMuons");
0080    TDirectoryFile* gmntmd1 = (TDirectoryFile*)d1->Get("GlobalMuonsNotTrackerMuons");
0081    TDirectoryFile* tmd2 = 0;
0082    TDirectoryFile* gmd2 = 0;
0083    TDirectoryFile* tmngmd2 = 0;
0084    TDirectoryFile* gmntmd2 = 0;
0085    if (d2) {
0086       tmd2 = (TDirectoryFile*)d2->Get("TrackerMuons");
0087       gmd2 = (TDirectoryFile*)d2->Get("GlobalMuons");
0088       tmngmd2 = (TDirectoryFile*)d2->Get("TrackerMuonsNotGlobalMuons");
0089       gmntmd2 = (TDirectoryFile*)d2->Get("GlobalMuonsNotGlobalMuons");
0090    }
0091 
0092    TCanvas* c1 = new TCanvas("c1");
0093    c1->Draw();
0094    TPaveStats* s1 = 0;
0095    TPaveStats* s2 = 0;
0096    TLegend* leg = 0;
0097    TRegexp re("*Pull[dxy]*", kTRUE);
0098 
0099    // Access histograms in five different locations:
0100    //   d1    -> MuonIdVal
0101    // tmd1    -> MuonIdVal/TrackerMuons
0102    // gmd1    -> MuonIdVal/GlobalMuons
0103    // tmngmd1 -> MuonIdVal/TrackerMuonsNotGlobalMuons
0104    // gmntmd1 -> MuonIdVal/GlobalMuonsNotTrackerMuons
0105    char pfx[5][7] = {"", "tm_", "gm_", "tmngm_", "gmntm_"};
0106 
0107    for(unsigned int i = 0; i < 5; i++) {
0108       if (i == 0) {}
0109       else if (i == 1 && tmd1) { d1 = tmd1; d2 = tmd2; }
0110       else if (i == 2 && gmd1) { d1 = gmd1; d2 = gmd2; }
0111       else if (i == 3 && tmngmd1) { d1 = tmngmd1; d2 = tmngmd2; }
0112       else if (i == 4 && gmntmd1) { d1 = gmntmd1; d2 = gmntmd2; }
0113       else continue;
0114 
0115       list = d1->GetListOfKeys();
0116       iter = list->MakeIterator();
0117       while((key = (TKey*)iter->Next())) {
0118          obj1 = key->ReadObj();
0119          obj2 = 0;
0120          s2   = 0;
0121          if (d2) {
0122             obj2 = d2->Get(obj1->GetName());
0123             // For backwards compatibility with old dqm files where histograms may be
0124             // stored in different directories, try FindObjectAny before giving up
0125             // However, only do this for histograms located in the "base" directory
0126             // of the first file, which is the only place to which I have been relocating
0127             // histograms
0128             if (! obj2 && i == 0)
0129                obj2 = f2->FindObjectAny(obj1->GetName());
0130          }
0131 
0132          if (obj1->InheritsFrom(TH1::Class()) || (obj1->InheritsFrom(TH2::Class()) && make2DPlots)) {
0133             ((TH1*)obj1)->Sumw2();
0134             if (obj2) ((TH1*)obj2)->Sumw2();
0135             // If there are two TH1Fs better normalize them
0136             // If just one don't bother
0137             if (obj2 && obj1->InheritsFrom(TH1F::Class())) {
0138                ((TH1F*)obj1)->Scale(1./((TH1F*)obj1)->Integral());
0139                ((TH1F*)obj2)->Scale(1./((TH1F*)obj2)->Integral());
0140             }
0141 
0142             if ((TString(obj1->GetName()).Index(re) >= 0)) {
0143                ((TH1F*)obj1)->Fit("gaus", "", "", -2., 2.);
0144                if (obj2)
0145                   ((TH1F*)obj2)->Fit("gaus", "", "", -2., 2.);
0146             }
0147 
0148             ((TH1*)obj1)->SetLineColor(4);
0149             ((TH1*)obj1)->SetMarkerColor(4);
0150             ((TH1*)obj1)->Draw("hist");
0151             ((TH1*)obj1)->Draw("funcsame");
0152             c1->Update();
0153             s1 = (TPaveStats*)obj1->FindObject("stats");
0154             s1->SetTextColor(4);
0155             s1->SetFillStyle(0);
0156 
0157             if (obj2) {
0158                ((TH1*)obj2)->SetLineColor(2);
0159                ((TH1*)obj2)->SetMarkerColor(2);
0160                ((TH1*)obj2)->Draw("histsame");
0161                ((TH1*)obj2)->Draw("funcsames");
0162                c1->Update();
0163 
0164                s2 = (TPaveStats*)obj2->FindObject("stats");
0165                s2->SetTextColor(2);
0166                s2->SetFillStyle(0);
0167                double height = s2->GetY2NDC()-s2->GetY1NDC();
0168                s2->SetY2NDC(s1->GetY1NDC()-.005);
0169                s2->SetY1NDC(s2->GetY2NDC()-height);
0170 
0171                // If obj2 has a higher maximum, set obj1 to that
0172                // Again only an issue for TH1F
0173                if (obj2->InheritsFrom(TH1F::Class()))
0174                   if (((TH1F*)obj2)->GetMaximum() > ((TH1F*)obj1)->GetMaximum())
0175                      ((TH1F*)obj1)->SetMaximum(((TH1F*)obj2)->GetMaximum()*1.05);
0176             }
0177 
0178             leg = 0;
0179             leg = new TLegend(0., 0., .22, .06);
0180             leg->SetFillColor(0);
0181             leg->AddEntry(obj1, version1.Data(), "l");
0182             if (obj2)
0183                leg->AddEntry(obj2, version2.Data(), "l");
0184             leg->Draw();
0185             c1->Update();
0186 
0187             // Output png files
0188             if (printPng)
0189                c1->Print(Form("%s%s.png", pfx[i], obj1->GetName()));
0190 
0191             delete leg;
0192          }
0193       }
0194    }
0195 
0196    f1->Close();
0197    if (f2)
0198       f2->Close();
0199 
0200    TPRegexp preg = TPRegexp("(\\S+).root");
0201    TString output = ((TObjString*)preg.MatchS(TString(filename1))->At(1))->GetString();
0202 
0203    // Output html file
0204    if (printHtml) {
0205    }
0206 
0207    // Output eps file
0208    if (printEps) {
0209    }
0210 }