Belle II Software  release-08-01-10
clusterFilterValidation.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 
11 
12 import logging
13 from tracking.run.mixins import BrowseTFileOnTerminateRunMixin
14 from tracking.run.event_generation import StandardEventGenerationRun
15 import tracking.metamodules as metamodules
16 import tracking.harvest.refiners as refiners
17 import tracking.harvest.harvesting as harvesting
18 from ROOT import Belle2 # make Belle2 namespace available
19 import sys
20 
21 import basf2
22 
23 from ROOT import gSystem
24 gSystem.Load('libtracking')
25 gSystem.Load('libtracking_trackFindingCDC')
26 
27 
28 def get_logger():
29  return logging.getLogger(__name__)
30 
31 
32 CONTACT = "oliver.frost@desy.de"
33 
34 
36  """Prepare and execute a basf2 job to read generated events or generate new events then validate the CDC cluster filter"""
37 
38  cluster_preparation_module = basf2.register_module("TFCDC_ClusterPreparer")
39 
40 
41  py_profile = True
42 
43  output_file_name = "ClusterFilterValidation.root" # Specification for BrowseTFileOnTerminateRunMixin
44 
45  def create_argument_parser(self, **kwds):
46  """Configure the basf2 job script using the translated command-line arguments"""
47  argument_parser = super(ClusterFilterValidationRun, self).create_argument_parser(**kwds)
48  return argument_parser
49 
50  def create_path(self):
51  """
52  Sets up a path that plays back pregenerated events or generates events
53  based on the properties in the base class.
54  """
55  main_path = super(ClusterFilterValidationRun, self).create_path()
56 
57  cluster_preparation_module = self.get_basf2_module(self.cluster_preparation_modulecluster_preparation_module)
58  main_path.add_module(cluster_preparation_module)
59 
60  # main_path.add_module(AxialStereoPairFitterModule())
61  validation_module = ClusterFilterValidationModule(output_file_name=self.output_file_nameoutput_file_nameoutput_file_name)
62  if self.py_profilepy_profile:
63  main_path.add_module(metamodules.PyProfilingModule(validation_module))
64  else:
65  main_path.add_module(validation_module)
66 
67  return main_path
68 
69 
70 class ClusterFilterValidationModule(harvesting.HarvestingModule):
71 
72  """Module to collect information about the facet creation cuts and compose validation plots on terminate."""
73 
74  def __init__(self, output_file_name):
75  """Constructor"""
76  super(ClusterFilterValidationModule, self).__init__(foreach="CDCWireHitClusterVector",
77  output_file_name=output_file_name)
78 
80 
81  self.cluster_varsetcluster_varset = Belle2.TrackFindingCDC.CDCWireHitClusterVarSet()
82 
83  def initialize(self):
84  """Receive signal at the start of event processing"""
85  self.cluster_varsetcluster_varset.initialize()
86  super(ClusterFilterValidationModule, self).initialize()
87 
88  def terminate(self):
89  """Receive signal at the end of event processing"""
90  self.cluster_varsetcluster_varset.terminate()
91  super(ClusterFilterValidationModule, self).terminate()
92 
93  def prepare(self):
94  """Fill the MC hit table"""
95  self.mc_hit_lookupmc_hit_lookup.fill()
96 
97  def pick(self, facet):
98  """Always pick, never reject"""
99  return True
100 
101  def peel(self, cluster):
102  """Extract and store CDC hit and cluster information"""
103 
104  mc_hit_lookup = self.mc_hit_lookupmc_hit_lookup
105 
106  self.cluster_varsetcluster_varset.extract(cluster)
107  cluster_crops = self.cluster_varsetcluster_varset.getNamedValues()
108  cluster_crops = dict(cluster_crops)
109 
110  # Truth variables
111  n_background = 0
112  for wireHit in list(cluster.items()):
113  cdcHit = wireHit.getHit()
114  if mc_hit_lookup.isBackground(cdcHit):
115  n_background += 1
116 
117  truth_dict = dict(
118  n_background_truth=n_background,
119  background_fraction_truth=1.0 * n_background / cluster.size()
120  )
121 
122  cluster_crops.update(truth_dict)
123  return cluster_crops
124 
125 
127  save_tree = refiners.save_tree(
128 
129  folder_name="tree"
130 
131  )
132 
133  save_histograms = refiners.save_histograms(
134 
135  outlier_z_score=5.0,
136  allow_discrete=True,
137  folder_name="histograms"
138 
139  )
140 
141 
142 def main():
144  run.configure_and_execute_from_commandline()
145 
146 
147 if __name__ == "__main__":
148  logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(levelname)s:%(message)s')
149  main()
static const CDCMCHitLookUp & getInstance()
Getter for the singletone instance.
mc_hit_lookup
reference to the CDCMCHitlookUp singleton
cluster_varset
reference to the CDCWireHitClusterVarSet
cluster_preparation_module
basf2 module for CDC cluster preparation
output_file_name
There is no default for the name of the output TFile.
Definition: mixins.py:61
Definition: main.py:1
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:91