Belle II Software development
Saving2ndMVAData Class Reference
Inheritance diagram for Saving2ndMVAData:
HarvestingModule

Public Member Functions

 __init__ (self, name, contact=None, checkObj='RecoTracks', output_file_name='flip-refit-MVA2.root')
 
 initialize (self)
 
 prepare (self)
 
 pick (self, recoTrack)
 
 peel (self, recoTrack)
 
 id (self)
 
 event (self)
 
 terminate (self)
 
 barn (self)
 
 gather (self)
 
 refine (self, crops)
 

Static Public Member Functions

 create_crop_part_collection ()
 
 iter_store_obj (store_obj)
 

Public Attributes

 checkObj = checkObj
 Name of the Obj to be picked.
 
str mcRecoTracks = "MCRecoTracks"
 Name of the StoreArray of the mc tracks.
 
 track_match_look_up = None
 Reference to the track match lookup object reading the relation information.
 
 outputname = output_file_name
 Name of the output file.
 
 foreach = foreach
 Name of the StoreArray or iterable StoreObjPtr that contains the objects to be harvested.
 
 output_file_name = output_file_name
 Name of the ROOT output file to be generated.
 
 title = title or self.name()
 Name of this harvest.
 
 contact = contact
 Contact email address to be displayed on the validation page.
 
int expert_level = self.default_expert_level if expert_level is None else expert_level
 Integer expert level that controls to detail of plots to be generated.
 
list refiners = []
 A list of additional refiner instances to be executed on top of the refiner methods that are members of this class.
 
 show_results = show_results
 Switch to show the result ROOT file in a TBrowser on terminate.
 
 stash = self.barn()
 stash of the harvested crops (start with those in the barn)
 
 crops
 the dictionaries from peel
 
 raw_crops = raw_crops
 the dictionaries from peel as a numpy.array of doubles
 

Static Public Attributes

 save_tree = refiners.save_tree(name="data")
 save a tree of all the collected variables
 
int default_expert_level = 1
 The default value of expert_level if not specified explicitly by the caller.
 

Detailed Description

 A dedicated module to save the variables using in flipping steps

Definition at line 18 of file savingFlippingVariablesFor2ndMVA.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
name,
contact = None,
checkObj = 'RecoTracks',
output_file_name = 'flip-refit-MVA2.root' )
Constructor

Definition at line 21 of file savingFlippingVariablesFor2ndMVA.py.

21 def __init__(self, name, contact=None, checkObj='RecoTracks', output_file_name='flip-refit-MVA2.root'):
22 """Constructor"""
23 super().__init__(foreach=checkObj, name=name, contact=contact, output_file_name=output_file_name)
24
25 ## Name of the Obj to be picked
26 self.checkObj = checkObj
27
28 ## Name of the StoreArray of the mc tracks
29 self.mcRecoTracks = "MCRecoTracks"
30
31 ## Reference to the track match lookup object reading the relation information
32 self.track_match_look_up = None
33
34 ## Name of the output file
35 self.outputname = output_file_name
36

Member Function Documentation

◆ barn()

barn ( self)
inherited
Coroutine that receives the dictionaries of names and values from peel and store them.

Reimplemented in PickleHarvestingModule.

Definition at line 287 of file harvesting.py.

287 def barn(self):
288 """Coroutine that receives the dictionaries of names and values from peel and store them."""
289 crop = (yield)
290 raw_crops = copy.copy(crop)
291 crops = copy.copy(crop)
292
293 if isinstance(crop, numbers.Number):
294 raw_crops = self.create_crop_part_collection()
295 try:
296 while True:
297 raw_crops.append(crop)
298 # next crop
299 crop = (yield)
300 except GeneratorExit:
301 crops = np.array(raw_crops)
302
303 elif isinstance(crop, collections.abc.MutableMapping):
304 for part_name in crop:
305 raw_crops[part_name] = self.create_crop_part_collection()
306
307 try:
308 while True:
309 for part_name, parts in list(raw_crops.items()):
310 if part_name in crop:
311 parts.append(crop[part_name])
312 else:
313 parts.append(np.NaN)
314 # next crop
315 crop = (yield)
316 except GeneratorExit:
317 for part_name, parts in list(raw_crops.items()):
318 crops[part_name] = np.array(parts)
319
320 else:
321 msg = f"Unrecognised crop {crop} of type {type(crop)}"
322 raise ValueError(msg)
323
324
325 self.raw_crops = raw_crops
326
327 self.crops = crops
328

