CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EcalCondTools.py
Go to the documentation of this file.
1 #
2 # Misc functions to manipulate Ecal records
3 # author: Stefano Argiro
4 # id: $Id: EcalCondTools.py,v 1.8 2009/07/16 08:25:49 argiro Exp $
5 #
6 #
7 # WARNING: we assume that the list of iovs for a given tag
8 # contains one element only, the case of several elements
9 # will need to be addressed
10 
11 #from pluginCondDBPyInterface import *
12 from CondCore.Utilities import iovInspector as inspect
13 from ROOT import TCanvas,TH1F, TH2F, gStyle, TChain, TTree, TLegend, TFile
14 import EcalPyUtils
15 import sys
16 from math import sqrt
17 
18 def listTags(db):
19  '''List all available tags for a given db '''
20  tags=db.allTags()
21  for tag in tags.split():
22  print tag
23 
24 def listIovs(db,tag):
25  '''List all available iovs for a given tag'''
26 
27  try :
28  iov = inspect.Iov(db,tag)
29  iovlist = iov.list()
30  print "Available iovs for tag: ",tag
31  for p in iovlist:
32  print " Since " , p[1], " Till " , p[2]
33 
34  except Exception,er :
35  print er
36 
37 def dumpXML(db,tag,since,filename='dump.xml'):
38  '''Dump record in XML format for a given tag '''
39  try :
40  iov = inspect.Iov(db,tag)
41  token = getToken(db,tag,since)
42  payload=inspect.PayLoad(db,token)
43  out = open(filename,'w')
44  print >> out, payload
45 
46  except Exception,er :
47  print er
48 
49 def plot (db, tag,since,filename='plot.root'):
50  '''Invoke the plot function from the wrapper and save to the specified \
51  file. The file format will reflect the extension given.'''
52 
53  try :
54  iov = inspect.Iov(db,tag)
55  iovlist = iov.list()
56  token = getToken(db,tag,since)
57  payload=inspect.PayLoad(db,token)
58  payload.plot(filename,"",[],[])
59 
60  except Exception,er :
61  print er
62 
63 
64 def compare(tag1,db1,since1,
65  tag2,db2,since2,filename='compare.root'):
66  '''Produce comparison plots for two records. Save plots to file \
67  according to format. tag can be an xml file'''
68 
69  coeff_1_b=[]
70  coeff_2_b=[]
71 
72  coeff_1_e=[]
73  coeff_2_e=[]
74 
75  if tag1.find(".xml") < 0:
76  try:
77  exec('import '+db1.moduleName(tag1)+' as Plug')
78  what = {'how':'barrel'}
79  w = inspect.setWhat(Plug.What(),what)
80  ex = Plug.Extractor(w)
81  p = getObject(db1,tag1,since1)
82  p.extract(ex)
83  coeff_1_b = [i for i in ex.values()]# first set of coefficients
84  what = {'how':'endcap'}
85  w = inspect.setWhat(Plug.What(),what)
86  ex = Plug.Extractor(w)
87  p.extract(ex)
88  coeff_1_e = [i for i in ex.values()]# first set of coefficients
89 
90 
91  except Exception,er :
92  print er
93  else:
94  coeff_1_b,coeff_1_e = EcalPyUtils.fromXML(tag1)
95 
96  if tag2.find(".xml")<0:
97  try:
98  exec('import '+db2.moduleName(tag2)+' as Plug')
99  what = {'how':'barrel'}
100  w = inspect.setWhat(Plug.What(),what)
101  ex = Plug.Extractor(w)
102  p = getObject(db2,tag2,since2)
103  p.extract(ex)
104  coeff_2_b = [i for i in ex.values()]# 2nd set of coefficients
105  what = {'how':'endcap'}
106  w = inspect.setWhat(Plug.What(),what)
107  ex = Plug.Extractor(w)
108  p.extract(ex)
109  coeff_2_e = [i for i in ex.values()]# first set of coefficients
110 
111 
112  except Exception, er :
113  print er
114  else:
115  coeff_2_b,coeff_2_e = EcalPyUtils.fromXML(tag2)
116 
117  gStyle.SetPalette(1)
118 
119  savefile = TFile(filename,"RECREATE")
120 
121  ebhisto,ebmap, profx, profy= compareBarrel(coeff_1_b,coeff_2_b)
122  eephisto,eepmap,eemhisto,eemmap=compareEndcap(coeff_1_e,coeff_2_e)
123 
124 #make more canvas
125 
126 
127 
128  diff_distro_can = TCanvas("EBdiff","EBdiff")
129  diff_distro_can.Divide(2,2)
130 
131  diff_distro_can.cd(1)
132  ebhisto.Draw()
133  diff_distro_can.cd(2)
134  ebmap.Draw("colz")
135  diff_distro_can.cd(3)
136  profx.Draw()
137  diff_distro_can.cd(4)
138  profy.Draw()
139 
140 # diff_distro_can.SaveAs(filename)
141 
142  c2 = TCanvas("EEdiff","EEdiff")
143  c2.Divide(2,2)
144 
145  c2.cd(1)
146  eephisto.Draw()
147  c2.cd(2)
148  eepmap.Draw("colz")
149  c2.cd(3)
150  eemhisto.Draw()
151  c2.cd(4)
152  eemmap.Draw("colz")
153 
154  EEfilename = "EE_"+filename
155 
156 # c2.SaveAs(EEfilename)
157 
158 
159 
160  eeborderphisto,eeborderpmap,eebordermhisto,eebordermmap=compareEndcapBorder(coeff_1_e,coeff_2_e)
161  ebborderhisto,ebbordermap = compareBarrelBorder(coeff_1_b,coeff_2_b)
162 
163  border_diff_distro_can = TCanvas("border_difference","borders difference")
164  border_diff_distro_can.Divide(2,3)
165 
166  border_diff_distro_can.cd(1)
167  ebborderhisto.Draw()
168  border_diff_distro_can.cd(2)
169  ebbordermap.Draw("colz")
170  border_diff_distro_can.cd(3)
171  eeborderphisto.Draw()
172  border_diff_distro_can.cd(4)
173  eeborderpmap.Draw("colz")
174  border_diff_distro_can.cd(5)
175  eebordermhisto.Draw()
176  border_diff_distro_can.cd(6)
177  eebordermmap.Draw("colz")
178 
179  bordersfilename = "borders_"+filename
180  prof_filename = "profiles_"+filename
181 
182 # border_diff_distro_can.SaveAs(bordersfilename)
183 
184  savefile.Write()
185 
186 
187 
188 def histo (db, tag,since,filename='histo.root'):
189  '''Make histograms and save to file. tag can be an xml file'''
190 
191  coeff_barl=[]
192  coeff_endc=[]
193 
194  if tag.find(".xml")< 0:
195  try:
196  exec('import '+db.moduleName(tag)+' as Plug')
197 
198  what = {'how':'barrel'}
199  w = inspect.setWhat(Plug.What(),what)
200  ex = Plug.Extractor(w)
201  p=getObject(db,tag,since)
202  p.extract(ex)
203  coeff_barl = [i for i in ex.values()]
204 
205 
206  what = {'how':'endcap'}
207  w = inspect.setWhat(Plug.What(),what)
208  ex = Plug.Extractor(w)
209  p.extract(ex)
210  coeff_endc = [i for i in ex.values()]
211 
212  except Exception, er :
213  print er
214 
215  else :
216  coeff_barl,coeff_endc=EcalPyUtils.fromXML(tag)
217 
218 
219  savefile = TFile(filename,"RECREATE")
220 
221  ebmap, ebeta, ebphi, eePmap, ebdist, eeMmap, prof_eePL, prof_eePR, prof_eeML, prof_eeMR, ebBorderdist = makedist(coeff_barl, coeff_endc)
222 
223 
224 
225  gStyle.SetPalette(1)
226 
227  c = TCanvas("CCdist")
228  c.Divide(2,2)
229 
230  c.cd(1)
231  ebmap.Draw("colz")
232  c.cd(2)
233  eePmap.Draw("colz")
234  c.cd(3)
235  ebdist.Draw()
236  c.cd(4)
237  eeMmap.Draw("colz")
238 
239 # c.SaveAs(filename)
240 
241  prof_eb_eta = ebeta.ProfileX()
242  prof_eb_phi = ebphi.ProfileX()
243 
244  c2 = TCanvas("CCprofiles")
245  c2.Divide(2,2)
246 
247  leg = TLegend(0.1,0.7,0.48,0.9)
248 
249  c2.cd(1)
250  prof_eb_eta.Draw()
251  c2.cd(2)
252  prof_eb_phi.Draw()
253  c2.cd(3)
254  prof_eePL.SetMarkerStyle(8)
255  prof_eePL.Draw()
256  prof_eePR.SetMarkerStyle(8)
257  prof_eePR.SetMarkerColor(2)
258  prof_eePR.Draw("same")
259  prof_eeML.SetMarkerStyle(8)
260  prof_eeML.SetMarkerColor(3)
261  prof_eeML.Draw("same")
262  prof_eeMR.SetMarkerStyle(8)
263  prof_eeMR.SetMarkerColor(5)
264  prof_eeMR.Draw("same")
265  leg.AddEntry(prof_eePL,"Dee1","lp")
266  leg.AddEntry(prof_eePR,"Dee2","lp")
267  leg.AddEntry(prof_eeMR,"Dee3","lp")
268  leg.AddEntry(prof_eeML,"Dee4","lp")
269  leg.Draw()
270  c2.cd(4)
271  ebBorderdist.Draw()
272 
273  extrafilename = "profiles_"+filename
274  # c2.SaveAs(extrafilename)
275 
276  savefile.Write()
277 
278 
279 def getToken(db,tag,since):
280  ''' Return payload token for a given iov, tag, db'''
281  try :
282  iov = inspect.Iov(db,tag)
283  iovlist = iov.list()
284  for p in iovlist:
285  tmpsince=p[1]
286  if str(tmpsince)==str(since) :
287  return p[0]
288  print "Could not retrieve token for tag: " , tag, " since: ", since
289  sys.exit(0)
290 
291  except Exception, er :
292  print er
293 
294 
295 def getObject(db,tag,since):
296  ''' Return payload object for a given iov, tag, db'''
297  found=0
298  try:
299  exec('import '+db.moduleName(tag)+' as Plug')
300  for elem in db.iov(tag).elements :
301  if str(elem.since())==str(since):
302  found=1
303  return Plug.Object(elem)
304 
305  except Exception, er :
306  print er
307 
308  if not found :
309  print "Could not retrieve payload for tag: " , tag, " since: ", since
310  sys.exit(0)
311 
312 
313 def makedist(coeff_barl, coeff_endc) :
314 
315  ebmap = TH2F("EB","EB",360,1,261,171, -85,86)
316  eePmap = TH2F("EE","EE",100, 1,101,100,1,101)
317  eeMmap = TH2F("EEminus","EEminus",100,1,101,100,1,101)
318  ebdist = TH1F("EBdist","EBdist",100,-2,2)
319  ebBorderdist = TH1F("EBBorderdist","EBBorderdist",100,-2,2)
320 
321  ebeta = TH2F("ebeta","ebeta",171,-85,86,100,-2,2)
322  ebphi = TH2F("ebphi","ebphi",360,1,361,100,-2,2)
323 
324  eePL = TH2F("EEPL","EEPlus Left",50,10,55,100,-2,2)
325  eePR = TH2F("EEPR","EEPlus Right",50,10,55,100,-2,2)
326  eeML = TH2F("EEML","EEMinus Left",50,10,55,100,-2,2)
327  eeMR = TH2F("EEMR","EEMinus Right",50,10,55,100,-2,2)
328 
329  for i,c in enumerate(coeff_barl):
330  ieta,iphi = EcalPyUtils.unhashEBIndex(i)
331  ebmap.Fill(iphi,ieta,c)
332  ebdist.Fill(c)
333  ebeta.Fill(ieta,c)
334  ebphi.Fill(iphi,c)
335 
336  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):
337  ebBorderdist.Fill(c)
338 
339 
340  for i,c in enumerate(coeff_endc):
341  ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
342  R = sqrt((ix-50)*(ix-50)+(iy-50)*(iy-50))
343 
344  if iz>0:
345  eePmap.Fill(ix,iy,c)
346  if ix<50:
347  eePL.Fill(R,c,1)
348  if ix>50:
349  eePR.Fill(R,c,1)
350 
351  if iz<0:
352  eeMmap.Fill(ix,iy,c)
353  if ix<50:
354  eeML.Fill(R,c,1)
355  if ix>50:
356  eeMR.Fill(R,c,1)
357 
358  prof_eePL = eePL.ProfileX()
359  prof_eePR = eePR.ProfileX()
360  prof_eeML = eeML.ProfileX()
361  prof_eeMR = eeMR.ProfileX()
362 
363  return ebmap, ebeta, ebphi, eePmap, ebdist, eeMmap, prof_eePL, prof_eePR, prof_eeML, prof_eeMR, ebBorderdist
364 
365 def compareBarrel(coeff_barl_1,coeff_barl_2) :
366  '''Return an histogram and a map of the differences '''
367 
368  diff_distro_h = TH1F("diffh","diffh",100,-2,2)
369  diff_distro_m = TH2F("diffm","diffm",360,1,361,171,-85,86)
370  diff_distro_m.SetStats(0)
371  ebeta = TH2F("ebeta","ebeta",171,-85,86,100,-2,2)
372  ebphi = TH2F("ebphi","ebphi",360,1,361,100,-2,2)
373 
374 
375  for i,c in enumerate(coeff_barl_1):
376  diff = c - coeff_barl_2[i]
377  ieta,iphi= EcalPyUtils.unhashEBIndex(i)
378  diff_distro_h.Fill(diff)
379  diff_distro_m.Fill(iphi,ieta,diff)
380  ebeta.Fill(ieta,diff)
381  ebphi.Fill(iphi,diff)
382 
383  prof_x_h = ebeta.ProfileX()
384  prof_y_h = ebphi.ProfileX()
385 
386  return diff_distro_h, diff_distro_m, prof_x_h, prof_y_h
387 
388 
389 
390 def compareBarrelBorder(coeff_barl_1,coeff_barl_2) :
391  '''Return an histogram and a map of the differences '''
392 
393  diff_distro_border_h = TH1F("diffborderh","diffh",100,-2,2)
394  diff_distro_border_m = TH2F("diffborderm","diffm",360,1,361,171,-85,86)
395  diff_distro_border_m.SetStats(0)
396 
397  for i,c in enumerate(coeff_barl_1):
398  diff = c - coeff_barl_2[i]
399  ieta,iphi= EcalPyUtils.unhashEBIndex(i)
400  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):
401  diff_distro_border_h.Fill(diff)
402  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):
403  diff_distro_border_m.Fill(iphi,ieta,diff)
404 
405  return diff_distro_border_h, diff_distro_border_m
406 
407 
408 
409 
410 
411 def compareEndcap(coeff_endc_1, coeff_endc_2) :
412  ''' Return an histogram and a map of the differences for each endcap'''
413 
414  diff_distro_h_eep = TH1F("diff EE+","diff EE+",100,-2,2)
415  diff_distro_h_eem = TH1F("diff EE-","diff EE-",100,-2,2)
416 
417 
418  diff_distro_m_eep = TH2F("map EE+","map EE+",100,1,101,100,1,101)
419  diff_distro_m_eem = TH2F("map EE-","map EE-",100,1,101,100,1,101)
420 
421  temp_h = TH1F("tempR","tempR",50,0,50)
422 
423  diff_distro_m_eep.SetStats(0)
424  diff_distro_m_eem.SetStats(0)
425 
426 
427  for i,c in enumerate(coeff_endc_1):
428  diff = c - coeff_endc_2[i]
429  ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
430  R = sqrt((ix-50)*(ix-50)+(iy-50)*(iy-50))
431 
432  if iz >0:
433  diff_distro_h_eep.Fill(diff)
434  diff_distro_m_eep.Fill(ix,iy,diff)
435 
436  else:
437  diff_distro_h_eem.Fill(diff)
438  diff_distro_m_eem.Fill(ix,iy,diff)
439 
440  return diff_distro_h_eep, \
441  diff_distro_m_eep, \
442  diff_distro_h_eem, \
443  diff_distro_m_eem
444 
445 
446 
447 def compareEndcapBorder(coeff_endc_1, coeff_endc_2) :
448  ''' Return an histogram and a map of the differences for each endcap'''
449 
450  border_diff_distro_h_eep = TH1F("borderdiff EE+","diff EE+",100,-2,2)
451  border_diff_distro_h_eem = TH1F("borderdiff EE-","diff EE-",100,-2,2)
452 
453 
454  border_diff_distro_m_eep = TH2F("bordermap EE+","map EE+",100,1,101,100,1,101)
455  border_diff_distro_m_eem = TH2F("bordermap EE-","map EE-",100,1,101,100,1,101)
456 
457  border_diff_distro_m_eep.SetStats(0)
458  border_diff_distro_m_eem.SetStats(0)
459 
460 
461  for i,c in enumerate(coeff_endc_1):
462  diff = c - coeff_endc_2[i]
463  ix,iy,iz = EcalPyUtils.unhashEEIndex(i)
464  Rsq = ((ix-50.0)**2+(iy-50.0)**2)
465 
466  if (iz >0 and (Rsq<144.0 or Rsq>2500.0)):
467  border_diff_distro_h_eep.Fill(diff)
468  border_diff_distro_m_eep.Fill(ix,iy,diff)
469  elif (iz<0 and (Rsq<144.0 or Rsq>2500.0)):
470  border_diff_distro_h_eem.Fill(diff)
471  border_diff_distro_m_eem.Fill(ix,iy,diff)
472 
473 
474  return border_diff_distro_h_eep, \
475  border_diff_distro_m_eep, \
476  border_diff_distro_h_eem, \
477  border_diff_distro_m_eem
#define abs(x)
Definition: mlp_lapack.h:159
T sqrt(T t)
Definition: SSEVec.h:28
def unhashEBIndex
Definition: EcalPyUtils.py:9
def unhashEEIndex
Definition: EcalPyUtils.py:14