Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-11-26 02:34:21

0001 #!/usr/bin/env python
0002 #
0003 # Launch the script with the command: ./compareDQM.py
0004 # Set below the two DQM input files (DQMfileOld,DQMfileNew)
0005 #
0006 # This script compares the plots cointained in two DQM files and save the superimposed plots 
0007 #
0008 
0009 DQMfileOld="/afs/cern.ch/user/s/sdonato/AFSwork/public/DQM_V0001_R000000001__CMSSW_X_Y_Z__RelVal__TrigVal.root"
0010 DQMfileNew="/afs/cern.ch/user/s/sdonato/AFSwork/public/DQM_V0001_R000000002__CMSSW_X_Y_Z__RelVal__TrigVal.root"
0011 labelNew = "New"
0012 labelOld = "Old"
0013 
0014 ########################## load libraries #################################
0015 
0016 import os, string, re, sys, math
0017 
0018 try:
0019         import ROOT
0020 except:
0021         print("\nCannot load PYROOT, make sure you have setup ROOT in the path")
0022         print("and pyroot library is also defined in the variable PYTHONPATH, try:\n")
0023         if (os.getenv("PYTHONPATH")):
0024             print(" setenv PYTHONPATH ${PYTHONPATH}:$ROOTSYS/lib\n")
0025         else:
0026             print(" setenv PYTHONPATH $ROOTSYS/lib\n")
0027         sys.exit()
0028 
0029 folder="plots"
0030 try:
0031     os.mkdir(folder)
0032 except:
0033     print("folder " + folder + " already exist")
0034 
0035 from ROOT import TFile
0036 from ROOT import TCanvas
0037 from ROOT import TLegend
0038 from ROOT import TH1F
0039 from ROOT import TGraphErrors
0040 
0041 ########################## define a function that return plots given a TFile #################################
0042 
0043 def GetPlots(_file0):
0044 #   _file0=TFile(filename)
0045     dir1 = _file0.Get("DQMData")
0046     dir2 = dir1.Get("Run 1")
0047     dir3 = dir2.Get("HLT")
0048     dir4 = dir3.Get("Run summary")
0049     plots=[]
0050     for type in dir4.GetListOfKeys():
0051         dirType= dir4.Get(type.GetName())
0052         for triggerKey in dirType.GetListOfKeys():
0053             triggerDir=dirType.Get(triggerKey.GetName())
0054             for plotKey in triggerDir.GetListOfKeys():
0055                 plotPointer=triggerDir.Get(plotKey.GetName())
0056                 plot=plotPointer
0057                 if(plot.GetName()=="efficiency"): 
0058                     for plotEfficiencyKey in plotPointer.GetListOfKeys():
0059                         plot=plotPointer.Get(plotEfficiencyKey.GetName())
0060                         plots=plots+[plot.Clone(triggerKey.GetName() + "_" + plot.GetName())]
0061                 else:
0062                     plots=plots+[plot.Clone(triggerKey.GetName() + "_" + plot.GetName())]
0063     
0064     return plots
0065 
0066 ########################## read DQM plots #################################
0067 fileNew=TFile(DQMfileOld)
0068 
0069 plotsNew=0
0070 plotsOld=0
0071 
0072 try:
0073   plotsNew = GetPlots(fileNew)
0074 except:
0075   print("Problem with ", fileNew)
0076 
0077 fileOld=TFile(DQMfileNew)
0078 
0079 try:
0080   plotsOld = GetPlots(fileOld)
0081 except:
0082   print("Problem with ", fileOld)
0083 
0084 ##### for kind of plots save a .png superimposing the New with the Old #####
0085 
0086 ROOT.gROOT.SetBatch()
0087 ROOT.gStyle.SetOptStat(0)
0088 c1 = TCanvas("c1","",1280,720)
0089 c1.SetGridx()
0090 c1.SetGridy()
0091 legend = TLegend(0.07,0.85,0.2,0.93);
0092 
0093 first=True
0094 for plotNew in plotsNew:
0095     for plotOld in plotsOld:
0096         if(plotNew.GetName()==plotOld.GetName()):
0097             plotOld.SetLineColor(4)
0098             plotOld.SetMarkerColor(4)
0099 #           plotOld.SetFillColor(4)
0100             plotNew.SetLineColor(2)
0101             plotNew.SetMarkerColor(2)
0102 #           plotNew.SetFillColor(2)
0103 #           plotNew.SetFillStyle(3002)
0104             
0105             plotNew.SetLineWidth(2)
0106             plotOld.SetLineWidth(2)
0107             if first:
0108                 legend.AddEntry(plotNew,labelNew,"l");
0109                 legend.AddEntry(plotOld,labelOld,"l");
0110             
0111             if plotNew.GetName().rfind("mistagrate)")>0:
0112                 plotOld.SetMinimum(0.001)
0113                 plotNew.SetMinimum(0.001)
0114                 c1.SetLogy(1)
0115             else:
0116                 c1.SetLogy(0)
0117             
0118             plotOld.SetMaximum(1.05*max(plotOld.GetMaximum(),plotNew.GetMaximum(),1))
0119             plotOld.Draw()
0120             plotNew.Draw("same")
0121             legend.Draw()
0122             c1.SaveAs(folder+"/"+plotNew.GetName()+".png")
0123             first=False
0124