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

Public Member Functions

 __init__ (self, output_file_name, local_track_cands_store_array_name="LocalTrackCands", mc_track_cands_store_array_name="MCTrackCands", legendre_track_cand_store_array_name="LegendreTrackCands")
 
 prepare (self)
 
 peel (self, local_track_cand)
 
 id (self)
 
 initialize (self)
 
 event (self)
 
 terminate (self)
 
 barn (self)
 
 gather (self)
 
 pick (self, crop)
 
 refine (self, crops)
 

Static Public Member Functions

 create_crop_part_collection ()
 
 iter_store_obj (store_obj)
 

Public Attributes

 mc_track_cands_store_array_name = mc_track_cands_store_array_name
 cached name of the TrackCands StoreArray
 
 legendre_track_cand_store_array_name = legendre_track_cand_store_array_name
 cached name of the Legendre TrackCands StoreArray
 
 mc_track_matcher_local = Belle2.TrackMatchLookUp(self.mc_track_cands_store_array_name, self.foreach)
 function to match local tracks to MC tracks
 
 mc_track_matcher_legendre
 function to match Legendre tracks to MC tracks
 
 cdcHits = Belle2.PyStoreArray("CDCHits")
 cached CDCHits StoreArray
 
 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 to be executed at the end of the harvesting / termination of the module Save a tree of all collected variables in a sub folder.
 
int default_expert_level = 1
 The default value of expert_level if not specified explicitly by the caller.
 

Detailed Description

Harvesting module to check for basic matching information of the found segments.
If you want to have matching information, please use the MC matcher module to make all possible
combinations between legendre, local and MC track candidates.
Also, the Segments must be transformed into GF track cands.

Definition at line 141 of file harvester.py.

Constructor & Destructor Documentation

◆ __init__()

__init__ ( self,
output_file_name,
local_track_cands_store_array_name = "LocalTrackCands",
mc_track_cands_store_array_name = "MCTrackCands",
legendre_track_cand_store_array_name = "LegendreTrackCands" )
Initialize the harvesting module with
Arguments
---------
  output_file_name Name of the root file
  local_track_cands_store_array_name Name of the StoreArray for local track cands
  mc_track_cands_store_array_name Name of the StoreArray for MC track cands
  legendre_track_cand_store_array_name Name of the StoreArray for legendre track cands

Definition at line 150 of file harvester.py.

155 legendre_track_cand_store_array_name="LegendreTrackCands"):
156 """
157 Initialize the harvesting module with
158 Arguments
159 ---------
160 output_file_name Name of the root file
161 local_track_cands_store_array_name Name of the StoreArray for local track cands
162 mc_track_cands_store_array_name Name of the StoreArray for MC track cands
163 legendre_track_cand_store_array_name Name of the StoreArray for legendre track cands
164 """
165 super().__init__(
166 foreach=local_track_cands_store_array_name,
167 output_file_name=output_file_name)
168
169
170 self.mc_track_cands_store_array_name = mc_track_cands_store_array_name
171
172 self.legendre_track_cand_store_array_name = legendre_track_cand_store_array_name
173
174
175 self.mc_track_matcher_local = Belle2.TrackMatchLookUp(self.mc_track_cands_store_array_name, self.foreach)
176
177 self.mc_track_matcher_legendre = Belle2.TrackMatchLookUp(
178 self.mc_track_cands_store_array_name,
179 legendre_track_cand_store_array_name)
180
Class to provide convenient methods to look up matching information between pattern recognition and M...

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)
inherited
Initialisation method of the module.

Prepares the receiver stash of objects to be harvestered.

Reimplemented in ClusterFilterValidationModule, ElossHarvestingModule, LegendreBinningValidationModule, SegmentFitValidationModule, Saving1stMVAData, Saving2ndMVAData, SegmentPairCreationValidationModule, EventwiseTrackingValidationModule, MCSideTrackingValidationModule, and PRSideTrackingValidationModule.

Definition at line 231 of file harvesting.py.

231 def initialize(self):
232 """Initialisation method of the module.
233
234 Prepares the receiver stash of objects to be harvestered.
235 """
236
237 self.stash = self.barn()
238

◆ 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,
local_track_cand )
 Extract the information from the segments. 

Reimplemented from HarvestingModule.

Definition at line 187 of file harvester.py.

