SQLamarr
The stand-alone ultra-fast simulation option for the LHCb experiment
HepMC2DataLoader.cpp
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 
11 // STL
12 #include <iostream>
13 #include <algorithm>
14 
15 // Local
16 #include "SQLamarr/HepMC2DataLoader.h"
17 #include "SQLamarr/db_functions.h"
18 #include "SQLamarr/preprocessor_symbols.h"
19 
20 namespace SQLamarr
21 {
22  //==========================================================================
23  // load
24  //==========================================================================
26  const std::string& file_path,
27  size_t run_number,
28  size_t evt_number
29  )
30  {
31  begin_transaction();
32  const int ds_id = insert_event(file_path, run_number, evt_number);
33 
34  HepMC3::ReaderAsciiHepMC2 reader(file_path.c_str());
35  while ( !reader.failed() )
36  {
37  HepMC3::GenEvent evt(HepMC3::Units::MEV, HepMC3::Units::MM);
38  reader.read_event(evt);
39 
40  auto pos = evt.event_pos();
41  const int event_id = insert_collision(
42  ds_id,
43  evt.event_number(),
44  pos.t(),
45  pos.x(),
46  pos.y(),
47  pos.z()
48  );
49 
50  std::vector<int> pvs;
51  for (auto& bp: evt.beams())
52  if (bp->end_vertex())
53  pvs.push_back(bp->end_vertex()->id());
54 
55  std::unordered_map<int, int> vtxid_mapping;
56  for (auto vertex: evt.vertices())
57  vtxid_mapping[vertex->id()] = insert_vertex(
58  event_id,
59  vertex->id(),
60  vertex->status(),
61  vertex->position().t(),
62  vertex->position().x(),
63  vertex->position().y(),
64  vertex->position().z(),
65  (std::find(pvs.begin(), pvs.end(), vertex->id()) != pvs.end())
66  );
67 
68  for (auto particle: evt.particles())
69  {
70  auto pv = particle->production_vertex();
71  auto ev = particle->end_vertex();
72 
73 
74  insert_particle(
75  event_id,
76  particle->id(),
77  (pv ? vtxid_mapping[pv->id()] : LAMARR_BAD_INDEX),
78  (ev ? vtxid_mapping[ev->id()] : LAMARR_BAD_INDEX),
79  particle->pid(),
80  particle->status(),
81  particle->momentum().e(),
82  particle->momentum().px(),
83  particle->momentum().py(),
84  particle->momentum().pz(),
85  particle->generated_mass()
86  );
87  }
88  }
89 
90  end_transaction();
91  }
92 }
93 
def load(self, str filename, int runNumber, int evtNumber)
Loads an ASCII file with HepMC3::ReaderAsciiHepMC2.