SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
HepMC2DataLoader.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 import ctypes
11 import os
12 from ctypes import POINTER
13 from SQLamarr import clib
14 import sqlite3
15 import contextlib
16 
17 from SQLamarr.db_functions import SQLite3DB
18 
19 clib.new_HepMC2DataLoader.argtypes = (ctypes.c_void_p,)
20 clib.new_HepMC2DataLoader.restype = ctypes.c_void_p
21 
22 clib.del_HepMC2DataLoader.argtypes = (ctypes.c_void_p,)
23 
24 clib.HepMC2DataLoader_load.argtypes = (
25  ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_size_t
26  )
27 
29  """
30  Data loader for HepMC2-format ASCII files.
31 
32  Example.
33  ```python
34  # Create the database connection
35  db = SQLamarr.SQLite3DB()
36 
37  # Create the data loader
38  data_loader = HepMCDataLoader(db)
39 
40  # Create the pipeline with a CleanEventStore algorithm
41  clean_all = SQLamarr.Pipeline([
42  SQLamarr.CleanEventStore(db)
43  ])
44 
45  # For each file in an input file list,
46  for file in my_list_of_files:
47  # load the file
48  data_loader.load(file)
49  # ...do something...
50 
51  # clean the database
52  clean_all.execute()
53  ```
54  """
55  def __init__(self, db: SQLite3DB):
56  """Acquires the reference to an open connection to the DB"""
57  self._db_db = db
58 
59  def load(self, filename: str, runNumber: int, evtNumber: int):
60  """Loads an ASCII file with
61  [HepMC3::ReaderAsciiHepMC2](http://hepmc.web.cern.ch/hepmc/classHepMC3_1_1ReaderAsciiHepMC2.html).
62  """
63  if not os.path.exists(filename):
64  raise FileNotFoundError(filename)
65 
66  _self = clib.new_HepMC2DataLoader(self._db_db.get())
67  clib.HepMC2DataLoader_load(
68  _self, filename.encode('ascii'), runNumber, evtNumber, self._db_db.path.encode('ascii')
69  )
70  clib.del_HepMC2DataLoader(_self)
Data loader for HepMC2-format ASCII files.
def __init__(self, SQLite3DB db)
Acquires the reference to an open connection to the DB.
def load(self, str filename, int runNumber, int evtNumber)
Loads an ASCII file with HepMC3::ReaderAsciiHepMC2.