HIPO  4.3.0
High Performance Output data format for experimental physics
reaction.h
Go to the documentation of this file.
1 //******************************************************************
2 //* ██╗ ██╗██╗██████╗ ██████╗ ██╗ ██╗ ██████╗
3 //* ██║ ██║██║██╔══██╗██╔═══██╗ ██║ ██║ ██╔═████╗
4 //* ███████║██║██████╔╝██║ ██║ ███████║ ██║██╔██║
5 //* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║
6 //* ██║ ██║██║██║ ╚██████╔╝ ██║██╗╚██████╔╝
7 //* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚═╝╚═╝ ╚═════╝
8 //************************ Jefferson National Lab (2017) ***********
9 //******************************************************************
10 //* A simple library to do physics analysis directly from hipo file
11 //* Author: G.Gavalian
12 //* Date: 11/22/2023
13 //*--
14 
15 
18 
19 #ifndef __FIZIKA_REACTION__
20 #define __FIZIKA_REACTION__
21 
22 #include <cstdlib>
23 #include <iostream>
24 #include "reader.h"
25 #include "fizika.h"
26 
27 namespace fizika {
28 
30 
37 class reaction {
38 
39  private:
40  hipo::reader reader;
41  hipo::dictionary factory;
42  hipo::event event;
43 
44  fizika::lorentz4 lz4_beam,lz4_target, centermass;
45 
46  std::vector<hipo::bank> banks;
47  std::vector<std::pair<int,int>> filter;
48  bool filter_exclusive = true;
49 
50  void initialize(const char* file, std::vector<std::string> blist){
51  reader.open(file);
52  reader.readDictionary(factory);
53  for(decltype(blist)::size_type i = 0; i < blist.size(); i++) {
54  hipo::bank b(factory.getSchema("REC::Particle"));
55  banks.push_back(b);
56  }
57  }
58 
59  public:
60 
65  reaction(const char* file){
66  initialize(file,{"REC::Particle"});
67  }
71  reaction(const char *file, std::initializer_list<std::tuple<int,int> > desc){
72  initialize(file,{"REC::Particle"});
73  init_filter(desc);
74  }
79  reaction(const char *file, double benergy, std::initializer_list<std::tuple<int,int> > desc){
80  initialize(file,{"REC::Particle"});
81  init_filter(desc);
82  lz4_beam.setXYZM(0.0,0.0,benergy, 0.0005);
83  lz4_target.setXYZM(0.0,0.0,0.0,0.938);
84  centermass = lz4_beam + lz4_target;
85  }
86 
93  reaction(const char *file, double benergy, int *pids, int *count, int length){
94  initialize(file,{"REC::Particle"});
95  lz4_beam.setXYZM(0.0,0.0,benergy, 0.0005);
96  lz4_target.setXYZM(0.0,0.0,0.0,0.938);
97  centermass = lz4_beam + lz4_target;
98  printf(">>>> initializing the reaction\n");
99  for(int k = 0; k < length; k++){
100  filter.push_back(std::make_pair(pids[k],count[k]));
101  printf("push back -> %6d %6d\n",pids[k],count[k]);
102  }
103  }
104 
105  virtual ~reaction(){}
106 
107 
109  fizika::lorentz4 &cm(){return centermass;}
110 
113  void init_filter(std::initializer_list<std::tuple<int,int> > desc){
114  std::initializer_list< std::tuple<int,int>>::iterator it; // same as: const int* it
115  for ( it=desc.begin(); it!=desc.end(); ++it){
116  printf("PID %d, count %d\n", std::get<0>(*it),std::get<1>(*it));
117  filter.push_back(std::make_pair( std::get<0>(*it),std::get<1>(*it)));
118  //banks[counter].parse(std::get<0>(*it),std::get<1>(*it),std::get<2>(*it),std::get<3>(*it));
119  //counter++;
120  }
121  }
122 
129  fizika::lorentz4 get(std::initializer_list<std::tuple<int,int,int, double> > desc){
130  fizika::lorentz4 vec(0.0,0.0,0.0,0.0), temp;
131  std::initializer_list< std::tuple<int,int,int ,double>>::iterator it;
132  for ( it=desc.begin(); it!=desc.end(); ++it){
133  int ind = index(std::get<1>(*it), std::get<2>(*it));
134  if(ind>=0) {
135  get_vector(temp,std::get<3>(*it),banks[0],ind, 1,2,3);
136  if(std::get<0>(*it)<0) vec -= temp; else vec += temp;
137  }
138  }
139  return vec;
140  }
141 
148  fizika::lorentz4 get(int *signs, int *pids, int *skips, double *masses, int length){
149  fizika::lorentz4 vec(0.0,0.0,0.0,0.0), temp;
150  //printf("analyzing # %d counts \n", length);
151  for(int k = 0; k < length; k++){
152  int ind = index(pids[k],skips[k]);
153  if(ind>=0){
154  get_vector(temp,masses[k],banks[0],ind,1,2,3);
155  if(signs[k]<0) vec -= temp; else vec += temp;
156  }
157  }
158  //printf("final vector %f %f %f %f\n",vec.x(),vec.y(),vec.z(),vec.e());
159  return vec;
160  }
161 
164  bool next(){
165  bool status = reader.next();
166  if(status==false) return status;
167  reader.read(event);
168  for(decltype(banks)::size_type r = 0; r < banks.size(); r++) event.read(banks[r]);
169  return true;
170  }
171 
173  void get_vector(fizika::lorentz4 &vec, double mass, hipo::bank &b, int order, int ind_px, int ind_py, int ind_pz){
174  vec.setXYZM(b.getFloat(ind_px, order), b.getFloat(ind_py, order),b.getFloat(ind_pz, order),mass);
175  }
176 
179  bool is_valid(){
180  for(decltype(filter)::size_type f = 0; f < filter.size(); f++){
181  int count = countpid(filter[f].first);
182  //printf(" pid = %d, count = %d\n",filter[f].first, count);
183  if(filter_exclusive==true){
184  if(count!=filter[f].second) return false;
185  } else { if(count<filter[f].second) return false; }
186  }
187  return true;
188  }
189 
192  int countpid(int pid){
193  int counter = 0;
194  for(int r = 0; r < banks[0].getRows(); r++){
195  int upid = banks[0].getInt(0,r);
196  int stat = banks[0].getInt("status",r);
197  if(upid == pid)
198  if(abs(stat)>2000&&abs(stat)<3000) counter++;
199  }
200  return counter;
201  }
202 
207  int index(int pid, int skip){
208  int skipped = 0;
209  for(int r = 0; r < banks[0].getRows(); r++){
210  int upid = banks[0].getInt(0,r);
211  int stat = banks[0].getInt("status",r);
212  if(upid == pid)
213  if(abs(stat)>2000&&abs(stat)<3000){
214  if(skip==skipped) { return r; } else { skipped++;}
215  }
216  }
217  return -1;
218  }
219 
221  std::vector<hipo::bank> &getBanks(){return banks;}
223  fizika::lorentz4 &beam(){ return lz4_beam; }
225  fizika::lorentz4 &target(){ return lz4_target; }
226 };
227 }
228 #endif
Four-component Lorentz vector for relativistic kinematics.
Definition: fizika.h:213
void setXYZM(double _x, double _y, double _z, double _m)
Set components from momentum and mass; energy is computed.
Definition: fizika.h:269
Physics analysis helper for filtering and reconstructing reactions from HIPO data.
Definition: reaction.h:37
reaction(const char *file, double benergy, std::initializer_list< std::tuple< int, int > > desc)
Construct a reaction with beam energy and particle filter.
Definition: reaction.h:79
fizika::lorentz4 & beam()
Definition: reaction.h:223
void get_vector(fizika::lorentz4 &vec, double mass, hipo::bank &b, int order, int ind_px, int ind_py, int ind_pz)
Fill a Lorentz vector from a bank row's momentum columns and a given mass.
Definition: reaction.h:173
reaction(const char *file)
Construct a reaction from a HIPO file (no filter, no beam setup).
Definition: reaction.h:65
bool next()
Advance to the next event and read all banks.
Definition: reaction.h:164
fizika::lorentz4 get(int *signs, int *pids, int *skips, double *masses, int length)
Reconstruct a Lorentz vector from arrays of particle parameters.
Definition: reaction.h:148
fizika::lorentz4 & cm()
Definition: reaction.h:109
int index(int pid, int skip)
Find the row index of the Nth particle with the given PID.
Definition: reaction.h:207
void init_filter(std::initializer_list< std::tuple< int, int > > desc)
Initialize the particle filter from PID/count pairs.
Definition: reaction.h:113
bool is_valid()
Check if the current event passes the particle filter.
Definition: reaction.h:179
int countpid(int pid)
Count particles with the given PID that pass status cuts.
Definition: reaction.h:192
reaction(const char *file, std::initializer_list< std::tuple< int, int > > desc)
Construct a reaction with a particle filter.
Definition: reaction.h:71
reaction(const char *file, double benergy, int *pids, int *count, int length)
Construct a reaction with beam energy and C-style filter arrays.
Definition: reaction.h:93
std::vector< hipo::bank > & getBanks()
Definition: reaction.h:221
virtual ~reaction()
Definition: reaction.h:105
fizika::lorentz4 & target()
Definition: reaction.h:225
fizika::lorentz4 get(std::initializer_list< std::tuple< int, int, int, double > > desc)
Reconstruct a Lorentz vector from a particle combination.
Definition: reaction.h:129
reaction()
Default constructor.
Definition: reaction.h:62
Represents a HIPO bank, a tabular data structure with rows and typed columns.
Definition: bank.h:352
float getFloat(int item, int index) const noexcept
Definition: bank.h:686
Collection of schema definitions, typically read from a HIPO file header.
Definition: dictionary.h:248
schema & getSchema(const char *name)
Retrieve a schema by name.
Definition: dictionary.h:271
Represents a HIPO event, a container for multiple structures/banks.
Definition: event.h:77
void read(hipo::bank &b)
Read a bank from this event (alias for getStructure).
Definition: event.cpp:67
Sequential reader for HIPO files.
Definition: reader.h:250
void read(hipo::event &dataevent)
Read the current event into the given event object.
Definition: reader.cpp:233
void readDictionary(hipo::dictionary &dict)
Read the schema dictionary from the file header.
Definition: reader.cpp:311
bool next()
Advance to the next event.
Definition: reader.cpp:334
void open(const char *filename)
Open a HIPO file for reading.
Definition: reader.cpp:81
Vector physics library including 3D and Lorentz 4-vectors for relativistic kinematics.
Definition: fizika.h:39
Sequential and random-access reader for HIPO files with event filtering and dictionary support.