BmnRoot 26.10.0-18-g4e3414f9
Software framework of the BM@N experiment
Loading...
Searching...
No Matches
BmnParticleEqualizer1D.h
Go to the documentation of this file.
1// -------------------------------------------------------------------------
2// ----- BmnParticleEqualizer1D header file -----
3// ----- Created 01/05/2024 by K. Gertsenberger -----
4// -------------------------------------------------------------------------
5
6/** BmnParticleEqualizer1D.h
7 ** @author Konstantin Gertsenberger <gertsen@jinr.ru>
8 **
9 ** The BmnParticleEqualizer1D add particles to equalize their number for all momentum intervals
10 **
11 ** The generator counts the particles already put on the stack by the other generators of the event and adds new ones
12 ** to equalize their number, so it MUST be registered in FairPrimaryGenerator as the LAST generator (otherwise it
13 ** would not see the particles of the generators registered after it). The requirement is verified in ReadEvent().
14 **
15 ** The number of the particles of every type is equalized inside every momentum interval independently, up to the
16 ** maximum number among the types found in the interval (the intervals themselves are not equalized with each other).
17 **
18 ** The momentum, the pseudorapidity and the azimuth angle of a new particle are sampled from the ONE dimensional
19 ** sample histograms of its momentum interval, i.e. independently of each other: the correlations between the values
20 ** inside the interval are not kept, use BmnParticleEqualizer2D if they matter. The angles are nevertheless distributed
21 ** as in the sample, i.e. inside the detector acceptance, and not uniformly.
22 **
23 ** A file with the sample histograms produced by BmnParticleEqualizer2D can be read by this class as well (it is a
24 ** superset), while a file produced by this class does not contain the two dimensional histograms needed by
25 ** BmnParticleEqualizer2D.
26 */
27
28#ifndef BMNPARTICLEEQUALIZER1D_H
29#define BMNPARTICLEEQUALIZER1D_H
30
31#include "BmnGlobalTrack.h"
32#include "BmnTrackMatch.h"
33#include "FairGenerator.h"
34#include "FairPrimaryGenerator.h"
35#include "TClonesArray.h"
36#include "TF1.h"
37#include "TFile.h"
38#include "TH1D.h"
39
40#include <map>
41#include <memory>
42#include <vector>
43using namespace std;
44
46{
47 public:
48 /** Constructor with input histograms **/
49 BmnParticleEqualizer1D(TString hist_file_name);
50 /** Constructor with given PDG codes for particles to be equalized
51 **@param pdg_codes Particles to be equalized (PDG codes)
52 **@param min_p Min value of momentum value
53 **@param max_p Max value of momentum value
54 **@param intervals Interval count for momentum values to equalize particles at all the intervals independently
55 **/
56 BmnParticleEqualizer1D(TString hist_file_name,
57 vector<Int_t> pdg_codes,
58 Double_t min_p = 0.0,
59 Double_t max_p = 4.0,
60 Int_t intervals = 20);
61
62 /** Destructor */
64
65 /** Initializer */
66 Bool_t Init();
67 /** Creates an event with equal number of particles at all momentum intervals
68 **@param primGen pointer to the FairPrimaryGenerator
69 */
70 virtual Bool_t ReadEvent(FairPrimaryGenerator* primGen);
71
73 {
74 // Default 'stParticleInfo' constructor for storing particle distrubutons
75 stParticleInfo(int pdg_key, Double_t min_p, Double_t max_p, Int_t interval_count);
76 // 'stParticleInfo' destructor, empty becausse of using unique pointers
78
79 vector<Int_t> fParticleNumber; ///< PDG -> Particle Count (divided by momentum intervals -> vector)
80 /// Part of the target number of the particles that has not been added yet, kept between the events so that a
81 /// fractional target is reached on average (divided by momentum intervals -> vector)
82 vector<Double_t> fParticleDebt;
83 /// Relative reconstruction efficiency of the particle type, 1 for all the intervals if the efficiency map is
84 /// absent in the file with the sample histograms (divided by momentum intervals -> vector)
85 vector<Double_t> fEfficiency;
86
87 unique_ptr<TH1D> fPHist; ///< PDG -> P histogram (TH1D) for the particle type
88 unique_ptr<TH1D> fPtHist; ///< PDG -> Pt histogram (TH1D) for the particle type
89 unique_ptr<TH1D> fYHist; ///< PDG -> Y (rapidity) histogram (TH1D) for the particle type
90 unique_ptr<TH1D> fEtaHist; ///< PDG -> Eta (pseudorapidity) histogram (TH1D) for the particle type
91 unique_ptr<TH1D> fThetaHist; ///< PDG -> Theta (polar angle) histogram (TH1D) for the particle type
92 unique_ptr<TH1D> fPhiHist; ///< PDG -> Phi (azimuth angle) histogram (TH1D) for the particle type
93
94 vector<unique_ptr<TH1D>>
95 fPHistVector; ///< P histograms for the particle type (divided by momentum intervals -> vector)
96 vector<unique_ptr<TH1D>>
97 fEtaHistVector; ///< Eta histograms for the particle type (divided by momentum intervals -> vector)
98 vector<unique_ptr<TH1D>>
99 fPhiHistVector; ///< Phi histograms for the particle type (divided by momentum intervals -> vector)
100
101 /// Way the momentum of a new particle is sampled in a momentum interval
102 enum class EPSampling
103 {
104 kNone, ///< the interval cannot be sampled at all, new particles are not added there
105 kIntervalHist, ///< from the P histogram of the interval itself (there is enough sample statistics)
106 kFitFunction ///< from the fitted function of the full P histogram (too little sample statistics)
107 };
108 vector<EPSampling>
109 fPSampling; ///< Momentum sampling way for the particle type (divided by momentum intervals -> vector)
110
111 /// Index of the momentum interval whose Eta histogram is used to sample the pseudorapidity for a given interval
112 /// (the interval itself when it has enough statistics, otherwise the nearest one that has), -1 if there is none
113 vector<Int_t> fEtaHistIndex;
114 /// The same for the azimuth angle: index of the momentum interval whose Phi histogram is used
115 vector<Int_t> fPhiHistIndex;
116
117 Double_t fY0; ///< Mean rapidity
118 Double_t fSigma; ///< Sigma in the rapidity distribution
119 Double_t fT; ///< Temperature in the P distribution
120 };
121
122 /** Status bit the generator sets on every particle it adds. TObject keeps the bits 0-13 of fBits for itself, so
123 ** 14 is the first one free for the user. CbmMCTrack copies these bits from TParticle, so the mark reaches the
124 ** reconstructed (DST) data and the added particles can be told there from the ones of the physics generators:
125 ** mc_track->TestBit(kAddedParticleBit) **/
126 enum
127 {
129 };
130
131 /** Set the target number of the particles of every type to be reached in every momentum interval of an event.
132 ** Zero (default) keeps the original behaviour: the target of an interval is the maximum number among the particle
133 ** types found in that interval of that event, so the numbers are equalized between the types but the momentum
134 ** spectrum stays the one of the input events. A positive value makes the target the same for all the intervals and
135 ** all the types, which gives a flat distribution both in the particle type and in the momentum - what is usually
136 ** needed to train a PID. The value may be fractional (and normally is: it is the number of particles per event per
137 ** interval, i.e. the wanted sample size divided by the number of events and by the number of intervals), the parts
138 ** below one particle are accumulated between the events **/
140 /** Switch on the correction of the number of the added particles by the reconstruction efficiency (off by
141 ** default). The efficiency map has to be present in the file with the sample histograms, see ProduceEfficiencyMap.
142 ** When it is on, the number of the particles added to an interval is divided by the relative efficiency of that
143 ** interval, compensating for the added particles lost at the reconstruction. 'min_efficiency' limits the resulting
144 ** factor: the intervals with a lower efficiency are corrected as if it were equal to it **/
145 Bool_t SetEfficiencyCorrection(Bool_t is_used, Double_t min_efficiency = 0.05);
146
147 /** Set the cut applied to the tracks of the sample while producing the sample histograms (and while drawing the
148 ** resulting distributions by ShowResultDistributions): 0 - all the primary particles are taken, 1 - the tracks
149 ** without points in Silicon and GEM detectors are excluded, 2 (default) - the tracks without points in Silicon,
150 ** GEM and ToF detectors are excluded. It defines the acceptance the sample histograms correspond to, so it has to
151 ** be set before ProduceSampleHistograms() and cannot be changed for an already produced file. The reconstructed
152 ** tracks counted by ShowResultDistributions and ProduceEfficiencyMap are selected in the same acceptance: the cut 2
153 ** requires a ToF hit matched to the track **/
154 static void SetSampleTrackCut(Int_t track_cut) { fSampleTrackCut = track_cut; }
155 /** Switch the filtering of the event particles by the sample statistics on or off (on by default). When it is on,
156 ** the particles of the event lying in the phase space regions where the sample histograms are empty are not
157 ** counted: the generator cannot add such particles back, so counting them would leave the numbers unequalized **/
159
160 // static function to build the map of the reconstruction efficiency from the reconstructed (DST) files listed in
161 // the given text file and to add it to the file with the sample histograms. Both the simulated and the
162 // experimental DST files can be given: the particle type of a track is taken from the MC track matched to it when
163 // it is available and is defined by the mass measured by the ToF detectors and by the sign of the charge
164 // otherwise. The map is the number of the reconstructed tracks of a particle type in a momentum interval divided
165 // by the number of the sample tracks in the same interval, normalized so that its maximum is 1, i.e. it is a
166 // RELATIVE efficiency
167 // return 0 if the map has been built successfully, a negative error code otherwise
168 static int ProduceEfficiencyMap(TString input_list_file,
169 TString histo_file = "$VMCWORKDIR/macro/recotools/particle_hists.root");
170
171 // static function to form sample histograms for equalizing the particle distributions
172 // return 0 if the histograms have been produced successfully, a negative error code otherwise
173 static int ProduceSampleHistograms(TString input_list_file,
174 TString output_histo_file = "$VMCWORKDIR/macro/recotools/particle_hists.root");
175
176 // fit P. Pt, Y, Eta histograms to generate the values according to the analytical functions
177 static int FitPHistogram(unique_ptr<TH1D>& hP, int pdg_key, Double_t p_min, Double_t p_max);
178 static int FitPtHistogram(unique_ptr<TH1D>& hPt,
179 int pdg_key,
180 Double_t pdg_mass,
181 Double_t pt_min,
182 Double_t pt_max,
183 Double_t& t_output);
184 static int FitYHistogram(unique_ptr<TH1D>& hY, int pdg_key, Double_t& y0_output, Double_t& sigma_putput);
185 static int FitEtaHistogram(unique_ptr<TH1D>& hEta, int pdg_key, Double_t eta_min, Double_t eta_max);
186
187 /** additional functions to check results of fitting and particle equalizing **/
188 // static function to fit the obtained sample histograms (to show whether the results are appropriate)
189 static void FitSampleHistograms(TString input_histo_file = "$VMCWORKDIR/macro/recotools/particle_hists.root");
190 // static function to show the obtained sample histograms with obtained fit (to check whether the results are
191 // appropriate)
192 static int ShowSampleHistograms(TString input_histo_file = "$VMCWORKDIR/macro/recotools/particle_hists.root");
193 // draw histogram with multiplicity of different particle types for all the momentum intervals
194 // as well as P, Pt, Y, Eta, Theta, Phi distributions after the equalizing
195 // it work both for SIM and DST files listed in the given text file ('input_list_file' var)
196 // return 0 if the distributions have been drawn successfully, a negative error code otherwise
197 static int ShowResultDistributions(TString input_list_file);
198
199 // none of the data members below is written to a ROOT file: they are all rebuilt from the file with the sample
200 // histograms at every start, so they are marked as transient ('//!') to keep them out of the generated streamer
201 // (the histograms are held by unique pointers, which cannot be streamed at all)
202 private:
203 static Int_t fSampleTrackCut; ///< Cut applied to the tracks of the sample (see SetSampleTrackCut)
204 Double_t fTargetPerInterval; //! Target particle number per interval, 0 = maximum of the event (see the setter)
205 Bool_t fUseEfficiencyCorrection; //! Whether the added particle number is corrected by the efficiency
206 Double_t fMinEfficiency; //! Lower limit of the efficiency used for the correction
207 Bool_t fIsEfficiencyMapRead; //! Whether the efficiency map has been found in the sample histogram file
208 /// Whether the particles of the event are filtered by the sample statistics (see SetSampleStatisticsCut)
210
211 static Double_t fPMin; ///< Min value of momentum
212 static Double_t fPMax; ///< Max value of momentum
213 static Int_t fIntervalCount; ///< Interval count for momentum to equalize particles at all the intervals
214 Double_t fIntervalStep; //! Interval step
215
216 // Whether correct input data for the generator is obtained. If false, the simulation run is stopped: running the
217 // equalizing generator is a rare and critical case, so silently skipping it would spoil the whole produced data set
219 // Particle PDG codes to be equalized
220 static vector<Int_t> fParticles;
221 // map with the full information on all the particle types
222 map<Int_t, unique_ptr<stParticleInfo>> fParticleInfos; //!
223 // Maximum number of a particle type among all particles (divided by P intervals -> vector)
224 vector<int> fMaxParticles; //!
225
226 // Get all sample histograms for all the particle types from the 'input_histo_file' file
227 // and calculate fit parameters (if necessary) according to the given histograms
228 Bool_t ReadSampleHistograms(TString input_histo_file = "$VMCWORKDIR/macro/recotools/particle_hists.root");
229
230 // verify that the momentum grid the sample histograms in the given file were produced with is exactly the one
231 // this class is configured for. The histograms of the intervals are read by name only, so a shifted grid is not
232 // detected by itself and would silently refer the values of an interval to a different momentum range
233 static Bool_t CheckMomentumGrid(TFile* hist_file);
234
235 // for every momentum interval define the index of the interval histogram to be used to sample a value: the
236 // interval itself if it has enough statistics, otherwise the nearest interval that has it (so the value stays
237 // tied to a close momentum region instead of being taken from the whole momentum range)
238 // the element is set to -1 if no interval has statistics at all
239 static void DefineNearestFilledIntervals(const vector<unique_ptr<TH1D>>& hist_vector, vector<Int_t>& index_vector);
240
241 // define PDG code of a particle by the given mass
242 // among particle types set by 'vecParticlePDG' vector or in the TDatabasePDG if the vector is empty
243 // tolerance: relative if tolerance > 0, absolute (in GeV) in case of tolerance <= 0
244 // return PDG code or 0 if no corresponding particle was found
245 static Int_t GetPDGCodeByMass(Double_t mass, Double_t tolerance = 1.e-6, const vector<int>& vecParticlePDG = {});
246
247 // define the particle type of a reconstructed (DST) track. For the simulated data ('mc_tracks' is given) it is the
248 // type of the related MC track: the one referenced by the global track itself (BmnGlobalTrack::GetRefIndex()) or,
249 // if the reference is not set, the one matched to it ('global_match'). Otherwise, as well as for a simulated track
250 // without any matching information, the type is defined by the mass measured by the ToF detectors and by the sign
251 // of the charge, as the mass alone does not distinguish a particle from its antiparticle. 'tof_mass' is set to the
252 // measured mass in the latter case and to -1 otherwise
253 // return the PDG code or 0 if the type cannot be defined
254 static Int_t GetGlobalTrackPDG(BmnGlobalTrack* global_track,
255 BmnTrackMatch* global_match,
256 TClonesArray* mc_tracks,
257 Double_t& tof_mass);
258
260};
261
262#endif
const Handle_t kNone
bool Bool_t
int Int_t
double Double_t
#define ClassDef(name, id)
#define BIT(n)
BmnGlobalTrack.h.
void SetSampleStatisticsCut(Bool_t is_used)
Switch the filtering of the event particles by the sample statistics on or off (on by default).
static int FitPHistogram(unique_ptr< TH1D > &hP, int pdg_key, Double_t p_min, Double_t p_max)
static void SetSampleTrackCut(Int_t track_cut)
Set the cut applied to the tracks of the sample while producing the sample histograms (and while draw...
static int ProduceEfficiencyMap(TString input_list_file, TString histo_file="$VMCWORKDIR/macro/recotools/particle_hists.root")
Bool_t fUseSampleStatisticsCut
Whether the efficiency map has been found in the sample histogram file.
static Int_t GetPDGCodeByMass(Double_t mass, Double_t tolerance=1.e-6, const vector< int > &vecParticlePDG={})
static Int_t fSampleTrackCut
Cut applied to the tracks of the sample (see SetSampleTrackCut).
static int FitYHistogram(unique_ptr< TH1D > &hY, int pdg_key, Double_t &y0_output, Double_t &sigma_putput)
Double_t fMinEfficiency
Whether the added particle number is corrected by the efficiency.
void SetTargetPerInterval(Double_t target)
Set the target number of the particles of every type to be reached in every momentum interval of an e...
static int ShowSampleHistograms(TString input_histo_file="$VMCWORKDIR/macro/recotools/particle_hists.root")
static Int_t GetGlobalTrackPDG(BmnGlobalTrack *global_track, BmnTrackMatch *global_match, TClonesArray *mc_tracks, Double_t &tof_mass)
static int ShowResultDistributions(TString input_list_file)
static Double_t fPMin
Min value of momentum.
Bool_t fUseEfficiencyCorrection
Target particle number per interval, 0 = maximum of the event (see the setter).
static Bool_t CheckMomentumGrid(TFile *hist_file)
Bool_t fIsEfficiencyMapRead
Lower limit of the efficiency used for the correction.
static void FitSampleHistograms(TString input_histo_file="$VMCWORKDIR/macro/recotools/particle_hists.root")
additional functions to check results of fitting and particle equalizing
static void DefineNearestFilledIntervals(const vector< unique_ptr< TH1D > > &hist_vector, vector< Int_t > &index_vector)
static int ProduceSampleHistograms(TString input_list_file, TString output_histo_file="$VMCWORKDIR/macro/recotools/particle_hists.root")
Bool_t ReadSampleHistograms(TString input_histo_file="$VMCWORKDIR/macro/recotools/particle_hists.root")
Bool_t SetEfficiencyCorrection(Bool_t is_used, Double_t min_efficiency=0.05)
Switch on the correction of the number of the added particles by the reconstruction efficiency (off b...
static int FitEtaHistogram(unique_ptr< TH1D > &hEta, int pdg_key, Double_t eta_min, Double_t eta_max)
static Int_t fIntervalCount
Interval count for momentum to equalize particles at all the intervals.
virtual ~BmnParticleEqualizer1D()
Destructor.
static Double_t fPMax
Max value of momentum.
static vector< Int_t > fParticles
Bool_t isActive
Interval step.
map< Int_t, unique_ptr< stParticleInfo > > fParticleInfos
static int FitPtHistogram(unique_ptr< TH1D > &hPt, int pdg_key, Double_t pdg_mass, Double_t pt_min, Double_t pt_max, Double_t &t_output)
BmnParticleEqualizer1D(TString hist_file_name)
Constructor with input histograms.
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen)
Creates an event with equal number of particles at all momentum intervals.
Definition json.hpp:4446
vector< Double_t > fEfficiency
Relative reconstruction efficiency of the particle type, 1 for all the intervals if the efficiency ma...
unique_ptr< TH1D > fPhiHist
PDG -> Phi (azimuth angle) histogram (TH1D) for the particle type.
unique_ptr< TH1D > fThetaHist
PDG -> Theta (polar angle) histogram (TH1D) for the particle type.
unique_ptr< TH1D > fEtaHist
PDG -> Eta (pseudorapidity) histogram (TH1D) for the particle type.
vector< EPSampling > fPSampling
Momentum sampling way for the particle type (divided by momentum intervals -> vector).
vector< Double_t > fParticleDebt
Part of the target number of the particles that has not been added yet, kept between the events so th...
stParticleInfo(int pdg_key, Double_t min_p, Double_t max_p, Int_t interval_count)
vector< unique_ptr< TH1D > > fEtaHistVector
Eta histograms for the particle type (divided by momentum intervals -> vector).
Double_t fT
Temperature in the P distribution.
unique_ptr< TH1D > fPtHist
PDG -> Pt histogram (TH1D) for the particle type.
vector< Int_t > fParticleNumber
PDG -> Particle Count (divided by momentum intervals -> vector).
Double_t fSigma
Sigma in the rapidity distribution.
vector< Int_t > fEtaHistIndex
Index of the momentum interval whose Eta histogram is used to sample the pseudorapidity for a given i...
EPSampling
Way the momentum of a new particle is sampled in a momentum interval.
@ kIntervalHist
from the P histogram of the interval itself (there is enough sample statistics)
@ kFitFunction
from the fitted function of the full P histogram (too little sample statistics)
unique_ptr< TH1D > fPHist
PDG -> P histogram (TH1D) for the particle type.
unique_ptr< TH1D > fYHist
PDG -> Y (rapidity) histogram (TH1D) for the particle type.
vector< unique_ptr< TH1D > > fPHistVector
P histograms for the particle type (divided by momentum intervals -> vector).
vector< Int_t > fPhiHistIndex
The same for the azimuth angle: index of the momentum interval whose Phi histogram is used.
vector< unique_ptr< TH1D > > fPhiHistVector
Phi histograms for the particle type (divided by momentum intervals -> vector).