◆ create_crop_part_collection()

create_crop_part_collection ( )
staticinherited
Create the storing objects for the crop values

Currently a numpy.array of doubles is used to store all values in memory.

Definition at line 279 of file harvesting.py.

279 def create_crop_part_collection():
280 """Create the storing objects for the crop values
281
282 Currently a numpy.array of doubles is used to store all values in memory.
283 """
284 return array.array("d")
285

◆ event()

event ( self)
inherited
Event method of the module

* Does invoke the prepare method before the iteration starts.
* In each event fetch the StoreArray / iterable StoreObjPtr,
* Iterate through all instances
* Feed each instance to the pick method to decide it the instance is relevant
* Forward it to the peel method that should generated a dictionary of values
* Store each dictionary of values

Definition at line 239 of file harvesting.py.

239 def event(self):
240 """Event method of the module
241
242 * Does invoke the prepare method before the iteration starts.
243 * In each event fetch the StoreArray / iterable StoreObjPtr,
244 * Iterate through all instances
245 * Feed each instance to the pick method to decide it the instance is relevant
246 * Forward it to the peel method that should generated a dictionary of values
247 * Store each dictionary of values
248 """
249 self.prepare()
250 stash = self.stash.send
251 pick = self.pick
252 peel = self.peel
253 for crop in self.gather():
254 if pick(crop):
255 crop = peel(crop)
256 if isinstance(crop, types.GeneratorType):
257 many_crops = crop
258 for crop in many_crops:
259 stash(crop)
260 else:
261 stash(crop)
262

◆ gather()

gather ( self)
inherited
Iterator that yield the instances form the StoreArray / iterable StoreObj.

Yields
------
Object instances from the StoreArray, iterable StoreObj or the StoreObj itself
in case it is not iterable.

Definition at line 329 of file harvesting.py.

329 def gather(self):
330 """Iterator that yield the instances form the StoreArray / iterable StoreObj.
331
332 Yields
333 ------
334 Object instances from the StoreArray, iterable StoreObj or the StoreObj itself
335 in case it is not iterable.
336 """
337
338 registered_store_arrays = Belle2.PyStoreArray.list()
339 registered_store_objs = Belle2.PyStoreObj.list()
340
341 foreach = self.foreach
342 foreach_is_store_obj = foreach in registered_store_objs
343 foreach_is_store_array = foreach in registered_store_arrays
344
345 if foreach is not None:
346 if foreach_is_store_array:
347 store_array = Belle2.PyStoreArray(self.foreach)
348 yield from store_array
349
350 elif foreach_is_store_obj:
351 store_obj = Belle2.PyStoreObj(self.foreach)
352 try:
353 yield from self.iter_store_obj(store_obj)
354 except TypeError:
355 # Cannot iter the store object. Yield it instead.
356 yield store_obj.obj()
357
358 else:
359 msg = f"Name {self.foreach} does not refer to a valid object on the data store"
360 raise KeyError(msg)
361 else:
362 yield None
363
A (simplified) python wrapper for StoreArray.
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
a (simplified) python wrapper for StoreObjPtr.
Definition PyStoreObj.h:67
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available objects for given durability.
Definition PyStoreObj.cc:28

◆ id()

id ( self)
inherited
Working around that name() is a method.

Exposing the name as a property using a different name

Definition at line 224 of file harvesting.py.

224 def id(self):
225 """Working around that name() is a method.
226
227 Exposing the name as a property using a different name
228 """
229 return self.name()
230

◆ initialize()

initialize ( self)
Initialization at the start of the event processing

Reimplemented from HarvestingModule.

Definition at line 37 of file savingFlippingVariablesFor2ndMVA.py.

