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