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