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