Belle II Software development
HarvestingModule Class Reference
Inheritance diagram for HarvestingModule:
VxdCdcMergerHarvesterMCSide VxdCdcMergerHarvesterPRSide FitValidationModule VxdCdcPartFinderHarvester MCParticleHarvester MCTrajectoryHarvester ReconstructionPositionHarvester SeedsAnalyser SegmentFakeRatesModule SegmentFinderParameterExtractorModule WrongRLInfoCounter PickleHarvestingModule VxdCdcMergerHarvester QueueHarvester

Public Member Functions

def __init__ (self, foreach, output_file_name, name=None, title=None, contact=None, expert_level=None, show_results=False)
 
def id (self)
 
def initialize (self)
 
def event (self)
 
def terminate (self)
 
def barn (self)
 
def gather (self)
 
def prepare (self)
 
def peel (self, crop)
 
def pick (self, crop)
 
def refine (self, crops)
 

Static Public Member Functions

def create_crop_part_collection ()
 
def iter_store_obj (store_obj)
 

Public Attributes

 foreach
 Name of the StoreArray or iterable StoreObjPtr that contains the objects to be harvested.
 
 output_file_name
 Name of the ROOT output file to be generated.
 
 title
 Name of this harvest.
 
 contact
 Contact email address to be displayed on the validation page.
 
 expert_level
 Integer expert level that controls to detail of plots to be generated.
 
 refiners
 A list of additional refiner instances to be executed on top of the refiner methods that are members of this class.
 
 show_results
 Switch to show the result ROOT file in a TBrowser on terminate.
 
 stash
 stash of the harvested crops (start with those in the barn)
 
 raw_crops
 the dictionaries from peel as a numpy.array of doubles
 
 crops
 the dictionaries from peel
 

Static Public Attributes

int default_expert_level = 1
 The default value of expert_level if not specified explicitly by the caller.
 

Detailed Description

Python module to generate summary figures of merits, plots and/or trees
from on StoreArray.

It runs as a proper module in the main path and examines each object in a StoreArray
in each event.


Notes
-----
Methods to be overwritten
``prepare``
    Method called at the start of each event, that may prepare things
    (e.g. setup lookup tables or precomputed list) used in the following methods.
``pick``
    Method called with each object in the StoreArray.
    Returns a False value if the object should be skipped.

``peel``
    Method called with each object in the StoreArray.
    Extractes the parts relevant for analysis and
    returns them as MutableMapping (e.g. a dict) of part_name and values.
    Currently only float values or values convertible to floats are supported.
    If requested that can change in the future.

On termination all the collected values are recasted to numpy arrays and
the whole ``crops`` of the harvest are casted to MutableMapping of numpy.array
with the same part_name and the same MutableMapping class as returned from peel.

Also in the termination phase refiners a invoked with the aggregated crops.
Refiners can be given in two ways.

First way is as a class methods marked as refiners like

    @refiners.Refiner
    def plot(self, crops, tdirectory, **kwds):
        ...

where self is the module instance, crops is the MutableMapping of numpy arrays and tdirectory
is the current tdirectory to which the current output shall be written.
The additional kwds leave room for future additional arguments.

Second way is to define the refiner method (like plot) out of line and add it to
the harvesting module instance refiners list like harvesting_module.refiners.append(plot).

Other specialised decorators to mark a function as a Refiner such as

* refiners.filter
* refiners.select
* refiners.groupby

exist.

Predefined refiner functions exist in the refiners python module as well.
For instance

    save_tree = refiners.save_tree()

is a predefined method to output the MutableMapping of numpy arrays as a TTree.

Definition at line 93 of file harvesting.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  foreach,
  output_file_name,
  name = None,
  title = None,
  contact = None,
  expert_level = None,
  show_results = False 
)
Constructor of the harvesting module.

Parameters
----------
foreach : string
    Name of a StoreArray, which objects should be investigated
output_file_name : string
    Name of the ROOT file to which the results should be written.
    Giving an opened ROOT file is also allowed.
    If None is given write to the current ROOT file.
name : string, optional
    Name of the harvest that is used in the names of ROOT plots and trees.
    Defaults to the class name.
title : string, optional
    Name of the harvest that is used in the title of ROOT plots and trees.
    Defaults to the name.
contact : string, optional
    Contact email address to be used in the validation plots contact. Defaults to None.
expert_level : int, optional
    Expert level that can be used to switch on more plots.
    Generally the higher the more detailed to analysis.
    Meaning depends entirely on the subclass implementing a certain policy.
    Defaults to default_expert_level.
show_results : bool, optional
   Indicator to show the refined results at termination of the path

Reimplemented in MCTrajectoryHarvester, MCParticleHarvester, VxdCdcMergerHarvester, VxdCdcMergerHarvesterPRSide, SegmentFinderParameterExtractorModule, FitValidationModule, VxdCdcMergerHarvesterMCSide, VxdCdcPartFinderHarvester, SegmentFakeRatesModule, SeedsAnalyser, ReconstructionPositionHarvester, WrongRLInfoCounter, QueueHarvester, SummarizeTriggerResults, and SummarizeTriggerVariables.