37 def initialize(self):
38 """Initialization at the start of the event processing"""
39 super().initialize()
40 self.track_match_look_up = Belle2.TrackMatchLookUp(self.mcRecoTracks, self.checkObj)
41 output_tfile = ROOT.TFile(self.outputname, "RECREATE")
42 self.outputname = output_tfile
43
Class to provide convenient methods to look up matching information between pattern recognition and M...

◆ iter_store_obj()

iter_store_obj ( store_obj)
staticinherited
Obtain a iterator from a StoreObj

Repeatedly calls iter(store_obj) or store_obj.__iter__()
until the final iterator returns itself

Returns
-------
iterator of the StoreObj

Definition at line 443 of file harvesting.py.

443 def iter_store_obj(store_obj):
444 """Obtain a iterator from a StoreObj
445
446 Repeatedly calls iter(store_obj) or store_obj.__iter__()
447 until the final iterator returns itself
448
449 Returns
450 -------
451 iterator of the StoreObj
452 """
453 iterable = store_obj.obj()
454 last_iterable = None
455 while iterable is not last_iterable:
456 if hasattr(iterable, "__iter__"):
457 iterable, last_iterable = iterable.__iter__(), iterable
458 else:
459 iterable, last_iterable = iter(iterable), iterable
460 return iterable
461
462

◆ peel()

peel ( self,
recoTrack )
store the information for each recoTrack

Reimplemented from HarvestingModule.

Definition at line 57 of file savingFlippingVariablesFor2ndMVA.py.

