CMS 3D CMS Logo

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