CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/CondTools/Ecal/python/EcalCondTools.py

Go to the documentation of this file.
00001 #
00002 # Misc functions to manipulate Ecal records
00003 # author: Stefano Argiro
00004 # id: $Id: EcalCondTools.py,v 1.13 2011/10/10 09:02:27 fay Exp $
00005 #
00006 #
00007 # WARNING: we assume that the list of iovs for a given tag
00008 #          contains one element only, the case of several elements
00009 #          will need to be addressed
00010 
00011 #from pluginCondDBPyInterface import *
00012 from CondCore.Utilities import iovInspector as inspect
00013 from ROOT import TCanvas,TH1F, TH2F, gStyle, TChain, TTree, TLegend, TFile
00014 import EcalPyUtils
00015 import sys
00016 from math import sqrt
00017 
00018 def listTags(db):
00019     '''List all available tags for a given db '''
00020     try:
00021         db.startTransaction()
00022         tags = db.allTags()
00023         db.commitTransaction()
00024         for tag in tags.split():
00025             print tag
00026     except:
00027         return ""
00028 
00029 def listIovs(db,tag):
00030     '''List all available iovs for a given tag'''
00031 
00032     try :
00033        iov = inspect.Iov(db,tag)
00034        iovlist = iov.list()
00035        print "Available iovs for tag: ",tag
00036        for p in iovlist:
00037            print "  Since " , p[1], " Till " , p[2]
00038      
00039     except Exception,er :
00040         print " listIovs exception ",er 
00041 
00042 def dumpXML(db,tag,since,filename='dump.xml'):
00043     '''Dump record in XML format for a given tag '''
00044     try :
00045        iov = inspect.Iov(db,tag)
00046        db.startTransaction()
00047        Plug = __import__(str(db.payloadModules(tag)[0]))
00048        payload = Plug.Object(db)
00049        listOfIovElem= [iovElem for iovElem in db.iov(tag).elements]
00050        inst = 0
00051        for elem in db.iov(tag).elements :
00052            inst = inst + 1
00053            if str(elem.since())==str(since):
00054                found = inst
00055                break
00056        db.commitTransaction()
00057        payload = inspect.PayLoad(db, tag, elem)
00058        out = open(filename,'w')
00059        print >> out, payload
00060       
00061     except Exception,er :
00062         print " dumpXML exception ",er
00063 
00064 def plot (db, tag,since,filename='plot.root'):
00065     '''Invoke the plot function from the wrapper and save to the specified \
00066        file. The file format will reflect the extension given.'''
00067    
00068     try :
00069        iov = inspect.Iov(db,tag)
00070        db.startTransaction()
00071        Plug = __import__(str(db.payloadModules(tag)[0]))
00072        payload = Plug.Object(db)
00073        listOfIovElem= [iovElem for iovElem in db.iov(tag).elements]
00074        inst = 0
00075        for elem in db.iov(tag).elements :
00076            inst = inst + 1
00077            if str(elem.since())==str(since):
00078                found = inst
00079                break
00080        db.commitTransaction()
00081        payload = inspect.PayLoad(db, tag, elem)
00082        payload.plot(filename,"",[],[])
00083             
00084     except Exception,er :
00085         print " plot exception ",er
00086         
00087 
00088 def compare(tag1,db1,since1,
00089             tag2,db2,since2,filename='compare.root'):
00090   '''Produce comparison plots for two records. Save plots to file \
00091      according to format. tag can be an xml file'''
00092   print "EcalCondTools.py compare tag1 ", tag1, "since1 : ", since1, " tag2 ", tag2," s2 ", since2
00093 
00094   coeff_1_b=[]
00095   coeff_2_b=[]
00096 
00097   coeff_1_e=[]
00098   coeff_2_e=[]
00099   
00100   if  tag1.find(".xml") < 0:
00101       found=0
00102       try:
00103         db1.startTransaction()
00104         Plug = __import__(str(db1.payloadModules(tag1)[0]))
00105         payload = Plug.Object(db1)
00106         listOfIovElem= [iovElem for iovElem in db1.iov(tag1).elements]
00107         what = {'how':'barrel'}
00108         w = inspect.setWhat(Plug.What(),what)
00109         exb = Plug.Extractor(w)
00110         what = {'how':'endcap'}
00111         w = inspect.setWhat(Plug.What(),what)
00112         exe = Plug.Extractor(w)
00113 #        p = getObject(db1,tag1,since1,ex)
00114 #        p.extract(ex)
00115         print " before loop"
00116         for elem in db1.iov(tag1).elements :       
00117           if str(elem.since())==str(since1):
00118             found=1
00119             payload.load(elem)
00120             payload.extract(exb)
00121             coeff_1_b = [i for i in exb.values()]# first set of coefficients
00122             payload.extract(exe)
00123             coeff_1_e = [i for i in exe.values()]
00124         db1.commitTransaction()
00125 
00126       except Exception,er :
00127         print " compare first set exception ",er
00128       if not found :
00129         print "Could not retrieve payload for tag: " , tag1, " since: ", since1
00130         sys.exit(0)
00131 
00132   else:
00133       coeff_1_b,coeff_1_e = EcalPyUtils.fromXML(tag1)
00134 
00135   if  tag2.find(".xml")<0:
00136       found=0
00137       try:  
00138         db2.startTransaction()
00139         Plug = __import__(str(db2.payloadModules(tag2)[0]))
00140         what = {'how':'barrel'}
00141         w = inspect.setWhat(Plug.What(),what)
00142         exb = Plug.Extractor(w)
00143         what = {'how':'endcap'}
00144         w = inspect.setWhat(Plug.What(),what)
00145         exe = Plug.Extractor(w)
00146 #        p = getObject(db2,tag2,since2)
00147 #        p.extract(ex)
00148         for elem in db2.iov(tag2).elements :       
00149           if str(elem.since())==str(since2):
00150             found=1
00151             payload.load(elem)
00152             payload.extract(exb)
00153             coeff_2_b = [i for i in exb.values()]# second set of coefficients
00154             payload.extract(exe)
00155             coeff_2_e = [i for i in exe.values()]
00156         db2.commitTransaction()
00157      
00158       except Exception, er :
00159           print " compare second set exception ",er
00160       if not found :
00161         print "Could not retrieve payload for tag: " , tag2, " since: ", since2
00162         sys.exit(0)
00163 
00164   else:
00165       coeff_2_b,coeff_2_e = EcalPyUtils.fromXML(tag2)
00166 
00167   gStyle.SetPalette(1)    
00168 
00169   savefile = TFile(filename,"RECREATE")
00170 
00171   ebhisto,ebmap, profx, profy= compareBarrel(coeff_1_b,coeff_2_b)
00172   eephisto,eepmap,eemhisto,eemmap=compareEndcap(coeff_1_e,coeff_2_e)
00173 
00174 #make more canvas (suppressed : use a root file)
00175 
00176 #  cEBdiff = TCanvas("EBdiff","EBdiff")
00177 #  cEBdiff.Divide(2,2)
00178 
00179 #  cEBdiff.cd(1)
00180 #  ebhisto.Draw()
00181 #  cEBdiff.cd(2)
00182 #  ebmap.Draw("colz")
00183 #  cEBdiff.cd(3)
00184 #  profx.Draw()
00185 #  cEBdiff.cd(4)
00186 #  profy.Draw()
00187 
00188 #  EBfilename = "EB_"+filename
00189 #  cEBdiff.SaveAs(EBfilename)
00190 
00191 #  cEEdiff = TCanvas("EEdiff","EEdiff")
00192 #  cEEdiff.Divide(2,2)
00193   
00194 #  cEEdiff.cd(1)
00195 #  eephisto.Draw()
00196 #  cEEdiff.cd(2)
00197 #  eepmap.Draw("colz")
00198 #  cEEdiff.cd(3)
00199 #  eemhisto.Draw()
00200 #  cEEdiff.cd(4)
00201 #  eemmap.Draw("colz")
00202 
00203 #  EEfilename = "EE_"+filename
00204 #  cEEdiff.SaveAs(EEfilename)
00205 
00206 
00207   eeborderphisto,eeborderpmap,eebordermhisto,eebordermmap=compareEndcapBorder(coeff_1_e,coeff_2_e)
00208   ebborderhisto,ebbordermap = compareBarrelBorder(coeff_1_b,coeff_2_b)
00209 
00210   border_diff_distro_can = TCanvas("border_difference","borders difference")
00211   border_diff_distro_can.Divide(2,3)
00212 
00213   border_diff_distro_can.cd(1)
00214   ebborderhisto.Draw()
00215   border_diff_distro_can.cd(2)
00216   ebbordermap.Draw("colz")
00217   border_diff_distro_can.cd(3)
00218   eeborderphisto.Draw()
00219   border_diff_distro_can.cd(4)
00220   eeborderpmap.Draw("colz")
00221   border_diff_distro_can.cd(5)
00222   eebordermhisto.Draw()
00223   border_diff_distro_can.cd(6)
00224   eebordermmap.Draw("colz")
00225 
00226   bordersfilename = "borders_"+filename
00227   prof_filename = "profiles_"+filename
00228   
00229 #  border_diff_distro_can.SaveAs(bordersfilename)
00230 
00231   savefile.Write()
00232 
00233 
00234 
00235 def histo (db, tag,since,filename='histo.root'):
00236     '''Make histograms and save to file. tag can be an xml file'''
00237     
00238     coeff_barl=[]
00239     coeff_endc=[]
00240     
00241     if  tag.find(".xml")< 0:
00242       found=0
00243       try:  
00244 #          exec('import '+db.moduleName(tag)+' as Plug')
00245         db.startTransaction()
00246         Plug = __import__(str(db.payloadModules(tag)[0]))
00247         payload = Plug.Object(db)
00248         listOfIovElem= [iovElem for iovElem in db.iov(tag).elements]
00249         what = {'how':'barrel'}
00250         w = inspect.setWhat(Plug.What(),what)
00251         exb = Plug.Extractor(w)
00252         what = {'how':'endcap'}
00253         w = inspect.setWhat(Plug.What(),what)
00254         exe = Plug.Extractor(w)
00255         for elem in db.iov(tag).elements :       
00256           if str(elem.since())==str(since):
00257             found=1
00258             payload.load(elem)
00259             payload.extract(exb)
00260             coeff_barl = [i for i in exb.values()]
00261             payload.extract(exe)
00262             coeff_endc = [i for i in exe.values()]
00263         db.commitTransaction()
00264 
00265       except Exception, er :
00266           print " histo exception ",er
00267       if not found :
00268         print "Could not retrieve payload for tag: " , tag, " since: ", since
00269         sys.exit(0)
00270 
00271     else :
00272       coeff_barl,coeff_endc=EcalPyUtils.fromXML(tag)
00273 
00274     savefile = TFile(filename,"RECREATE")
00275 
00276     ebmap, ebeta, ebphi, eePmap, ebdist, eeMmap, prof_eePL, prof_eePR, prof_eeML, prof_eeMR, ebBorderdist = makedist(coeff_barl, coeff_endc)
00277 
00278     gStyle.SetPalette(1)
00279 
00280     c =  TCanvas("CCdist")
00281     c.Divide(2,2)
00282 
00283     c.cd(1)  
00284     ebmap.Draw("colz")
00285     c.cd(2)
00286     eePmap.Draw("colz")
00287     c.cd(3)
00288     ebdist.Draw()
00289     c.cd(4)
00290     eeMmap.Draw("colz")
00291 
00292 #    c.SaveAs(filename)
00293 
00294     prof_eb_eta = ebeta.ProfileX()
00295     prof_eb_phi = ebphi.ProfileX()
00296 
00297     c2 = TCanvas("CCprofiles")
00298     c2.Divide(2,2)
00299 
00300     leg = TLegend(0.1,0.7,0.48,0.9)
00301 
00302     c2.cd(1)
00303     prof_eb_eta.Draw()
00304     c2.cd(2)
00305     prof_eb_phi.Draw()
00306     c2.cd(3)
00307     prof_eePL.SetMarkerStyle(8)
00308     prof_eePL.Draw()
00309     prof_eePR.SetMarkerStyle(8)
00310     prof_eePR.SetMarkerColor(2)
00311     prof_eePR.Draw("same")
00312     prof_eeML.SetMarkerStyle(8)
00313     prof_eeML.SetMarkerColor(3)
00314     prof_eeML.Draw("same")
00315     prof_eeMR.SetMarkerStyle(8)
00316     prof_eeMR.SetMarkerColor(5)
00317     prof_eeMR.Draw("same")
00318     leg.AddEntry(prof_eePL,"Dee1","lp")
00319     leg.AddEntry(prof_eePR,"Dee2","lp")
00320     leg.AddEntry(prof_eeMR,"Dee3","lp")
00321     leg.AddEntry(prof_eeML,"Dee4","lp")
00322     leg.Draw()
00323     c2.cd(4)
00324     ebBorderdist.Draw()
00325 
00326     extrafilename = "profiles_"+filename
00327  #   c2.SaveAs(extrafilename)
00328 
00329     savefile.Write()      
00330 
00331 
00332 def getToken(db,tag,since):
00333     ''' Return payload token for a given iov, tag, db'''
00334     try :
00335        iov = inspect.Iov(db,tag)
00336        iovlist = iov.list()
00337        for p in iovlist:
00338            tmpsince=p[1]
00339            if str(tmpsince)==str(since) :
00340                return p[0]
00341        print "Could not retrieve token for tag: " , tag, " since: ", since
00342        sys.exit(0)
00343        
00344     except Exception, er :
00345        print er
00346 
00347 
00348 def getObject(db,tag,since):
00349     ''' Return payload object for a given iov, tag, db'''
00350     found=0
00351     try:
00352 #       exec('import '+db.moduleName(tag)+' as Plug')  
00353        db.startReadOnlyTransaction()
00354        Plug = __import__(str(db.payloadModules(tag)[0]))
00355        print " getObject Plug"
00356        payload = Plug.Object(db)
00357        db.commitTransaction()
00358        listOfIovElem= [iovElem for iovElem in db.iov(tag).elements]
00359        print " getObject before loop"
00360        for elem in db.iov(tag).elements :       
00361            if str(elem.since())==str(since):
00362                found=1
00363                print " getObject found ", elem.since()
00364 #               return Plug.Object(elem)
00365                return elem
00366            
00367     except Exception, er :
00368         print " getObject exception ",er
00369 
00370     if not found :
00371         print "Could not retrieve payload for tag: " , tag, " since: ", since
00372         sys.exit(0)
00373 
00374 
00375 def makedist(coeff_barl, coeff_endc) :
00376 
00377     ebmap = TH2F("EB","EB",360,1,261,171, -85,86)
00378     eePmap = TH2F("EE","EE",100, 1,101,100,1,101)
00379     eeMmap = TH2F("EEminus","EEminus",100,1,101,100,1,101)
00380     ebdist = TH1F("EBdist","EBdist",100,-2,2)
00381     ebBorderdist = TH1F("EBBorderdist","EBBorderdist",100,-2,2)
00382 
00383     ebeta = TH2F("ebeta","ebeta",171,-85,86,100,-2,2)
00384     ebphi = TH2F("ebphi","ebphi",360,1,361,100,-2,2)
00385 
00386     eePL = TH2F("EEPL","EEPlus Left",50,10,55,100,-2,2)
00387     eePR = TH2F("EEPR","EEPlus Right",50,10,55,100,-2,2)
00388     eeML = TH2F("EEML","EEMinus Left",50,10,55,100,-2,2)
00389     eeMR = TH2F("EEMR","EEMinus Right",50,10,55,100,-2,2)
00390     
00391     for i,c in enumerate(coeff_barl):
00392         ieta,iphi = EcalPyUtils.unhashEBIndex(i)
00393         ebmap.Fill(iphi,ieta,c)
00394         ebdist.Fill(c)
00395         ebeta.Fill(ieta,c)
00396         ebphi.Fill(iphi,c)
00397 
00398         if (abs(ieta)==85 or abs(ieta)==65 or abs(ieta)==64 or abs(ieta)==45 or abs(ieta)==44 or abs(ieta)==25 or abs(ieta)==24 or abs(ieta)==1 or iphi%20==1 or iphi%20==0):
00399             ebBorderdist.Fill(c)
00400 
00401 
00402     for i,c in enumerate(coeff_endc):
00403         ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
00404         R = sqrt((ix-50)*(ix-50)+(iy-50)*(iy-50))
00405 
00406         if  iz>0:
00407             eePmap.Fill(ix,iy,c)
00408             if ix<50:
00409                 eePL.Fill(R,c,1)
00410             if ix>50:
00411                 eePR.Fill(R,c,1)
00412 
00413         if iz<0:
00414             eeMmap.Fill(ix,iy,c)
00415             if ix<50:
00416                 eeML.Fill(R,c,1)
00417             if ix>50:
00418                 eeMR.Fill(R,c,1)
00419 
00420     prof_eePL = eePL.ProfileX()
00421     prof_eePR = eePR.ProfileX()
00422     prof_eeML = eeML.ProfileX()
00423     prof_eeMR = eeMR.ProfileX()
00424     
00425     return ebmap, ebeta, ebphi, eePmap, ebdist, eeMmap, prof_eePL, prof_eePR, prof_eeML, prof_eeMR, ebBorderdist
00426 
00427 def compareBarrel(coeff_barl_1,coeff_barl_2) :
00428   '''Return an histogram and a map of the differences '''
00429 
00430   diff_distro_h   = TH1F("diffh","diffh",100,-2,2)
00431   diff_distro_m   = TH2F("diffm","diffm",360,1,361,171,-85,86)
00432   diff_distro_m.SetStats(0)
00433   ebeta = TH2F("ebeta","ebeta",171,-85,86,100,-2,2)
00434   ebphi = TH2F("ebphi","ebphi",360,1,361,100,-2,2)
00435 
00436   
00437   for i,c in enumerate(coeff_barl_1):  
00438       diff = c - coeff_barl_2[i]      
00439       ieta,iphi= EcalPyUtils.unhashEBIndex(i)
00440       diff_distro_h.Fill(diff) 
00441       diff_distro_m.Fill(iphi,ieta,diff)
00442       ebeta.Fill(ieta,diff)
00443       ebphi.Fill(iphi,diff)
00444 
00445   prof_x_h = ebeta.ProfileX()
00446   prof_y_h = ebphi.ProfileX()
00447           
00448   return diff_distro_h, diff_distro_m, prof_x_h, prof_y_h
00449 
00450 
00451 
00452 def compareBarrelBorder(coeff_barl_1,coeff_barl_2) :
00453   '''Return an histogram and a map of the differences '''
00454 
00455   diff_distro_border_h   = TH1F("diffborderh","diffh",100,-2,2)
00456   diff_distro_border_m   = TH2F("diffborderm","diffm",360,1,361,171,-85,86)
00457   diff_distro_border_m.SetStats(0)
00458   
00459   for i,c in enumerate(coeff_barl_1):  
00460       diff = c - coeff_barl_2[i]      
00461       ieta,iphi= EcalPyUtils.unhashEBIndex(i)
00462       if (abs(ieta)==85 or abs(ieta)==65 or abs(ieta)==64 or abs(ieta)==45 or abs(ieta)==44 or abs(ieta)==25 or abs(ieta)==24 or abs(ieta)==1 or iphi%20==1 or iphi%20==0):
00463           diff_distro_border_h.Fill(diff) 
00464       if (abs(ieta)==85 or abs(ieta)==65 or abs(ieta)==64 or abs(ieta)==45 or abs(ieta)==44 or abs(ieta)==25 or abs(ieta)==24 or abs(ieta)==1 or iphi%20==0 or iphi%20==1): 
00465           diff_distro_border_m.Fill(iphi,ieta,diff)
00466           
00467   return diff_distro_border_h, diff_distro_border_m 
00468 
00469 
00470 
00471 
00472     
00473 def compareEndcap(coeff_endc_1, coeff_endc_2) :
00474     ''' Return an histogram and a map of the differences for each endcap'''
00475 
00476     diff_distro_h_eep   = TH1F("diff EE+","diff EE+",100,-2,2)
00477     diff_distro_h_eem   = TH1F("diff EE-","diff EE-",100,-2,2)
00478 
00479     
00480     diff_distro_m_eep   = TH2F("map EE+","map EE+",100,1,101,100,1,101)
00481     diff_distro_m_eem   = TH2F("map EE-","map EE-",100,1,101,100,1,101)
00482 
00483     temp_h = TH1F("tempR","tempR",50,0,50)
00484     
00485     diff_distro_m_eep.SetStats(0)
00486     diff_distro_m_eem.SetStats(0)
00487 
00488 
00489     for i,c in enumerate(coeff_endc_1):  
00490       diff = c - coeff_endc_2[i]
00491       ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
00492       R = sqrt((ix-50)*(ix-50)+(iy-50)*(iy-50))
00493       
00494       if iz >0:
00495           diff_distro_h_eep.Fill(diff)
00496           diff_distro_m_eep.Fill(ix,iy,diff)
00497           
00498       else:
00499           diff_distro_h_eem.Fill(diff)
00500           diff_distro_m_eem.Fill(ix,iy,diff)
00501 
00502     return diff_distro_h_eep, \
00503            diff_distro_m_eep, \
00504            diff_distro_h_eem, \
00505            diff_distro_m_eem
00506 
00507 
00508 
00509 def compareEndcapBorder(coeff_endc_1, coeff_endc_2) :
00510     ''' Return an histogram and a map of the differences for each endcap'''
00511 
00512     border_diff_distro_h_eep   = TH1F("borderdiff EE+","diff EE+",100,-2,2)
00513     border_diff_distro_h_eem   = TH1F("borderdiff EE-","diff EE-",100,-2,2)
00514 
00515     
00516     border_diff_distro_m_eep   = TH2F("bordermap EE+","map EE+",100,1,101,100,1,101)
00517     border_diff_distro_m_eem   = TH2F("bordermap EE-","map EE-",100,1,101,100,1,101)
00518     
00519     border_diff_distro_m_eep.SetStats(0)
00520     border_diff_distro_m_eem.SetStats(0)
00521 
00522 
00523     for i,c in enumerate(coeff_endc_1):  
00524       diff = c - coeff_endc_2[i]
00525       ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
00526       Rsq = ((ix-50.0)**2+(iy-50.0)**2)
00527       
00528       if (iz >0 and (Rsq<144.0 or Rsq>2500.0)):
00529           border_diff_distro_h_eep.Fill(diff)
00530           border_diff_distro_m_eep.Fill(ix,iy,diff)
00531       elif (iz<0 and (Rsq<144.0 or Rsq>2500.0)):
00532           border_diff_distro_h_eem.Fill(diff)
00533           border_diff_distro_m_eem.Fill(ix,iy,diff)
00534       
00535 
00536     return border_diff_distro_h_eep, \
00537            border_diff_distro_m_eep, \
00538            border_diff_distro_h_eem, \
00539            border_diff_distro_m_eem