Definition at line 158 of file harvesting.py.

165 show_results=False):
166 """Constructor of the harvesting module.
167
168 Parameters
169 ----------
170 foreach : string
171 Name of a StoreArray, which objects should be investigated
172 output_file_name : string
173 Name of the ROOT file to which the results should be written.
174 Giving an opened ROOT file is also allowed.
175 If None is given write to the current ROOT file.
176 name : string, optional
177 Name of the harvest that is used in the names of ROOT plots and trees.
178 Defaults to the class name.
179 title : string, optional
180 Name of the harvest that is used in the title of ROOT plots and trees.
181 Defaults to the name.
182 contact : string, optional
183 Contact email address to be used in the validation plots contact. Defaults to None.
184 expert_level : int, optional
185 Expert level that can be used to switch on more plots.
186 Generally the higher the more detailed to analysis.
187 Meaning depends entirely on the subclass implementing a certain policy.
188 Defaults to default_expert_level.
189 show_results : bool, optional
190 Indicator to show the refined results at termination of the path
191 """
192
193 super().__init__()
194
195
196 self.foreach = foreach
197
198
199 self.output_file_name = output_file_name
200
201 if not isinstance(self.output_file_name, (ROOT.TFile, str)):
202 raise TypeError("output_file_name is allowed to be a string or a ROOT.TFile object")
203
204
205 self.set_name(name or self.__class__.__name__)
206
207
208 self.title = title or self.name()
209
210
211 self.contact = contact
212
213
214 self.expert_level = self.default_expert_level if expert_level is None else expert_level
215
216
218 self.refiners = []
219
220
221 self.show_results = show_results
222

Member Function Documentation

◆ barn()

def barn (   self)
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()

def create_crop_part_collection ( )
static
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()

def event (   self)
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()

def gather (   self)
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.
Definition: PyStoreArray.h:72
static std::vector< std::string > list(DataStore::EDurability durability=DataStore::EDurability::c_Event)
Return list of available arrays for given durability.
Definition: PyStoreArray.cc:28
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()

def id (   self)
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()

def initialize (   self)
Initialisation method of the module.

Prepares the receiver stash of objects to be harvestered.

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()

def iter_store_obj (   store_obj)
static
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()

def peel (   self,
  crop 
)
Unpack the the instances and return and dictionary of names to values or
a generator of those dictionaries to be saved.

Returns
-------
dict(str -> float)
    Unpacked names and values
or

Yields
------
dict(str -> float)
    Unpacked names and values

Reimplemented in SeedsAnalyser, FitValidationModule, SegmentFakeRatesModule, SegmentFinderParameterExtractorModule, VxdCdcMergerHarvesterMCSide, MCParticleHarvester, VxdCdcMergerHarvester, MCTrajectoryHarvester, VxdCdcPartFinderHarvester, VxdCdcMergerHarvesterPRSide, SummarizeTriggerResults, ReconstructionPositionHarvester, WrongRLInfoCounter, and SummarizeTriggerVariables.

Definition at line 371 of file harvesting.py.

371 def peel(self, crop):
372 """Unpack the the instances and return and dictionary of names to values or
373 a generator of those dictionaries to be saved.
374
375 Returns
376 -------
377 dict(str -> float)
378 Unpacked names and values
379 or
380
381 Yields
382 ------
383 dict(str -> float)
384 Unpacked names and values
385
386 """
387 return {"name": np.nan}
388

◆ pick()

def pick (   self,
  crop 
)
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, MCParticleHarvester, 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()

def prepare (   self)
Default implementation of prepare.

Can be overridden by subclasses.

Reimplemented in SegmentFakeRatesModule, and FitValidationModule.

Definition at line 364 of file harvesting.py.

364 def prepare(self):
365 """Default implementation of prepare.
366
367 Can be overridden by subclasses.
368 """
369 return
370

◆ refine()

def refine (   self,
  crops 
)
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()

def terminate (   self)
Termination method of the module.

Finalize the collected crops.
Start the refinement.

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

◆ contact

contact

Contact email address to be displayed on the validation page.

Definition at line 211 of file harvesting.py.

◆ crops

crops

the dictionaries from peel

Definition at line 327 of file harvesting.py.

◆ default_expert_level

int default_expert_level = 1
static

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

Definition at line 156 of file harvesting.py.

◆ expert_level

expert_level

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

Definition at line 214 of file harvesting.py.

◆ foreach

foreach

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

Definition at line 196 of file harvesting.py.

◆ output_file_name

output_file_name

Name of the ROOT output file to be generated.

Definition at line 199 of file harvesting.py.

◆ raw_crops

raw_crops

the dictionaries from peel as a numpy.array of doubles

Definition at line 325 of file harvesting.py.

◆ refiners

refiners

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.

◆ show_results

show_results

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

Definition at line 221 of file harvesting.py.

◆ stash

stash

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

Definition at line 237 of file harvesting.py.

◆ title

title

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: