Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Aida2RootEx.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
19 " Simple example to illustrate the usage of aida2root converter "
20 from __future__ import print_function
21 
22 # =============================================================================
23 __author__ = "Vanya BELYAEV ibelyaev@phys.syr.edu"
24 # =============================================================================
25 import os
26 import sys
27 
28 import ROOT
29 
30 if "-b" in sys.argv:
31  # Force batch mode
32  ROOT.gROOT.SetBatch(True)
33 
34 from GaudiPython.GaudiAlgs import SUCCESS, HistoAlgo, aida2root
35 
36 # list of booked histograms
37 paths = (
38  "HistoEx/ 1D histo ",
39  "HistoEx/ 2D histo ",
40  "HistoEx/ 3D histo ",
41  "HistoEx1/ 1D histo ",
42  "HistoEx1/ 2D histo ",
43  "HistoEx1/ 3D histo ",
44  "HistoEx2/ x vs y ",
45  "HistoEx2/ x vs y+3x ",
46  "HistoEx2/ x vs y-3x ",
47  "HistoEx2/ x vs y (profile)",
48  "HistoEx2/ x vs y+3x (profile)",
49  "HistoEx2/ x vs y-3x (profile)",
50 )
51 
52 # =============================================================================
53 # @class Aida2RootEx1
54 # Simple algorithm which used aida2root utility
55 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
56 # @date 2007-01-24
57 
58 
60  # Standard Constructor
61  def __init__(self, name="Aida2RootEx1"):
62  """Standard Constructor"""
63  HistoAlgo.__init__(self, name)
64 
65  # the main execution method
66  def execute(self):
67  "The main execution method"
68 
69  # list of booked histograms
70  for path in paths:
71  self.Print("AIDA object: '%s'" % path)
72  # get AIDA pointer
73  aida = self.histoSvc(path)
74  if not aida:
75  return self.Error("Invalid AIDA at '%s'" % path)
76  # explicitly convert to ROOT
77  root = aida2root(aida)
78  if not root:
79  return self.Error("Invalid conversion to ROOT '%s'" % path)
80  # use the native ROOT printout
81  root.Print()
82 
83  return SUCCESS
84 
85 
86 # =============================================================================
87 
88 # =============================================================================
89 # @class Aida2RootEx2
90 # Simple algorithm which uses aida2root utility
91 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
92 # @date 2007-01-24
93 
94 
96  # Standard Constructor
97  def __init__(self, name="Aida2RootEx2"):
98  """Standard Constructor"""
99  HistoAlgo.__init__(self, name)
100 
101  # the main execution method
102  def execute(self):
103  "The main execution method"
104 
105  # get the service itself
106  s = self.histoSvc()
107 
108  for path in paths:
109  self.Print("AIDA object: '%s'" % path)
110  root = s.getAsROOT(path)
111  if not root:
112  return self.Error("Invalid conversion to ROOT '%s'" % path)
113  # use the native ROOT printout
114  root.Print()
115 
116  return SUCCESS
117 
118 
119 # =============================================================================
120 
121 
122 # =============================================================================
123 # The main configuration method
124 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
125 # @date 2007-01-24
126 def configure(gaudi=None):
127  """the main configuration method"""
128 
129  if not gaudi:
130  from GaudiPython.Bindings import AppMgr
131 
132  gaudi = AppMgr()
133 
134  # reuse the previous example
135  import HistoEx2
136 
137  HistoEx2.configure(gaudi)
138 
139  hsvc = gaudi.service("HistogramPersistencySvc")
140  hsvc.OutputFile = "aida2rootex.root"
141 
142  # create the algorithms
143  alg1 = Aida2RootEx1()
144  alg2 = Aida2RootEx2()
145  # append them to the list of Top-Level algorithms
146  gaudi.addAlgorithm(alg1)
147  gaudi.addAlgorithm(alg2)
148 
149  return SUCCESS
150 
151 
152 # =============================================================================
153 # The third way to convert AIDA histograms into ROOT
154 # @author Vanya BELYAEV ibelyaev@physics.syr.edu
155 # @date 2007-01-24
156 def useScript(histos):
157  "the third way to convert AIDA histograms into ROOT"
158 
159  from GaudiPython.Bindings import AppMgr
160 
161  g = AppMgr()
162 
163  hsvc = g.histsvc()
164 
165  i = 0
166  for histo in histos:
167  root = hsvc.getAsROOT(histo)
168  if not root:
169  print("ERROR in access the histogram '%s' " % histo)
170  continue
171  canvas = ROOT.TCanvas("canvas_%d" % i, histo, 250, 250)
172  root.Draw()
173  name = histo.replace("/", "_")
174  name = name.replace("\\", "_")
175  name = name.replace('"', "_")
176  name = name.replace("'", "_")
177  name = name.replace("'", "_")
178  name = name.replace(" ", "_")
179  name = name.replace(os.sep, "_") + ".png"
180  if os.path.exists(name):
181  # strictly speaking, it is not needed, but avoids a message on the stderr
182  os.remove(name)
183  canvas.Print(name)
184  print("The file name is '%s'" % name)
185  i += 1
186 
187 
188 # =============================================================================
189 
190 # =============================================================================
191 # The actual job execution
192 # =============================================================================
193 if "__main__" == __name__:
194  print(__doc__, __author__)
195 
196  from GaudiPython.Bindings import AppMgr
197 
198  gaudi = AppMgr()
199  configure(gaudi)
200  gaudi.run(5)
201 
202  # use the scripts
203  useScript(paths)
204 
205 # =============================================================================
206 # The END
207 # =============================================================================
Aida2RootEx.Aida2RootEx1.__init__
def __init__(self, name="Aida2RootEx1")
Definition: Aida2RootEx.py:61
GaudiPython.GaudiAlgs.HistoAlgo
Definition: GaudiAlgs.py:768
Aida2RootEx.configure
def configure(gaudi=None)
Definition: Aida2RootEx.py:126
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:869
Aida2RootEx.Aida2RootEx1.execute
def execute(self)
Definition: Aida2RootEx.py:66
Aida2RootEx.Aida2RootEx2
Definition: Aida2RootEx.py:95
GaudiPython.Bindings
Definition: Bindings.py:1
Aida2RootEx.Aida2RootEx2.execute
def execute(self)
Definition: Aida2RootEx.py:102
HistoEx2.configure
def configure(gaudi=None)
Definition: HistoEx2.py:63
Aida2RootEx.Aida2RootEx1
Definition: Aida2RootEx.py:59
Aida2RootEx.useScript
def useScript(histos)
Definition: Aida2RootEx.py:156
GaudiMP.GMPBase.aida2root
aida2root
Definition: GMPBase.py:74
Aida2RootEx.Aida2RootEx2.__init__
def __init__(self, name="Aida2RootEx2")
Definition: Aida2RootEx.py:97
GaudiPython.GaudiAlgs
Definition: GaudiAlgs.py:1