KaliVeda
Toolkit for HIC analysis
KVIDZAFromZGridMassHomogenizer.cpp
1 #include "KVGaussFitMassModifier.h"
2 #include "KVIDZAFromZGridMassHomogenizer.h"
3 #include "KVMultiGaussIsotopeFit.h"
4 #include "TCanvas.h"
5 
6 
13 
14 void KVIDZAFromZGridMassHomogenizer::correct_masses_for_grid(const TString& grid_name, int Z, int deltaA,
15  const KVNameValueList& mass_fit_parameters)
16 {
17  // Correct the masses for a given grid/telescope
18  //
19  // \param[in] Z the element atomic number to correct
20  // \param[in] deltaA mass modification \f$\Delta A\f$ to apply to all isotopes of element, can be positive or negative
21  // \param[in] mass_fit_parameters parameters to control fit; by default KVMultiGaussIsotopeFit::mass_fit_parameters
22 
23  auto gr = GetGrid(grid_name);
24  if(gr)
25  {
26  auto h2d = dynamic_cast<TH2*>(theData.Get(grid_name));
27  if(!h2d)
28  {
29  KVError::Warning(this, "correct_masses_for_grid",
30  "No data histo with name %s found for grid %s",
31  grid_name.Data(),gr->GetName());
32  return;
33  }
34  auto h = gr->LinearizeHistoToPID(h2d,100);
35  KVGaussFitMassModifier gfmm(gr, Z);
36  gfmm.Modify(deltaA, h, mass_fit_parameters);
37  }
38 }
39 
40 
41 
52 
54  : theData{datafile}
55 {
56  // Prepare mass homogeneity analysis
57  //
58  // \param[in] gridfile name/path to file containing grids to analyse
59  // \param[in] datafile name/path to ROOT file containing 2D identification maps corresponding to the grids
60  //
61  // \note We assume that the histogram and grid for each telescope have the same name, which allows to associate them easily!
62  //
63  // If more than 1 'runlist' parameter is used for the grids in the file, we print the list of runlists
64  // and inform that 1 runlist must be chosen before proceeding.
65 
66  KVIDGraph::SetAutoAdd(false);
67  if(theGrids.ReadAsciiFile(gridfile))
68  {
69  theGrids.AddLastReadGrids();
70  theGrids.SetName(gridfile);
71  KVError::Info(this, "KVIDZAFromZGridMassHomogenizer",
72  "Read %d reference grids from file %s",
73  theGrids.GetGrids()->GetEntries(),
74  theGrids.GetName());
75  for(auto _gr : *theGrids.GetGrids())
76  {
77  ++theRunLists[dynamic_cast<KVIDGrid*>(_gr)->GetRunList()];
78  }
79  if(theRunLists.find("") != theRunLists.end())
80  KVError::Info(this, "KVIDZAFromZGridMassHomogenizer",
81  " - %d grids are valid for all runs", theRunLists[""]);
82  if(theRunLists.size()>1)
83  {
84  KVError::Warning(this, "KVIDZAFromZGridMassHomogenizer",
85  "File contains grids with several different runlists. You must use SetCurrentRunList() to proceed:");
86  for(auto& rl : theRunLists)
87  {
88  if(rl.first!="")
89  KVError::Warning(this, "KVIDZAFromZGridMassHomogenizer",
90  " - %d grids are valid for runlist %s", rl.second, rl.first.c_str());
91  }
92  }
93  else
94  {
95  auto it = theRunLists.begin();
96  currentRunList = it->first.c_str();
97  KVError::Info(this, "KVIDZAFromZGridMassHomogenizer",
98  " - all %d grids are valid for runlist %s", it->second, it->first.c_str());
99  }
100  }
101  else
102  KVError::Error(this, "KVIDZAFromZGridMassHomogenizer",
103  "Could not read file %s",
104  gridfile.Data());
105 }
106 
107 
108 
111 
112 void KVIDZAFromZGridMassHomogenizer::Analyse(KVIDZAFromZGrid* grid, double min_yield)
113 {
114  // Analyse a grid according to the given minimum relative yield cut-off
115 
116  if(!grid)
117  return;
118  grid->Initialize();
119  if(!grid->GetFits()->GetEntries())
120  return;
121  KVError::Info(this, "Analyse", "Grid = %s", grid->GetName());
122  TIter nxt_fit(grid->GetFits());
124  while( (fit = (KVMultiGaussIsotopeFit*)nxt_fit()) )
125  {
126  KVNumberList alist;
127  auto yields = fit->GetRankedYields();
128  int i=0;
129  for(auto a : fit->GetIsotopesRankedByYield())
130  {
131  if(yields[i]>=min_yield*yields[0])
132  alist.Add(a);
133  ++i;
134  if(i==3)
135  break;
136  }
137  all_mass_ranges[fit->GetZ()][alist.AsString()].push_back(grid->GetName());
138  }
139 }
140 
141 
142 
149 
151 {
152  // Print in the terminal the mass distribution statistics obtained from the analysis
153  //
154  // For each element \f$Z\f$ for which a fit exists in at least 1 grid, this prints out
155  // each associated 'mass list' (list of \f$A\f$-values, KVNumberList format) and a (partial) list
156  // of the telescopes/grids which are concerned
157 
158  for(auto& stats : all_mass_ranges)
159  {
160  std::cout << "Z = " << stats.first << " :\n";
161  for(auto& stat : stats.second)
162  {
163  std::cout << "\t" << stat.first << " : " << stat.second.size() << "\t{";
164  int i=0, imax=5;
165  for(auto&s : stat.second)
166  {
167  if(i) std::cout << ", ";
168  ++i;
169  if(i>imax)
170  {
171  std::cout << "... ";
172  break;
173  }
174  std::cout << s;
175  }
176  std::cout << "}" << std::endl;
177  }
178  }
179 }
180 
181 
182 
186 
187 void KVIDZAFromZGridMassHomogenizer::ShowFits(int Z, const std::string& alist)
188 {
189  // For a given element and 'mass list' (see Print()), display the associated multi-gaussian fits for different grids/telescopes
190  // (up to 20 can be displayed).
191 
192  if(all_mass_ranges[Z].find(alist)!=all_mass_ranges[Z].end())
193  {
194  int maxNtel = 20;
195  auto Ntel = (int)all_mass_ranges[Z][alist].size();
196  int nx=3,ny=2;
197  auto tel_to_show = std::min(Ntel,maxNtel);
198  if(tel_to_show<6)
199  {
200  if(tel_to_show<4)
201  {
202  ny=1;
203  nx=tel_to_show;
204  }
205  }
206  else if(tel_to_show>6)
207  {
208  if(tel_to_show<10)
209  {
210  ny=3;
211  }
212  else if(tel_to_show<13)
213  {
214  nx=4;
215  ny=3;
216  }
217  else if(tel_to_show<16)
218  {
219  nx=5;
220  ny=3;
221  }
222  else if(tel_to_show<21)
223  {
224  nx=5;
225  ny=4;
226  }
227  }
228  int can_dimx=1920, can_dimy=1080;
229  if(tel_to_show<4)
230  {
231  can_dimy=600; can_dimx=tel_to_show*can_dimy;
232  }
233  auto c = new TCanvas(Form("fits_Z=%d_A=%s",Z,alist.c_str()),Form("fits_Z=%d_A=%s",Z,alist.c_str()),-can_dimx,can_dimy);
234  c->Divide(nx,ny);
235  int itel=1;
236  for(auto s : all_mass_ranges[Z][alist])
237  {
238  auto gr = GetGrid(s.c_str());
239  gr->Initialize();
240  auto pad = c->cd(itel);
241  gr->GetMultiGaussFit(Z)->DrawFitWithGaussians("",s.c_str());
242  // legend height = .2/5*(ngauss+1)
243  auto dy = .2*(gr->GetMultiGaussFit(Z)->GetNGaussians()+1)/5.;
244  pad->BuildLegend(.1,.9-dy,.45,.9);
245  pad->Modified();
246  pad->Update();
247  ++itel;
248  if(itel>tel_to_show)
249  break;
250  }
251  }
252  else
253  {
254  KVError::Error(this,"ShowFits","No mass range '%s' for Z=%d",alist.c_str(),Z);
255  }
256 }
257 
258 
259 
267 
268 void KVIDZAFromZGridMassHomogenizer::CorrectMasses(int Z, const std::string& alist, int deltaA,
269  const KVNameValueList& mass_fit_parameters)
270 {
271  // Apply the given mass-shift to the isotopes for element \f$Z\f$ in all grids corresponding to the given 'mass list' (see Print()).
272  //
273  // \param[in] Z element atomic number
274  // \param[in] alist 'mass list' attributed to grids to modify (see Print())
275  // \param[in] deltaA mass modification \f$\Delta A\f$ to apply to all isotopes of element, can be positive or negative
276  // \param[in] mass_fit_parameters parameters to control fit; by default KVMultiGaussIsotopeFit::mass_fit_parameters
277 
278  if(all_mass_ranges[Z].find(alist)!=all_mass_ranges[Z].end())
279  {
280  for(auto s : all_mass_ranges[Z][alist])
281  {
282  correct_masses_for_grid(s.c_str(),Z,deltaA,mass_fit_parameters);
283  }
284  }
285 }
286 
287 
288 
296 
297 void KVIDZAFromZGridMassHomogenizer::CorrectMassesForTelescope(int Z, const TString& idtel, int deltaA, const KVNameValueList& mass_fit_parameters)
298 {
299  // Apply the given mass-shift to the isotopes for element \f$Z\f$ of the given telescope/grid
300  //
301  // \param[in] Z element atomic number
302  // \param[in] idtel name of grid/telescope to modify
303  // \param[in] deltaA mass modification \f$\Delta A\f$ to apply to all isotopes of element, can be positive or negative
304  // \param[in] mass_fit_parameters parameters to control fit; by default KVMultiGaussIsotopeFit::mass_fit_parameters
305 
306  correct_masses_for_grid(idtel,Z,deltaA,mass_fit_parameters);
307 }
308 
309 
310 
313 
315 {
316  // Save all grids: overwrites original file if no filename given (filename=""), otherwise writes a new file
317  if(filename.IsNull())
318  theGrids.WriteAsciiFile(theGrids.GetName());
319  else
320  theGrids.WriteAsciiFile(filename);
321 }
322 
323 
325 
#define c(i)
size_t size(const MatrixT &matrix)
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
char * Form(const char *fmt,...)
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
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
Abstract base class for 2D identification grids in e.g. (dE,E) maps.
Definition: KVIDGrid.h:74
Check & correct if necessary that isotopic masses used in different multi-gauss fits are consistent.
void CorrectMassesForTelescope(int Z, const TString &idtel, int deltaA, const KVNameValueList &mass_fit_parameters)
void SaveGrids(const TString &filename)
Save all grids: overwrites original file if no filename given (filename=""), otherwise writes a new f...
KVIDZAFromZGridMassHomogenizer(const TString &gridfile, const TString &datafile)
void CorrectMasses(int Z, const std::string &alist, int deltaA, const KVNameValueList &mass_fit_parameters)
void ShowFits(int Z, const std::string &alist)
Hybrid charge & mass identification grid.
void Initialize() override
auto GetFits() const
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
const Char_t * AsString(Int_t maxchars=0) const
void Add(Int_t)
Add value 'n' to the list.
virtual Int_t GetEntries() const
T * Get(const char *namecycle)
const char * GetName() const override
virtual void SetName(const char *name)
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)
end
ClassImp(TPyArg)