SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
PyTransformer.py
1 # (c) Copyright 2022 CERN for the benefit of the LHCb Collaboration.
2 #
3 # This software is distributed under the terms of the GNU General Public
4 # Licence version 3 (GPL Version 3), copied verbatim in the file "LICENCE".
5 #
6 # In applying this licence, CERN does not waive the privileges and immunities
7 # granted to it by virtue of its status as an Intergovernmental Organization
8 # or submit itself to any jurisdiction.
9 
10 from SQLamarr.db_functions import SQLite3DB
11 
13  """
14  Decorator transforming a python function in a Transformer that can be
15  pipelined to C++ algorithms in a `SQLamarr.Pipeline`.
16 
17  Example.
18  ```python
19  import SQLamarr
20  import pandas as pd
21 
22  db = SQLamarr.SQLite3DB().seed(42)
23 
24  loader = SQLamarr.HepMC2DataLoader(db)
25 
26  pv_finder = SQLamarr.PVFinder(db)
27  mcps = SQLamarr.MCParticleSelector(db)
28 
29  @SQLamarr.PyTransformer(db)
30  def selector (connection):
31  df = pd.read_sql_query("SELECT * FROM MCParticles", connection)
32  df = df[df.pz > 10000] # Apply a selection
33  df.to_sql("MCParticles", connection, if_exists='replace')
34 
35  pipeline = SQLamarr.Pipeline((pv_finder, mcps, selector))
36 
37  loader.load("my_file_0.mc2", 42, 0)
38  pipeline.execute()
39 
40  loader.load("my_file_1.mc2", 42, 1)
41  pipeline.execute()
42 
43  ```
44 
45 
46  """
47  def __init__(self, db: SQLite3DB):
48  """@private: Acquire the database connection"""
49  self._db_db = db
50 
51  def __call__(self, f):
52  """@private: Wrap a function in an algorithm"""
53  def wrapped():
54  with self._db_db.connect() as c:
55  f (c)
56  return wrapped
57 
Decorator transforming a python function in a Transformer that can be pipelined to C++ algorithms in ...