57 def peel(self, recoTrack):
58 """store the information for each recoTrack"""
59 track_match_look_up = self.track_match_look_up
60 nan = float('nan')
61 flipped_pz_estimate = nan
62 y_variance = nan
63 tan_lambda_estimate = nan
64 d0_variance = nan
65 x_variance = nan
66 z_estimate = nan
67 phi0_variance = nan
68 px_variance = nan
69 pz_estimate = nan
70 p_value = nan
71 pt_estimate = nan
72 y_estimate = nan
73 d0_estimate = nan
74 x_estimate = nan
75 py_variance = nan
76 pz_variance = nan
77 omega_variance = nan
78 tan_lambda_variance = nan
79 z_variance = nan
80 omega_estimate = nan
81 pt_resolution = nan
82 px_estimate = nan
83 pt_variance = nan
84 phi0_estimate = nan
85 flipped_z_estimate = nan
86 py_estimate = nan
87 flipped_z_variance = nan
88 flipped_pz_variance = nan
89 flipped_pt_variance = nan
90 flipped_py_estimate = nan
91 z0_variance = nan
92 flipped_p_value = nan
93 flipped_px_variance = nan
94 flipped_py_variance = nan
95 flipped_x_estimate = nan
96 quality_flip_indicator = nan
97 quality_2ndflip_indicator = nan
98 isPrimary_misID = False
99 ismatched = False
100 ismatched_CC = False
101 ismatched_WC = False
102 isclone_CC = False
103 isclone_WC = False
104 isclone = False
105 isbackground = False
106 isghost = False
107 isprimary = False
108 charge_truth = nan
109 track_charge = nan
110 inGoingArmTime = nan
111 inGoingArmTimeError = nan
112 outGoingArmTime = nan
113 outGoingArmTimeError = nan
114 InOutArmTimeDifference = nan
115 InOutArmTimeDifferenceError = nan
116
117 if (recoTrack):
118 mc_particle = track_match_look_up.getRelatedMCParticle(recoTrack)
119 fit_result = track_match_look_up.getRelatedTrackFitResult(recoTrack)
120
121 inGoingArmTime = recoTrack.getIngoingArmTime()
122 inGoingArmTimeError = recoTrack.getIngoingArmTimeError()
123 outGoingArmTime = recoTrack.getOutgoingArmTime()
124 outGoingArmTimeError = recoTrack.getOutgoingArmTimeError()
125 InOutArmTimeDifference = recoTrack.getInOutArmTimeDifference()
126 InOutArmTimeDifferenceError = recoTrack.getInOutArmTimeDifferenceError()
127
128 quality_flip_indicator = recoTrack.getFlipQualityIndicator()
129 quality_2ndflip_indicator = recoTrack.get2ndFlipQualityIndicator()
130
131 ismatched = track_match_look_up.isAnyChargeMatchedPRRecoTrack(recoTrack)
132 ismatched_CC = track_match_look_up.isCorrectChargeMatchedPRRecoTrack(recoTrack)
133 ismatched_WC = track_match_look_up.isWrongChargeMatchedPRRecoTrack(recoTrack)
134
135 isclone = track_match_look_up.isAnyChargeClonePRRecoTrack(recoTrack)
136 isclone_CC = track_match_look_up.isCorrectChargeClonePRRecoTrack(recoTrack)
137 isclone_WC = track_match_look_up.isWrongChargeClonePRRecoTrack(recoTrack)
138
139 isbackground = track_match_look_up.isBackgroundPRRecoTrack(recoTrack)
140 isghost = track_match_look_up.isGhostPRRecoTrack(recoTrack)
141
142 if mc_particle and fit_result:
143 isprimary = bool(mc_particle.hasStatus(Belle2.MCParticle.c_PrimaryParticle))
144 charge_truth = mc_particle.getCharge()
145 track_charge = fit_result.getChargeSign()
146 if isprimary:
147 if charge_truth != track_charge:
148 isPrimary_misID = True
149
150 recoTrack_flipped = recoTrack.getRelated("RecoTracks_flipped")
151 if recoTrack_flipped:
152 track_flipped = recoTrack_flipped.getRelated("Tracks_flipped")
153 if track_flipped:
154 fit_result_flipped = track_flipped.getTrackFitResultWithClosestMassByName(
155 Belle2.Const.pion, "TrackFitResults_flipped")
156 if (fit_result and fit_result_flipped):
157 cov6 = fit_result.getCovariance6()
158 mom = fit_result.getMomentum()
159 pos = fit_result.getPosition()
160
161 pt_estimate = mom.Rho()
162 pt_variance = np.divide(mom.X() ** 2 * cov6(3, 3) + mom.Y() ** 2 * cov6(4, 4) -
163 2 * mom.X() * mom.Y() * cov6(3, 4), mom.Perp2())
164 pt_resolution = np.divide(pt_variance, pt_estimate)
165
166 tan_lambda_estimate = fit_result.getCotTheta()
167 omega_estimate = fit_result.getOmega()
168 phi0_estimate = fit_result.getPhi() % (2.0 * math.pi)
169 d0_estimate = fit_result.getD0()
170 d0_variance = fit_result.getCov()[0]
171 z0_variance = fit_result.getCov()[12]
172 phi0_variance = fit_result.getCov()[5]
173 omega_variance = fit_result.getCov()[9]
174 tan_lambda_variance = fit_result.getCov()[14]
175 x_estimate = pos.X()
176 y_estimate = pos.Y()
177 z_estimate = pos.Z()
178 x_variance = cov6(0, 0)
179 y_variance = cov6(1, 1)
180 z_variance = cov6(2, 2)
181 px_estimate = mom.X()
182 py_estimate = mom.Y()
183 pz_estimate = cov6(5, 5)
184 px_variance = cov6(3, 3)
185 py_variance = cov6(4, 4)
186 pz_variance = cov6(5, 5)
187 p_value = fit_result.getPValue()
188
189 cov6_flipped = fit_result_flipped.getCovariance6()
190 mom_flipped = fit_result_flipped.getMomentum()
191 pos_flipped = fit_result_flipped.getPosition()
192
193 flipped_pt_variance = np.divide(
194 mom_flipped.X() ** 2 * cov6_flipped(3, 3) +
195 mom_flipped.Y() ** 2 * cov6_flipped(4, 4) -
196 2 * mom_flipped.X() * mom_flipped.Y() * cov6_flipped(3, 4),
197 mom_flipped.Perp2())
198
199 flipped_z_estimate = pos_flipped.Z()
200 flipped_pz_estimate = mom_flipped.Z()
201 flipped_py_estimate = mom_flipped.Y()
202
203 flipped_x_estimate = pos_flipped.X()
204 flipped_z_variance = cov6_flipped(2, 2)
205 flipped_pz_variance = cov6_flipped(5, 5)
206 flipped_px_variance = cov6_flipped(3, 3)
207 flipped_py_variance = cov6_flipped(4, 4)
208 flipped_p_value = fit_result_flipped.getPValue()
209
210 crops = dict(
211 flipped_pz_estimate=flipped_pz_estimate,
212 y_variance=y_variance,
213 tan_lambda_estimate=tan_lambda_estimate,
214 d0_variance=d0_variance,
215 x_variance=x_variance,
216 z_estimate=z_estimate,
217 phi0_variance=phi0_variance,
218 px_variance=px_variance,
219 pz_estimate=pz_estimate,
220 p_value=p_value,
221 pt_estimate=pt_estimate,
222 y_estimate=y_estimate,
223 d0_estimate=d0_estimate,
224 x_estimate=x_estimate,
225 py_variance=py_variance,
226 pz_variance=pz_variance,
227 omega_variance=omega_variance,
228 tan_lambda_variance=tan_lambda_variance,
229 z_variance=z_variance,
230 omega_estimate=omega_estimate,
231 pt_resolution=pt_resolution,
232 px_estimate=px_estimate,
233 pt_variance=pt_variance,
234 phi0_estimate=phi0_estimate,
235 flipped_z_estimate=flipped_z_estimate,
236 py_estimate=py_estimate,
237 flipped_z_variance=flipped_z_variance,
238 flipped_pz_variance=flipped_pz_variance,
239 flipped_pt_variance=flipped_pt_variance,
240 flipped_py_estimate=flipped_py_estimate,
241 z0_variance=z0_variance,
242 flipped_p_value=flipped_p_value,
243 flipped_px_variance=flipped_px_variance,
244 flipped_py_variance=flipped_py_variance,
245 flipped_x_estimate=flipped_x_estimate,
246 quality_flip_indicator=quality_flip_indicator,
247 quality_2ndflip_indicator=quality_2ndflip_indicator,
248 isPrimary_misID=isPrimary_misID,
249 ismatched=ismatched,
250 ismatched_CC=ismatched_CC,
251 ismatched_WC=ismatched_WC,
252 isclone_CC=isclone_CC,
253 isclone_WC=isclone_WC,
254 isclone=isclone,
255 isbackground=isbackground,
256 isghost=isghost,
257 isprimary=isprimary,
258 charge_truth=charge_truth,
259 track_charge=track_charge,
260 inGoingArmTime=inGoingArmTime,
261 inGoingArmTimeError=inGoingArmTimeError,
262 outGoingArmTime=outGoingArmTime,
263 outGoingArmTimeError=outGoingArmTimeError,
264 InOutArmTimeDifference=InOutArmTimeDifference,
265 InOutArmTimeDifferenceError=InOutArmTimeDifferenceError,
266 )
267 return crops
268

