| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

ExampleTree.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Simple Example of filling and writing a ROOT TTree 
00004 #
00005 #  Usage:
00006 #   nuwa.py -n -1 ExampleTree simulationData.root
00007 
00008 # Load DybPython
00009 from DybPython.DybPythonAlg import DybPythonAlg
00010 from GaudiPython import SUCCESS, FAILURE
00011 from GaudiPython import gbl
00012 from DybPython.Util import irange
00013 
00014 # Make shortcuts to any ROOT classes you want to use
00015 TTree = gbl.TTree
00016 from array import array
00017 
00018 
00019 # Define the algorithm
00020 class ExampleTreeAlg(DybPythonAlg):
00021     "Example Tree-writing Python Algorithm"
00022     def __init__(self,name):
00023         DybPythonAlg.__init__(self,name)
00024         return
00025 
00026     def initialize(self):
00027         status = DybPythonAlg.initialize(self)
00028         if status.isFailure(): return status
00029         self.info("initializing")
00030 
00031         # Initialize services
00032         #  Statistics Service: Use for histograms, graphs, trees
00033         self.statsSvc = self.svc('IStatisticsSvc','StatisticsSvc')
00034         if self.statsSvc == None:
00035             self.error("Failed to initialize statistics service.")
00036             return FAILURE        
00037 
00038         # Make a tree
00039         self.simTree= TTree("simTree","Tree of Simulated Hits")
00040 
00041         # Make branch addresses for single-numbers. 'i'=int, 'd'=double
00042         self.simTime = array('d',[0])
00043         self.nSimHits = array('i',[0])
00044         # Make branch addresses for array branches
00045         self.maxSimHits = 4096
00046         self.simHitPmtId = array('i',self.maxSimHits*[0])
00047         self.simHitTime = array('d',self.maxSimHits*[0])
00048 
00049         # Make Branches
00050         self.simTree.Branch('simTime',self.simTime,'simTimeSec/D')
00051         self.simTree.Branch('nSimHits',self.nSimHits,'nSimHits/I')
00052         self.simTree.Branch('simHitPmtId',self.simHitPmtId,
00053                             'simHitPmtId[nSimHits]/I')
00054         self.simTree.Branch('simHitTime',self.simHitTime,
00055                             'simHitTime[nSimHits]/D')
00056 
00057         status = self.statsSvc.put('/file0/sim/simTree', self.simTree)
00058         if status.isFailure(): return status
00059 
00060         return SUCCESS
00061 
00062     def execute(self):
00063         self.info("executing")
00064 
00065         # Access current data
00066         evt = self.evtSvc()
00067         simHdr = evt["/Event/Sim/SimHeader"]
00068         if simHdr == None:
00069             self.error("Failed to get current sim header")
00070             return FAILURE
00071 
00072         # Set Branch values
00073         self.simTime[0] = simHdr.timeStamp().GetSeconds()
00074         # Loop over hits in each detector
00075         shh = simHdr.hits()
00076         hitCollectionMap = shh.hitCollection()
00077         nHits = 0
00078         for shcPair in irange(hitCollectionMap.begin(),
00079                               hitCollectionMap.end()):
00080             detectorId = shcPair.first
00081             hitCollection = shcPair.second
00082             self.info( "Found %d hits for detector %d"
00083                        % ( hitCollection.collection().size(),
00084                            detectorId )
00085                        )
00086             for hit in hitCollection.collection():
00087                 self.simHitPmtId[nHits] = hit.sensDetId()
00088                 self.simHitTime[nHits] = hit.hitTime()
00089                 nHits += 1
00090         self.nSimHits[0] = nHits
00091 
00092         # Fill the tree with current branch values
00093         self.simTree.Fill()
00094 
00095         return SUCCESS
00096         
00097     def finalize(self):
00098         self.info("finalizing")
00099         status = DybPythonAlg.finalize(self)
00100         return status
00101 
00102 
00103 #####  Job Configuration for nuwa.py ########################################
00104 
00105 def configure():
00106     from StatisticsSvc.StatisticsSvcConf import StatisticsSvc
00107     statsSvc = StatisticsSvc()
00108     statsSvc.Output ={"file0":"simHitStats.root"}
00109     return
00110 
00111 def run(app):
00112     '''
00113     Configure and add an algorithm to job
00114     '''
00115     app.ExtSvc += ["StatisticsSvc"]
00116     example = ExampleTreeAlg("MyTreeExample")
00117     app.addAlgorithm(example)
00118     pass
00119 
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:05:58 2011 for DivingIn by doxygen 1.4.7