187 def peel(self, local_track_cand):
188 """ Extract the information from the segments. """
189 mc_track_matcher_local = self.mc_track_matcher_local
190 mc_track_matcher_legendre = self.mc_track_matcher_legendre
191
192 cdc_hits = self.cdcHits
193
194 is_background = mc_track_matcher_local.isBackgroundPRRecoTrack(local_track_cand)
195 is_ghost = mc_track_matcher_local.isGhostPRRecoTrack(local_track_cand)
196 is_matched = mc_track_matcher_local.isAnyChargeMatchedPRRecoTrack(local_track_cand)
197 is_clone = mc_track_matcher_local.isAnyChargeClonePRRecoTrack(local_track_cand)
198 is_clone_or_matched = is_matched or is_clone
199 hit_purity = abs(mc_track_matcher_local.getRelatedPurity(local_track_cand))
200
201 # Stereo Track?
202 first_cdc_hit_id = local_track_cand.getHitIDs(Belle2.Const.CDC)[0]
203 first_cdc_hit = cdc_hits[first_cdc_hit_id]
204 is_stereo = first_cdc_hit.getISuperLayer() % 2 == 1
205 superlayer_of_segment = first_cdc_hit.getISuperLayer()
206
207 has_partner = np.NaN
208 hit_purity_of_partner = np.NaN
209 hit_efficiency_of_partner = np.NaN
210 mc_track_pt = np.NaN
211 mc_track_dist = np.NaN
212 number_of_new_hits = local_track_cand.getNHits()
213 number_of_hits = local_track_cand.getNHits()
214 number_of_hits_in_same_superlayer = np.NaN
215 partner_has_stereo_information = np.NaN
216
217 if is_clone_or_matched:
218 related_mc_track_cand = mc_track_matcher_local.getRelatedMCRecoTrack(local_track_cand)
219 has_partner = (mc_track_matcher_legendre.isAnyChargeMatchedMCRecoTrack(related_mc_track_cand) or
220 mc_track_matcher_legendre.isAnyChargeMergedMCRecoTrack(related_mc_track_cand))
221 mc_track_pt = related_mc_track_cand.getMomSeed().Pt()
222 mc_track_dist = related_mc_track_cand.getPosSeed().Mag()
223 if has_partner:
224 partner_legendre_track_cand = mc_track_matcher_legendre.getRelatedPRRecoTrack(related_mc_track_cand)
225 hit_purity_of_partner = abs(mc_track_matcher_legendre.getRelatedPurity(partner_legendre_track_cand))
226 hit_efficiency_of_partner = abs(mc_track_matcher_legendre.getRelatedEfficiency(related_mc_track_cand))
227
228 # Count number of new hits
229 legendre_hits = set(list(partner_legendre_track_cand.getHitIDs()))
230 local_hits = set(list(local_track_cand.getHitIDs()))
231
232 local_hits_new = local_hits - legendre_hits
233 number_of_new_hits = len(local_hits_new)
234
235 # Count number of hits in this superlayer
236 partner_has_stereo_information = 0
237 number_of_hits_in_same_superlayer = 0
238 for cdc_hit_ID in legendre_hits:
239 cdc_hit = cdc_hits[cdc_hit_ID]
240 if cdc_hit.getISuperLayer() == superlayer_of_segment:
241 number_of_hits_in_same_superlayer += 1
242
243 if cdc_hit.getISuperLayer() % 2 == 1:
244 partner_has_stereo_information = 1
245
246 return dict(superlayer_of_segment=superlayer_of_segment,
247 mc_track_dist=mc_track_dist,
248 is_background=is_background,
249 is_ghost=is_ghost,
250 is_matched=is_matched,
251 is_clone=is_clone,
252 is_clone_or_matched=is_clone_or_matched,
253 is_stereo=is_stereo,
254 mc_track_pt=mc_track_pt,
255 hit_purity=hit_purity,
256 has_partner=has_partner,
257 hit_purity_of_partner=hit_purity_of_partner,
258 hit_efficiency_of_partner=hit_efficiency_of_partner,
259 number_of_new_hits=number_of_new_hits,
260 number_of_hits=number_of_hits,
261 number_of_hits_in_same_superlayer=number_of_hits_in_same_superlayer,
262 partner_has_stereo_information=partner_has_stereo_information)
263

◆ pick()

pick ( self,
crop )
inherited
Indicate whether the instance should be forwarded to the peeling

Returns
-------
bool : Indicator if the instance is valuable in the current harvest.

Reimplemented in VxdCdcMergerHarvesterMCSide, ClusterFilterValidationModule, MCParticleHarvester, ElossHarvestingModule, LegendreBinningValidationModule, SegmentFitValidationModule, Saving1stMVAData, Saving2ndMVAData, SegmentPairCreationValidationModule, EventwiseTrackingValidationModule, MCSideTrackingValidationModule, PRSideTrackingValidationModule, and VxdCdcMergerHarvester.

Definition at line 389 of file harvesting.py.

389 def pick(self, crop):
390 """Indicate whether the instance should be forwarded to the peeling
391
392 Returns
393 -------
394 bool : Indicator if the instance is valuable in the current harvest.
395 """
396 return True
397

◆ prepare()

prepare ( self)
 Prepare the CDC lookup. 

Reimplemented from HarvestingModule.

Definition at line 181 of file harvester.py.

181 def prepare(self):
182 """ Prepare the CDC lookup. """
183
184 self.cdcHits = Belle2.PyStoreArray("CDCHits")
185 return HarvestingModule.prepare(self)
186

◆ 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

◆ cdcHits

cdcHits = Belle2.PyStoreArray("CDCHits")

cached CDCHits StoreArray

Definition at line 184 of file harvester.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.

◆ legendre_track_cand_store_array_name

legendre_track_cand_store_array_name = legendre_track_cand_store_array_name

cached name of the Legendre TrackCands StoreArray

Definition at line 172 of file harvester.py.

◆ mc_track_cands_store_array_name

mc_track_cands_store_array_name = mc_track_cands_store_array_name

cached name of the TrackCands StoreArray

Definition at line 170 of file harvester.py.

◆ mc_track_matcher_legendre

mc_track_matcher_legendre
Initial value:
self.mc_track_cands_store_array_name,
legendre_track_cand_store_array_name)

function to match Legendre tracks to MC tracks

Definition at line 177 of file harvester.py.

◆ mc_track_matcher_local

mc_track_matcher_local = Belle2.TrackMatchLookUp(self.mc_track_cands_store_array_name, self.foreach)

function to match local tracks to MC tracks

Definition at line 175 of file harvester.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.

◆ 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
static
Initial value:
= refiners.save_tree(
)

Refiners to be executed at the end of the harvesting / termination of the module Save a tree of all collected variables in a sub folder.

Definition at line 266 of file harvester.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.


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