Belle II Software  release-05-01-25
ARICHStandAlone.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 from basf2 import *
5 from optparse import OptionParser
6 import os
7 
8 # --------------------------------------------------------------------
9 # Example script for ARICH stand alone simulation and reconstruction
10 # (only ARICH is simulated, very fast)
11 # Instead of tracks from CDC, the track information is taken from MC hits
12 # on aerogel plane (ARICHAeroHit).
13 # Smearing of track position and direction is applied to mimic tracking
14 # resolution.
15 #
16 # Author: Luka Santelj (Oct. 2016)
17 # Contributors : Luka Santelj, Leonid Burmistrov
18 # --------------------------------------------------------------------
19 
20 parser = OptionParser()
21 parser.add_option('-n', '--nevents', dest='nevents', default=1000, help='Number of events to process')
22 parser.add_option('-f', '--file', dest='filename', default='ARICHEvents.root')
23 parser.add_option('-r', '--hepr', action="store_true", dest='hepr', default=False, help='Visualisation with heprep')
24 parser.add_option('-o', '--overlap', action="store_true", dest='overlap', default=False, help='Run overlap checker')
25 parser.add_option('-b', '--rootbatch', action="store_true", dest='rootbatch',
26  default=False, help='Run analysis root script in batch mode')
27 parser.add_option('-m', '--rootoff', action="store_true", dest='rootoff', default=False, help='Do not run root script in the end')
28 
29 (options, args) = parser.parse_args()
30 
31 home = os.environ['BELLE2_LOCAL_DIR']
32 
33 # use_local_database("centraldb/dbcache.txt")
34 # use_local_database("./ARICH_db_Test/centraldb/database.txt", "", False, LogLevel.ERROR, False)
35 
36 # Suppress messages and warnings during processing:
37 set_log_level(LogLevel.ERROR)
38 
39 # Create path
40 main = create_path()
41 
42 # Set number of events to generate
43 eventinfosetter = register_module('EventInfoSetter')
44 if not (options.hepr):
45  if not (options.overlap):
46  eventinfosetter.param({'evtNumList': [int(options.nevents)], 'runList': [1]})
47 if (options.hepr):
48  eventinfosetter.param({'evtNumList': [1], 'runList': [1]})
49 if (options.overlap):
50  eventinfosetter.param({'evtNumList': [1], 'runList': [1]})
51 main.add_module(eventinfosetter)
52 
53 # Histogram manager immediately after master module
54 histo = register_module('HistoManager')
55 histo.param('histoFileName', 'DQMhistograms.root') # File to save histograms
56 main.add_module(histo)
57 
58 # Gearbox: access to database (xml files)
59 gearbox = register_module('Gearbox')
60 main.add_module(gearbox)
61 
62 # Geometry
63 # only ARICH and magnetic field
64 geometry = register_module('Geometry')
65 geometry.param('components', [
66  'MagneticField',
67  'ARICH'])
68 main.add_module(geometry)
69 
70 # Particle gun
71 particlegun = register_module('ParticleGun')
72 particlegun.param('pdgCodes', [211, -211, 321, -321])
73 particlegun.param('nTracks', 1)
74 # particlegun.param('varyNTracks', True)
75 particlegun.param('momentumGeneration', 'uniform')
76 particlegun.param('momentumParams', [0.5, 4])
77 # particlegun.param('momentumParams', [0.4, 0.8])
78 particlegun.param('thetaGeneration', 'uniformCos')
79 particlegun.param('thetaParams', [17, 35])
80 particlegun.param('phiGeneration', 'uniform')
81 particlegun.param('phiParams', [0, 360])
82 particlegun.param('vertexGeneration', 'fixed')
83 particlegun.param('xVertexParams', [0])
84 particlegun.param('yVertexParams', [0])
85 particlegun.param('zVertexParams', [0])
86 particlegun.param('independentVertices', False)
87 main.add_module(particlegun)
88 
89 # Simulation
90 simulation = register_module('FullSim')
91 # Visualisation with HepRep
92 if (options.hepr):
93  print('Visualisation with HepRep')
94  simulation.param('EnableVisualization', True)
95  simulation.param('UICommandsAtIdle', [
96  '/vis/open HepRepFile',
97  '/vis/scene/create',
98  '/vis/scene/add/volume',
99  '/vis/sceneHandler/attach',
100  '/vis/viewer/flush',
101  '/vis/scene/add/trajectories smooth',
102  '/vis/scene/add/hits'
103  ])
104 
105 main.add_module(simulation)
106 
107 # Check for volume intersection/overlaps
108 if (options.overlap):
109  print('Check for volume intersection/overlaps')
110  overlapchecker = register_module('OverlapChecker')
111  main.add_module(overlapchecker)
112 
113 # ARICH digitization
114 arichDigi = register_module('ARICHDigitizer')
115 main.add_module(arichDigi)
116 
117 # convert ARICHDigits to ARICHHits
118 arichHits = register_module('ARICHFillHits')
119 main.add_module(arichHits)
120 
121 # ARICH reconstruction
122 # calculate PID likelihoods for all tracks
123 arichreco = register_module('ARICHReconstructor')
124 # use MC hits (ARICHAeroHits) instead of reconstructed tracks
125 arichreco.param('inputTrackType', 1)
126 # store Cherenkov angle information
127 arichreco.param('storePhotons', 1)
128 main.add_module(arichreco)
129 
130 # ARICH Ntuple
131 # create flat ntuple for performance analysis
132 arichNtuple = register_module('ARICHNtuple')
133 arichNtuple.param('outputFile', options.filename)
134 main.add_module(arichNtuple)
135 
136 # ARICH DQM
137 # create DQM occupancy plots
138 arichdqm = register_module('ARICHDQM')
139 main.add_module(arichdqm)
140 
141 # Uncomment to store DataStore content to root file
142 # output = register_module('RootOutput')
143 # output.param('outputFileName', 'DataStore.root')
144 # main.add_module(output)
145 
146 # Uncomment to show event display
147 # display = register_module('Display')
148 # display.param('showARICHHits', True)
149 # display.param('fullGeometry', True)
150 # main.add_module(display)
151 
152 # Show progress of processing
153 progress = register_module('Progress')
154 main.add_module(progress)
155 
156 # Process events
157 process(main)
158 
159 # Print call statistics
160 print(statistics)
161 
162 # Make basic performance plots
163 if (options.rootbatch):
164  com = 'root -l -b -q ' + options.filename + ' ' + home + '/arich/utility/scripts/plotEfficiency.C'
165 else:
166  com = 'root -l ' + options.filename + ' ' + home + '/arich/utility/scripts/plotEfficiency.C'
167 
168 if not (options.hepr):
169  if not (options.overlap):
170  if not (options.rootoff):
171  os.system(com)
172 
173 # Retrieve of the histograms from TCanvas produced by plotEfficiency.C
174 com = 'root -l -b -q ' + 'ARICHPerformance.root' + ' ' + home + '/arich/utility/scripts/plotEfficiencyConvertTCanvasToHist.C'
175 if not (options.hepr):
176  if not (options.overlap):
177  if not (options.rootoff):
178  os.system(com)