CMS 3D CMS Logo

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