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 
49 
51  : theData{datafile}
52 {
53  // Prepare mass homogeneity analysis
54  //
55  // \param[in] gridfile name/path to file containing grids to analyse
56  // \param[in] datafile name/path to ROOT file containing 2D identification maps corresponding to the grids
57  //
58  // \note We assume that the histogram and grid for each telescope have the same name, which allows to associate them easily!
59 
60  KVIDGraph::SetAutoAdd(false);
61  if(theGrids.ReadAsciiFile(gridfile))
62  {
63  theGrids.AddLastReadGrids();
64  theGrids.SetName(gridfile);
65  KVError::Info(this, "KVIDZAFromZGridMassHomogenizer",
66  "Read %d reference grids from file %s",
67  theGrids.GetGrids()->GetEntries(),
68  theGrids.GetName());
69  }
70  else
71  KVError::Error(this, "KVIDZAFromZGridMassHomogenizer",
72  "Could not read file %s",
73  gridfile.Data());
74 }
75 
76 
77 
80 
81 void KVIDZAFromZGridMassHomogenizer::Analyse(KVIDZAFromZGrid* grid, double min_yield)
82 {
83  // Analyse a grid according to the given minimum relative yield cut-off
84 
85  if(!grid)
86  return;
87  grid->Initialize();
88  if(!grid->GetFits()->GetEntries())
89  return;
90  KVError::Info(this, "Analyse", "Grid = %s", grid->GetName());
91  TIter nxt_fit(grid->GetFits());
93  while( (fit = (KVMultiGaussIsotopeFit*)nxt_fit()) )
94  {
95  KVNumberList alist;
96  auto yields = fit->GetRankedYields();
97  int i=0;
98  for(auto a : fit->GetIsotopesRankedByYield())
99  {
100  if(yields[i]>=min_yield*yields[0])
101  alist.Add(a);
102  ++i;
103  if(i==3)
104  break;
105  }
106  all_mass_ranges[fit->GetZ()][alist.AsString()].push_back(grid->GetName());
107  }
108 }
109 
110 
111 
118 
120 {
121  // Print in the terminal the mass distribution statistics obtained from the analysis
122  //
123  // For each element \f$Z\f$ for which a fit exists in at least 1 grid, this prints out
124  // each associated 'mass list' (list of \f$A\f$-values, KVNumberList format) and a (partial) list
125  // of the telescopes/grids which are concerned
126 
127  for(auto& stats : all_mass_ranges)
128  {
129  std::cout << "Z = " << stats.first << " :\n";
130  for(auto& stat : stats.second)
131  {
132  std::cout << "\t" << stat.first << " : " << stat.second.size() << "\t{";
133  int i=0, imax=5;
134  for(auto&s : stat.second)
135  {
136  if(i) std::cout << ", ";
137  ++i;
138  if(i>imax)
139  {
140  std::cout << "... ";
141  break;
142  }
143  std::cout << s;
144  }
145  std::cout << "}" << std::endl;
146  }
147  }
148 }
149 
150 
151 
155 
156 void KVIDZAFromZGridMassHomogenizer::ShowFits(int Z, const std::string& alist)
157 {
158  // For a given element and 'mass list' (see Print()), display the associated multi-gaussian fits for different grids/telescopes
159  // (up to 20 can be displayed).
160 
161  if(all_mass_ranges[Z].find(alist)!=all_mass_ranges[Z].end())
162  {
163  int maxNtel = 20;
164  auto Ntel = (int)all_mass_ranges[Z][alist].size();
165  int nx=3,ny=2;
166  auto tel_to_show = std::min(Ntel,maxNtel);
167  if(tel_to_show<6)
168  {
169  if(tel_to_show<4)
170  {
171  ny=1;
172  nx=tel_to_show;
173  }
174  }
175  else if(tel_to_show>6)
176  {
177  if(tel_to_show<10)
178  {
179  ny=3;
180  }
181  else if(tel_to_show<13)
182  {
183  nx=4;
184  ny=3;
185  }
186  else if(tel_to_show<16)
187  {
188  nx=5;
189  ny=3;
190  }
191  else if(tel_to_show<21)
192  {
193  nx=5;
194  ny=4;
195  }
196  }
197  int can_dimx=1920, can_dimy=1080;
198  if(tel_to_show<4)
199  {
200  can_dimy=600; can_dimx=tel_to_show*can_dimy;
201  }
202  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);
203  c->Divide(nx,ny);
204  int itel=1;
205  for(auto s : all_mass_ranges[Z][alist])
206  {
207  auto gr = GetGrid(s.c_str());
208  gr->Initialize();
209  auto pad = c->cd(itel);
210  gr->GetMultiGaussFit(Z)->DrawFitWithGaussians("",s.c_str());
211  // legend height = .2/5*(ngauss+1)
212  auto dy = .2*(gr->GetMultiGaussFit(Z)->GetNGaussians()+1)/5.;
213  pad->BuildLegend(.1,.9-dy,.45,.9);
214  pad->Modified();
215  pad->Update();
216  ++itel;
217  if(itel>tel_to_show)
218  break;
219  }
220  }
221  else
222  {
223  KVError::Error(this,"ShowFits","No mass range '%s' for Z=%d",alist.c_str(),Z);
224  }
225 }
226 
227 
228 
236 
237 void KVIDZAFromZGridMassHomogenizer::CorrectMasses(int Z, const std::string& alist, int deltaA,
238  const KVNameValueList& mass_fit_parameters)
239 {
240  // Apply the given mass-shift to the isotopes for element \f$Z\f$ in all grids corresponding to the given 'mass list' (see Print()).
241  //
242  // \param[in] Z element atomic number
243  // \param[in] alist 'mass list' attributed to grids to modify (see Print())
244  // \param[in] deltaA mass modification \f$\Delta A\f$ to apply to all isotopes of element, can be positive or negative
245  // \param[in] mass_fit_parameters parameters to control fit; by default KVMultiGaussIsotopeFit::mass_fit_parameters
246 
247  if(all_mass_ranges[Z].find(alist)!=all_mass_ranges[Z].end())
248  {
249  for(auto s : all_mass_ranges[Z][alist])
250  {
251  correct_masses_for_grid(s.c_str(),Z,deltaA,mass_fit_parameters);
252  }
253  }
254 }
255 
256 
257 
265 
266 void KVIDZAFromZGridMassHomogenizer::CorrectMassesForTelescope(int Z, const TString& idtel, int deltaA, const KVNameValueList& mass_fit_parameters)
267 {
268  // Apply the given mass-shift to the isotopes for element \f$Z\f$ of the given telescope/grid
269  //
270  // \param[in] Z element atomic number
271  // \param[in] idtel name of grid/telescope to modify
272  // \param[in] deltaA mass modification \f$\Delta A\f$ to apply to all isotopes of element, can be positive or negative
273  // \param[in] mass_fit_parameters parameters to control fit; by default KVMultiGaussIsotopeFit::mass_fit_parameters
274 
275  correct_masses_for_grid(idtel,Z,deltaA,mass_fit_parameters);
276 }
277 
278 
279 
282 
284 {
285  // Save all grids: overwrites original file if no filename given (filename=""), otherwise writes a new file
286  if(filename.IsNull())
287  theGrids.WriteAsciiFile(theGrids.GetName());
288  else
289  theGrids.WriteAsciiFile(filename);
290 }
291 
292 
294 
#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
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)