KaliVeda
Toolkit for HIC analysis
KVIDZAFromZGridBatchMassCorrector.cpp
1 #include "KVIDZAFromZGridBatchMassCorrector.h"
2 #include "KVIDZAFromZGrid.h"
3 #include "KVIDZAFromZGridMassCorrector.h"
4 #include "KVGaussFitMassModifier.h"
5 #include "KVMultiGaussIsotopeFit.h"
6 
7 
12 
14  const TString& bad_grids_id_map_file
15  )
16  : reference_id_maps{bad_grids_id_map_file}
17 {
18  // \param[in] ref_grids_file name/path to file containing 'reference' grids
19  // \param[in] bad_grids_file name/path to file containing grids to be corrected wrt 'reference' grids
20  // \param[in] bad_grids_id_map_file name/path to file containing 2d identification maps for the 'bad' (to be corrected) grids
21 
22  KVIDGraph::SetAutoAdd(false);
23  if(reference_grids.ReadAsciiFile(ref_grids_file))
24  {
25  reference_grids.AddLastReadGrids();
26  reference_grids.SetName(ref_grids_file);
27  KVError::Info(this, "KVIDZAFromZGridBatchMassCorrector",
28  "Read %d reference grids from file %s",
29  reference_grids.GetGrids()->GetEntries(),
30  reference_grids.GetName());
31  }
32  else
33  KVError::Error(this, "KVIDZAFromZGridBatchMassCorrector",
34  "Could not read file %s",
35  ref_grids_file.Data());
36 
37  if(grids_to_correct.ReadAsciiFile(bad_grids_file))
38  {
39  grids_to_correct.AddLastReadGrids();
40  grids_to_correct.SetName(bad_grids_file);
41  KVError::Info(this, "KVIDZAFromZGridBatchMassCorrector",
42  "Read %d grids to correct from file %s",
43  grids_to_correct.GetGrids()->GetEntries(),
44  grids_to_correct.GetName());
45  }
46  else
47  KVError::Error(this, "KVIDZAFromZGridBatchMassCorrector",
48  "Could not read file %s",
49  bad_grids_file.Data());
50 }
51 
52 
53 
57 
59  : reference_id_maps{ref_id_map_file}
60 {
61  // \param[in] ref_grids_file name/path to file containing 'reference' grids
62  // \param[in] ref_id_map_file name/path to file containing 2d identification maps for the 'reference' grids
63 
64  KVIDGraph::SetAutoAdd(false);
65  if(reference_grids.ReadAsciiFile(ref_grids_file))
66  {
67  reference_grids.AddLastReadGrids();
68  reference_grids.SetName(ref_grids_file);
69  KVError::Info(this, "KVIDZAFromZGridBatchMassCorrector",
70  "Read %d reference grids from file %s",
71  reference_grids.GetGrids()->GetEntries(),
72  reference_grids.GetName());
73  }
74  else
75  KVError::Error(this, "KVIDZAFromZGridBatchMassCorrector",
76  "Could not read file %s",
77  ref_grids_file.Data());
78 }
79 
80 
81 
83 
85  std::function<KVString (const KVString&)> deduce_ref_grid_name_from_bad_grid_name,
86  const KVNameValueList& mass_fit_parameters, double min_yield_cutoff,
87  std::function<KVString (const KVString&)> deduce_bad_histo_name)
88 {
89  KVError::Info(this, "CorrectGrid", "Treating grid : %s", bad_grid->GetName());
90 
91  // deduce name of reference grid
92  auto ref_grid_name = deduce_ref_grid_name_from_bad_grid_name(bad_grid->GetName());
93 
94  // get reference grid
95  KVIDZAFromZGrid* ref_grid = dynamic_cast<KVIDZAFromZGrid*>(reference_grids.GetGrid(ref_grid_name));
96  if(!ref_grid)
97  {
98  KVError::Info(this, "CorrectGrids",
99  "No reference grid %s in file %s to compare with grid %s",
100  ref_grid_name.Data(), reference_grids.GetName(), bad_grid->GetName());
101  return;
102  }
103 
104  CorrectGridToReference(bad_grid, ref_grid, mass_fit_parameters, min_yield_cutoff, deduce_bad_histo_name);
105 }
106 
107 
108 
111 
112 void KVIDZAFromZGridBatchMassCorrector::CorrectGridToReference(KVIDZAFromZGrid* bad_grid, KVIDZAFromZGrid* ref_grid, const KVNameValueList& mass_fit_parameters, double min_yield_cutoff, std::function<KVString (const KVString&)> deduce_bad_histo_name)
113 {
114  // determine & apply corrections for grid
115  KVIDZAFromZGridMassCorrector toto(ref_grid, bad_grid);
116  auto Correct = toto.DetermineMassCorrections(min_yield_cutoff);
117  if(Correct.size())
118  {
119  if(apply_corrections)
120  {
121  // find & linearize histo for new fit
122  auto h2d = dynamic_cast<TH2*>(reference_id_maps.Get(deduce_bad_histo_name(bad_grid->GetName())));
123  if(!h2d)
124  {
125  KVError::Warning(this, "CorrectGrids",
126  "No reference idmap with name %s found for bad grid %s",
127  deduce_bad_histo_name(bad_grid->GetName()).Data(),bad_grid->GetName());
128  return;
129  }
130  auto h = bad_grid->LinearizeHistoToPID(h2d,100);
131  toto.ApplyCorrections(Correct, h, mass_fit_parameters);
132  }
133  else
134  toto.ApplyCorrections(Correct, nullptr, mass_fit_parameters);
135  }
136 }
137 
138 
139 
141 
142 void KVIDZAFromZGridBatchMassCorrector::CorrectGridToReference(const TString& bad_grid_name, const TString& ref_grid_name, const KVNameValueList& mass_fit_parameters, double min_yield_cutoff, std::function<KVString (const KVString&)> deduce_bad_histo_name)
143 {
144  auto bad_grid = dynamic_cast<KVIDZAFromZGrid*>(grids_to_correct.GetGrid(bad_grid_name));
145  if(!bad_grid)
146  {
147  KVError::Info(this, "CorrectGridToReference",
148  "No grid %s defined in file %s",
149  bad_grid_name.Data(), grids_to_correct.GetName());
150  return;
151  }
152  KVIDZAFromZGrid* ref_grid = dynamic_cast<KVIDZAFromZGrid*>(reference_grids.GetGrid(ref_grid_name));
153  if(!ref_grid)
154  {
155  KVError::Info(this, "CorrectGridToReference",
156  "No reference grid %s in file %s to compare with grid %s",
157  ref_grid_name.Data(), reference_grids.GetName(), bad_grid->GetName());
158  return;
159  }
160  CorrectGridToReference(bad_grid,ref_grid,mass_fit_parameters,min_yield_cutoff,deduce_bad_histo_name);
161 }
162 
163 
164 
166 
168  std::function<KVString (const KVString&)> deduce_ref_grid_name_from_bad_grid_name,
169  const KVNameValueList& mass_fit_parameters, double min_yield_cutoff,
170  std::function<KVString (const KVString&)> deduce_bad_histo_name)
171 {
172  auto bad_grid = dynamic_cast<KVIDZAFromZGrid*>(grids_to_correct.GetGrid(bad_grid_name));
173  if(!bad_grid)
174  {
175  KVError::Info(this, "CorrectGrid",
176  "No grid %s defined in file %s",
177  bad_grid_name.Data(), grids_to_correct.GetName());
178  return;
179  }
180  CorrectGrid(bad_grid,deduce_ref_grid_name_from_bad_grid_name,mass_fit_parameters,min_yield_cutoff,deduce_bad_histo_name);
181 }
182 
183 
184 
187 
188 void KVIDZAFromZGridBatchMassCorrector::CorrectGrids(std::function<KVString(const KVString&)> deduce_ref_grid_name_from_bad_grid_name,
189  const KVNameValueList& mass_fit_parameters,
190  double min_yield_cutoff,
191  std::function<KVString(const KVString&)> deduce_bad_histo_name
192  )
193 {
194  // loop over all grids to correct
195  TIter nxt_grid(grids_to_correct.GetGrids());
196  KVIDZAFromZGrid* bad_grid;
197  while( (bad_grid = (KVIDZAFromZGrid*)nxt_grid()) )
198  {
199  CorrectGrid(bad_grid,deduce_ref_grid_name_from_bad_grid_name,mass_fit_parameters,min_yield_cutoff,deduce_bad_histo_name);
200  }
201 }
202 
203 
204 
206 
208 {
209  grids_to_correct.WriteAsciiFile(filename);
210 }
211 
212 
213 
217 
219 {
220  // \warning Only use this method if you used the KVIDZAFromZGridBatchMassCorrector() constructor with 2 arguments: the path
221  // to the file containing grids to be refitted, and the path to the ROOT file containing the data to be used for the fits.
222  TIter nxt_grid(reference_grids.GetGrids());
224  while( (gr = (KVIDZAFromZGrid*)nxt_grid()) )
225  {
226  RefitReferenceGridToData(gr->GetName(),mass_fit_params);
227  }
228 }
229 
230 
231 
233 
234 void KVIDZAFromZGridBatchMassCorrector::RefitReferenceGridToData(const TString& ref_grid_name, const KVNameValueList& mass_fit_params)
235 {
236  auto gr = (KVIDZAFromZGrid*)reference_grids.GetGrid(ref_grid_name);
237  KVError::Info(this,"RefitAllReferenceGridsToData", "Grid %s", gr->GetName());
238  gr->Initialize();
239  if(!gr->GetFits() || gr->GetFits()->IsEmpty())
240  return;
241  auto hdata = (TH2*)reference_id_maps.Get(gr->GetName());//assume data hist has same name as grid!
242  if(!hdata)
243  return;
244  auto pid_dist = gr->LinearizeHistoToPID(hdata,100);
245  gr->GetFits()->ls();
246  KVNumberList zfits;
247  TIter nxt_fit(gr->GetFits());
249  while( (mgf = (KVMultiGaussIsotopeFit*)nxt_fit()) )
250  {
251  zfits.Add(mgf->GetZ());
252  }
253  for(auto z : zfits)
254  {
255  KVError::Info(this,"RefitReferenceGridToData", "...Z=%d", z);
256  KVGaussFitMassModifier gfmm(gr, z);
257  gfmm.Modify(0, pid_dist, mass_fit_params);
258  }
259 }
260 
261 
262 
264 
266 {
267  reference_grids.WriteAsciiFile(filename);
268 }
269 
270 
271 
273 
275 {
276  auto grid = (KVIDZAFromZGrid*)reference_grids.GetGrid(grid_name);
277  if(!grid)
278  {
279  KVError::Error(this, "DrawReferenceFit", "No grid with name %s", grid_name.Data());
280  return;
281  }
282  auto fit = grid->GetMultiGaussFit(Z);
283  if(!fit)
284  {
285  KVError::Error(this, "DrawReferenceFit", "No fit for Z=%d in grid %s", Z, grid_name.Data());
286  return;
287  }
288  fit->DrawFitWithGaussians();
289 }
290 
291 
292 
294 
296 {
297  auto grid = (KVIDZAFromZGrid*)grids_to_correct.GetGrid(grid_name);
298  if(!grid)
299  {
300  KVError::Error(this, "DrawOtherFit", "No grid with name %s", grid_name.Data());
301  return;
302  }
303  auto fit = grid->GetMultiGaussFit(Z);
304  if(!fit)
305  {
306  KVError::Error(this, "DrawOtherFit", "No fit for Z=%d in grid %s", Z, grid_name.Data());
307  return;
308  }
309  fit->DrawFitWithGaussians();
310 }
311 
312 
314 
int Int_t
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Change isotope masses in a multi-gauss fit by fixed offset.
static void SetAutoAdd(Bool_t yes=kTRUE)
Definition: KVIDGraph.h:156
const Char_t * GetName() const override
Definition: KVIDGraph.cpp:1416
KVIDGraph * GetGrid(const Char_t *name) const
Return pointer to grid with name "name".
Int_t WriteAsciiFile(const Char_t *filename, const TCollection *selection=0)
void AddLastReadGrids()
Add all grids read the last time ReadAsciiFile() was called to the grid manager.
Bool_t ReadAsciiFile(const Char_t *filename)
const KVSeqCollection * GetGrids() const
Modify/refit all multi-gauss fits in all grids in a file.
void CorrectGrids(std::function< KVString(const KVString &)> deduce_ref_grid_name_from_bad_grid_name, const KVNameValueList &mass_fit_parameters, double min_yield_cutoff=0.8, std::function< KVString(const KVString &)> deduce_bad_histo_name=[](const KVString &n){ return n;})
loop over all grids to correct
void CorrectGrid(const TString &bad_grid_name, std::function< KVString(const KVString &)> deduce_ref_grid_name_from_bad_grid_name, const KVNameValueList &mass_fit_parameters, double min_yield_cutoff=0.8, std::function< KVString(const KVString &)> deduce_bad_histo_name=[](const KVString &n){ return n;})
void RefitAllReferenceGridsToData(const KVNameValueList &mass_fit_params)
void DrawReferenceFit(const TString &grid_name, Int_t Z) const
void DrawOtherFit(const TString &grid_name, Int_t Z) const
KVIDZAFromZGridBatchMassCorrector(const TString &ref_grids_file, const TString &bad_grids_file, const TString &bad_grids_id_map_file)
void CorrectGridToReference(KVIDZAFromZGrid *bad_grid, KVIDZAFromZGrid *ref_grid, const KVNameValueList &mass_fit_parameters, double min_yield_cutoff=0.8, std::function< KVString(const KVString &)> deduce_bad_histo_name=[](const KVString &n){ return n;})
determine & apply corrections for grid
Modify masses used in multi-gauss fits of an id-grid to be conform to those of a 'reference' grid.
void ApplyCorrections(const std::vector< KVIDZAFromZGridMassCorrector::mass_correction_t > &, TH1 *pid_dist, const KVNameValueList &mass_fit_parameters)
std::vector< mass_correction_t > DetermineMassCorrections(double min_yield)
Hybrid charge & mass identification grid.
TH1 * LinearizeHistoToPID(const TH2 *hdata, int nbins=100) const override
Function for fitting PID mass spectra.
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:86
void Add(Int_t)
Add value 'n' to the list.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
virtual Int_t GetEntries() const
T * Get(const char *namecycle)
const char * GetName() const override
virtual void SetName(const char *name)
void ls(Option_t *option="") const override
const char * Data() const
TGraphErrors * gr
TH1 * h
void Error(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:116
void Warning(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:125
void Info(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:134
fit(model, train_loader, val_loader, num_epochs, batch_size, optimizer, criterion, save_best, scheduler)
ClassImp(TPyArg)