◆ pick()

pick ( self,
recoTrack )
pick every recoTrack

Reimplemented from HarvestingModule.

Definition at line 53 of file savingFlippingVariablesFor2ndMVA.py.

53 def pick(self, recoTrack):
54 """pick every recoTrack"""
55 return True
56

◆ prepare()

prepare ( self)
preparation at the start of each event.
   make sure the checkObj exist

Reimplemented from HarvestingModule.

Definition at line 44 of file savingFlippingVariablesFor2ndMVA.py.

44 def prepare(self):
45 """preparation at the start of each event.
46 make sure the checkObj exist
47 """
48 super().prepare()
49 checkDatas = Belle2.PyStoreArray(self.checkObj)
50 if (not checkDatas):
51 return False
52

◆ refine()

refine ( self,
crops )
inherited
Receive the gathered crops and forward them to the refiners.

Reimplemented in PickleHarvestingModule.

Definition at line 398 of file harvesting.py.

398 def refine(self, crops):
399 """Receive the gathered crops and forward them to the refiners."""
400
401 kwds = {}
402 if self.output_file_name:
403 # Save everything to a ROOT file
404 if isinstance(self.output_file_name, ROOT.TFile):
405 output_tdirectory = self.output_file_name
406 else:
407 output_tfile = ROOT.TFile(self.output_file_name, 'recreate')
408 output_tdirectory = output_tfile
409
410 else:
411 output_tdirectory = None
412
413 try:
414 with root_cd(output_tdirectory):
415 for refiner in self.refiners:
416 refiner(self, crops, tdirectory=output_tdirectory, **kwds)
417
418 # Get the methods marked as refiners from the class
419 cls = type(self)
420 for name in dir(cls):
421 if isinstance(getattr(cls, name), Refiner):
422 refiner = getattr(self, name)
423 # Getattr already binds self
424 refiner(crops, tdirectory=output_tdirectory, **kwds)
425
426 finally:
427 # If we opened the TFile ourself, close it again
428 if self.output_file_name:
429 if isinstance(self.output_file_name, str):
430 output_tfile.Close()
431
432 if self.show_results and self.output_file_name:
433 if isinstance(self.output_file_name, str):
434 output_tfile = ROOT.TFile(self.output_file_name)
435 root_browse(output_tfile)
436 input("Press enter to close")
437 output_tfile.Close()
438 else:
439 root_browse(self.output_file_name)
440 input("Press enter to close")
441

