CMS 3D CMS Logo

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