ATLAS Offline Software
L1CaloDumpRampDataAlgorithm.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 
3 import AthenaPython.PyAthena as PyAthena
4 from array import array
5 import ROOT
6 from PyKernel import PyKernel
7 import scipy
8 import traceback
9 
10 # vscharf 20150519: This algorithm doesn't work with release 20.1.X-VAL due to
11 # memory management issues between ROOT and python. Replaced by
12 # TrigT1CaloCalibUtils/L1CaloDumpRampData
14  """ Test reading back in conditions data. """
15  def __init__(self, name="L1CaloDumpRampDataAlgorithm"):
16  super(L1CaloDumpRampDataAlgorithm, self).__init__(name=name)
17  self.detectorStore = None
18 
19  def initialize(self):
20  self.detectorStore = PyAthena.py_svc("StoreGateSvc/DetectorStore")
21 
22  if self.detectorStore is None:
23  self.msg.failure("Problem retrieving DetectorStore")
24  return PyAthena.StatusCode.Failure
25 
26  return PyAthena.StatusCode.Success
27 
28  def execute(self):
29  """ Read L1CaloRampData in finalize, nothing to do here. """
30  return PyAthena.StatusCode.Success
31 
32  def finalize(self):
33  try: # retrieve ramp data container from detstore
34  rampDataContainer = PyKernel.retrieveDet(PyAthena.L1CaloRampDataContainer,
35  "/L1CaloRampMaker/L1CaloRampDataContainer")
36  energyScanResults = PyKernel.retrieveDet(PyAthena.CondAttrListCollection,
37  "/TRIGGER/L1Calo/V1/Results/EnergyScanResults")
38  except Exception:
39  rampDataContainer = None
40 
41  if rampDataContainer is None:
42  self.msg.error("Error retrieve L1Calo data.")
43  return PyAthena.StatusCode.Failure
44 
45  # root tgraph expects array objects
46  x = array('d') # calorimeter
47  y = array('d') # level1 adc
48  ex = array('d')
49  ey = array('d')
50 
51  xmlfile = open("rampdata.xml", "w")
52  func = ROOT.TF1("func", "pol1", 0., 255.)
53  xml_channel = "<Channel coolid='0x%x' slope='%.4f' offset='%.4f' chi2='%.4f' />\n"
54  isfinite = scipy.isfinite
55 
56  xmlfile.write("""<?xml version='1.0' encoding='utf-8'?>
57 <TriggerTowerDecoratorData>
58 <default name='slope' />
59 """)
60 
61  rootfile = ROOT.TFile.Open("graphs.root", "RECREATE")
62  if not rootfile:
63  self.msg.error("Could not open root file.")
64  return PyAthena.StatusCode.Failure
65 
66  for rampDataContainerIt in rampDataContainer:
67  del x[:]
68  del y[:]
69  del ex[:]
70  del ey[:]
71 
72  coolId, rampData = (rampDataContainerIt.first, rampDataContainerIt.second)
73  for step in range(rampData.getNSteps()):
74  if step != 0 and rampData.getLevel1(step).mean() < x[-1]:
75  continue # protect against saturated tower problems
76  x.append(rampData.getLevel1(step).mean())
77  ex.append(rampData.getLevel1(step).rms())
78  y.append(rampData.getCalo(step).mean())
79  ey.append(rampData.getCalo(step).rms())
80 
81  if len(x) == 0: continue
82  try:
83  ramp = ROOT.TGraphErrors(len(x), x, y, ex, ey)
84  except Exception as e:
85  traceback.print_exc(e)
86  continue
87 
88  attrList = energyScanResults.attributeList(coolId)
89  try:
90  slope = attrList["Slope"]
91  offset = attrList["Offset"]
92  chi2 = attrList["Chi2"]
93  func.SetParameters(offset, slope)
94 
95  except Exception:
96  self.msg.warning("Could not obtain fit data from database.")
97 
98  func.SetParameters(0., 1.)
99  ramp.Fit(func, "QFR0")
100  slope = func.GetParameter(1)
101  offset = func.GetParameter(0)
102  chi2 = func.GetChisquare()
103 
104  if isfinite(chi2) and isfinite(offset) and isfinite(slope):
105  ramp.GetListOfFunctions().Add(func.Clone("func_0x%x" % coolId))
106  xmlfile.write(xml_channel % (coolId, slope, offset, chi2))
107 
108  ramp.SetName("0x%x" % coolId)
109  ramp.SetTitle("Ramp 0x%x" % coolId)
110  ramp.Write()
111 
112  try:
113  rootfile.Close()
114  except Exception as e:
115  traceback.print_exc(e)
116 
117  xmlfile.write("</TriggerTowerDecoratorData>\n")
118  xmlfile.close()
119  return PyAthena.StatusCode.Success
mean
void mean(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:254
PyAthena::Alg::initialize
virtual StatusCode initialize() override
Definition: PyAthenaAlg.cxx:60
PyAthena::Alg::execute
virtual StatusCode execute() override
Definition: PyAthenaAlg.cxx:93
PyAthena::Alg::finalize
virtual StatusCode finalize() override
Definition: PyAthenaAlg.cxx:86
L1CaloDumpRampDataAlgorithm.L1CaloDumpRampDataAlgorithm
Definition: L1CaloDumpRampDataAlgorithm.py:13
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
L1CaloDumpRampDataAlgorithm.L1CaloDumpRampDataAlgorithm.__init__
def __init__(self, name="L1CaloDumpRampDataAlgorithm")
Definition: L1CaloDumpRampDataAlgorithm.py:15
L1CaloDumpRampDataAlgorithm.L1CaloDumpRampDataAlgorithm.detectorStore
detectorStore
Definition: L1CaloDumpRampDataAlgorithm.py:17
array
python.grid.Add
def Add(name)
Definition: grid.py:41
Trk::open
@ open
Definition: BinningType.h:40
AthCommonMsg< Algorithm >::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
beamspotnt.rms
rms
Definition: bin/beamspotnt.py:1266
PyAthena::Alg
Definition: PyAthenaAlg.h:33
error
Definition: IImpactPoint3dEstimator.h:70