◆ terminate()

terminate ( self)
inherited
Termination method of the module.

Finalize the collected crops.
Start the refinement.

Reimplemented in ClusterFilterValidationModule.

Definition at line 263 of file harvesting.py.

263 def terminate(self):
264 """Termination method of the module.
265
266 Finalize the collected crops.
267 Start the refinement.
268 """
269
270 self.stash.close()
271 del self.stash
272
273 try:
274 self.refine(self.crops)
275 except AttributeError:
276 pass
277

Member Data Documentation

◆ checkObj

checkObj = checkObj

Name of the Obj to be picked.

Definition at line 26 of file savingFlippingVariablesFor2ndMVA.py.

◆ contact

contact = contact
inherited

Contact email address to be displayed on the validation page.

Definition at line 211 of file harvesting.py.

◆ crops

crops
inherited

the dictionaries from peel

Definition at line 274 of file harvesting.py.

◆ default_expert_level

int default_expert_level = 1
staticinherited

The default value of expert_level if not specified explicitly by the caller.

Definition at line 156 of file harvesting.py.

◆ expert_level

int expert_level = self.default_expert_level if expert_level is None else expert_level
inherited

Integer expert level that controls to detail of plots to be generated.

Definition at line 214 of file harvesting.py.

◆ foreach

foreach = foreach
inherited

Name of the StoreArray or iterable StoreObjPtr that contains the objects to be harvested.

Definition at line 196 of file harvesting.py.

◆ mcRecoTracks

str mcRecoTracks = "MCRecoTracks"

Name of the StoreArray of the mc tracks.

Definition at line 29 of file savingFlippingVariablesFor2ndMVA.py.

◆ output_file_name

output_file_name = output_file_name
inherited

Name of the ROOT output file to be generated.

Definition at line 199 of file harvesting.py.

◆ outputname

outputname = output_file_name

Name of the output file.

Definition at line 35 of file savingFlippingVariablesFor2ndMVA.py.

◆ raw_crops

raw_crops = raw_crops
inherited

the dictionaries from peel as a numpy.array of doubles

Definition at line 325 of file harvesting.py.

◆ refiners

list refiners = []
inherited

A list of additional refiner instances to be executed on top of the refiner methods that are members of this class.

Definition at line 218 of file harvesting.py.

◆ save_tree

save_tree = refiners.save_tree(name="data")
static

save a tree of all the collected variables

Definition at line 270 of file savingFlippingVariablesFor2ndMVA.py.

◆ show_results

show_results = show_results
inherited

Switch to show the result ROOT file in a TBrowser on terminate.

Definition at line 221 of file harvesting.py.

◆ stash

stash = self.barn()
inherited

stash of the harvested crops (start with those in the barn)

Definition at line 237 of file harvesting.py.

◆ title

title = title or self.name()
inherited

Name of this harvest.

Title particle of this harvest

Definition at line 208 of file harvesting.py.

◆ track_match_look_up

track_match_look_up = None

Reference to the track match lookup object reading the relation information.

Definition at line 32 of file savingFlippingVariablesFor2ndMVA.py.


The documentation for this class was generated from the following file: