KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVedaLoss.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Wed Feb 2 15:49:27 2011
2 //Author: frankland,,,,
3 
4 #include "KVedaLoss.h"
5 #include "KVedaLossMaterial.h"
6 #include "KVedaLossRangeFitter.h"
7 #include <TString.h>
8 #include <TSystem.h>
9 #include <TEnv.h>
10 #include <KVSystemDirectory.h>
11 #include <KVSystemFile.h>
12 #include "TGeoMaterial.h"
13 
15 
18 
19 
35 
37 {
38  // Call this static method with yes=kTRUE in order to recalculate the nominal limits
39  // on incident ion energies for which the range tables are valid.
40  //
41  // Normally all range, \f$dE\f$, \f$E_{res}\f$ functions are limited to range \f$0\leq E\leq E_{max}\f$,
42  // where \f$E_{max}\f$ is nominal maximum energy for which range tables are valid
43  // (usually 400MeV/u for \f$Z<3\f$, 250MeV/u for \f$Z>3\f$).
44  //
45  // If higher energies are required, call static method KVedaLoss::SetIgnoreEnergyLimits() **BEFORE ANY MATERIALS ARE CREATED**
46  // in order to recalculate the \f$E_{max}\f$ limits in such a way that:
47  // - range function is always monotonically increasing function of \f$E_{inc}\f$;
48  // - stopping power is concave (i.e. no minimum of stopping power followed by an increase)
49  //
50  // Then, at the most, the new limit will be 1 GeV/nucleon, or
51  // at the least, it will remain at the nominal (400 or 250 MeV/nucleon) level.
53 }
54 
55 
56 
58 
60 {
62 }
63 
64 
65 
68 
70  : KVIonRangeTable("VEDALOSS",
71  "Calculation of range and energy loss of charged particles in matter using VEDALOSS range tables")
72 {
73  // Default constructor
74 
76  if (!CheckMaterialsList()) {
77  Error("KVedaLoss", "Problem reading range tables. Do not use.");
78  }
79 }
80 
81 
82 
85 
87 {
88  // Destructor
89 }
90 
91 
92 
99 
101 {
102  // PRIVATE method - called to initialize fMaterials list of all known materials
103  // properties, read from file given by TEnv variable KVedaLoss.RangeTables
104  //
105  // any files in $(WORKING_DIR)/vedaloss/*.dat will also be read, these contain
106  // materials added by the user(s)
107 
108  Info("init_materials", "Initialising KVedaLoss...");
109  fMaterials = new KVHashList;
110  fMaterials->SetName("VEDALOSS materials list");
111  fMaterials->SetOwner();
112 
113  TString DataFilePath;
114  if (!KVBase::SearchKVFile(gEnv->GetValue("KVedaLoss.RangeTables", "kvloss.data"), DataFilePath, "data")) {
115  Error("init_materials()", "Range tables file %s not found", gEnv->GetValue("KVedaLoss.RangeTables", "kvloss.data"));
116  return kFALSE;
117  }
118 
119  bool ok = ReadMaterials(DataFilePath);
120 
122  // read all user materials in directory
124  TIter nxtfil(matDir.GetListOfFiles());
125  KVSystemFile* fil;
126  while ((fil = (KVSystemFile*)nxtfil())) {
127  if (TString(fil->GetName()).EndsWith(".dat")) ok = ok && ReadMaterials(fil->GetFullPath());
128  }
129  }
130  return ok;
131 }
132 
133 
134 
137 
138 Bool_t KVedaLoss::ReadMaterials(const Char_t* DataFilePath) const
139 {
140  // Read and add range tables for materials in file
141 
142  Char_t name[25], gtype[25], state[10];
143  Float_t Amat = 0.;
144  Float_t Dens = 0.;
145  Float_t MoleWt = 0.;
146  Float_t Temp = 19.;
147  Float_t Zmat = 0.;
148  int mat_count = 0;
149 
150  FILE* fp;
151  if (!(fp = fopen(DataFilePath, "r"))) {
152  Error("init_materials()", "Range tables file %s cannot be opened", DataFilePath);
153  return kFALSE;
154  }
155  else {
156  char line[132];
157  while (fgets(line, 132, fp)) { // read lines from file
158 
159  switch (line[0]) {
160 
161  case '/': // ignore comment lines
162  break;
163 
164  case '+': // header lines
165 
166  if (sscanf(line, "+ %s %s %s %f %f %f %f %f",
167  gtype, name, state, &Dens, &Zmat, &Amat,
168  &MoleWt, &Temp)
169  != 8) {
170  Error("init_materials()", "Problem reading file %s", DataFilePath);
171  fclose(fp);
172  return kFALSE;
173  }
174  //found a new material
175  KVedaLossMaterial* tmp_mat = new KVedaLossMaterial(this, name, gtype, state, Dens,
176  Zmat, Amat, MoleWt);
177  fMaterials->Add(tmp_mat);
178  if (!tmp_mat->ReadRangeTable(fp)) return kFALSE;
179  tmp_mat->Initialize();
180  ++mat_count;
181  if (tmp_mat->IsGas()) tmp_mat->SetTemperatureAndPressure(19., 1.*KVUnits::atm);
182  break;
183  }
184  }
185  fclose(fp);
186  }
187  return kTRUE;
188 }
189 
190 
191 
196 
198 {
199  // Use the RANGE tables to generate a new material of a given element.
200  // If A is given only one isotope will be used, by default the natural abundance
201  // of each isotope is used to generate a mixture
202 
203  unique_ptr<KVIonRangeTable> yanez(KVIonRangeTable::GetRangeTable("RANGE"));
204  KVIonRangeTableMaterial* mat = yanez->AddElementalMaterial(Z, A);
205  AddMaterial(mat);
206  return GetMaterial(mat->GetName());
207 }
208 
209 
210 
213 
215 {
216  // If the given material is defined in the RANGE tables, import it into VEDALOSS
217 
218  unique_ptr<KVIonRangeTable> yanez(KVIonRangeTable::GetRangeTable("RANGE"));
219  if (yanez->GetMaterial(name)) {
220  AddMaterial(yanez->GetMaterial(name));
221  return kTRUE;
222  }
223  return kFALSE;
224 }
225 
226 
227 
230 
231 KVIonRangeTableMaterial* KVedaLoss::AddCompoundMaterial(const Char_t* name, const Char_t* symbol, Int_t nel, Int_t* Z, Int_t* A, Int_t* nat, Double_t dens) const
232 {
233  // Use the RANGE tables to generate a new compound material
234 
235  unique_ptr<KVIonRangeTable> yanez(KVIonRangeTable::GetRangeTable("RANGE"));
236  KVIonRangeTableMaterial* mat = yanez->AddCompoundMaterial(name, symbol, nel, Z, A, nat, dens);
237  AddMaterial(mat);
238  return GetMaterial(mat->GetName());
239 }
240 
241 
242 
245 
246 KVIonRangeTableMaterial* KVedaLoss::AddMixedMaterial(const Char_t* name, const Char_t* symbol, Int_t nel, Int_t* Z, Int_t* A, Int_t* nat, Double_t* prop, Double_t dens) const
247 {
248  // Use the RANGE tables to generate a new mixed material
249 
250  unique_ptr<KVIonRangeTable> yanez(KVIonRangeTable::GetRangeTable("RANGE"));
251  KVIonRangeTableMaterial* mat = yanez->AddMixedMaterial(name, symbol, nel, Z, A, nat, prop, dens);
252  AddMaterial(mat);
253  return GetMaterial(mat->GetName());
254 }
255 
256 
257 
258 
261 
263 {
264  // Returns pointer to material of given name or type.
266  if (!M) {
268  }
269  return M;
270 }
271 
272 
273 
282 
284 {
285  // Add a material (taken from a different range table) to VEDALOSS
286  // This means fitting the ranges for Z=1-100 and writing the parameters in a
287  // file which will be stored in
288  //
289  // $(WORKING_DIR)/VEDALOSS/[name].dat
290  //
291  // which will be read at each initialisation to include the new material
292 
293  KVedaLossRangeFitter vlfit;
294  vlfit.SetMaterial(mat);
295  TString matname = mat->GetName();
296  matname.ReplaceAll(" ", "_"); //no spaces in filename
297  matname += ".dat";
298  // check directory exists & make it if necessary
302  }
303  vlfit.DoFits(Form("%s/%s", fLocalMaterialsDirectory.Data(), matname.Data()));
304  ReadMaterials(Form("%s/%s", fLocalMaterialsDirectory.Data(), matname.Data()));
305 }
306 
307 
308 
310 
312 {
313  printf("KVedaLoss::%s\n%s\n", GetName(), GetTitle());
314  printf("\nEnergy loss & range tables loaded for %d materials:\n\n", fMaterials->GetEntries());
315  fMaterials->ls();
316 }
317 
318 
319 
324 
326 {
327  // Create and fill a list of all materials for which range tables exist.
328  // Each entry is a TNamed with the name and type (title) of the material.
329  // User's responsibility to delete list after use (it owns its objects).
330 
331  if (CheckMaterialsList()) {
332  TObjArray* list = new TObjArray(fMaterials->GetEntries());
333  list->SetOwner(kTRUE);
334  TIter next(fMaterials);
335  KVedaLossMaterial* mat;
336  while ((mat = (KVedaLossMaterial*)next())) {
337  list->Add(new TNamed(mat->GetName(), mat->GetType()));
338  }
339  return list;
340  }
341  return 0;
342 }
343 
344 
int Int_t
KVIonRangeTableMaterial * M
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
float Float_t
const Bool_t kTRUE
const char Option_t
R__EXTERN TEnv * gEnv
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
static const Char_t * GetWORKDIRFilePath(const Char_t *namefile="")
Definition: KVBase.cpp:127
const Char_t * GetType() const
Definition: KVBase.h:170
static Bool_t SearchKVFile(const Char_t *name, TString &fullpath, const Char_t *kvsubdir="")
Definition: KVBase.cpp:541
Extended version of ROOT THashList.
Definition: KVHashList.h:28
Material for use in energy loss & range calculations.
void SetTemperatureAndPressure(Double_t T, Double_t P)
Abstract base class for calculation of range & energy loss of charged particles in matter.
KVIonRangeTableMaterial * GetMaterial(const Char_t *material) const
Returns pointer to material of given name or type.
static KVIonRangeTable * GetRangeTable(const Char_t *name)
Generates an instance of the KVIonRangeTable plugin class corresponding to given name.
virtual void SetOwner(Bool_t enable=kTRUE)
virtual TObject * FindObjectByType(const Char_t *) const
virtual void Add(TObject *obj)
virtual TObject * FindObject(const char *name) const
Extension of ROOT TSystemDirectory class, handling browsing directories on disk.
virtual TList * GetListOfFiles() const
Extended ROOT TSystemFile with added info on file size etc.
Definition: KVSystemFile.h:17
const Char_t * GetFullPath() const
Definition: KVSystemFile.h:48
Description of material in the KVedaLoss range table.
static void SetNoLimits(Bool_t on=kTRUE)
Bool_t ReadRangeTable(FILE *fp)
static Bool_t CheckIon(Int_t Z)
Fit a range table using the VEDALOSS functional.
void SetMaterial(KVIonRangeTableMaterial *m)
Sets range table to fit. Also finds material with closest Z in VEDALOSS library.
void DoFits(TString output_file, Int_t Zmin=1, Int_t Zmax=100)
Perform fits to range tables for elements from Zmin to Zmax.
C++ implementation of VEDALOSS stopping power calculation.
Definition: KVedaLoss.h:33
KVIonRangeTableMaterial * AddCompoundMaterial(const Char_t *, const Char_t *, Int_t, Int_t *, Int_t *, Int_t *, Double_t=-1.0) const
Use the RANGE tables to generate a new compound material.
Definition: KVedaLoss.cpp:231
TObjArray * GetListOfMaterials()
Definition: KVedaLoss.cpp:325
Bool_t init_materials() const
Definition: KVedaLoss.cpp:100
KVIonRangeTableMaterial * GetMaterialWithNameOrType(const Char_t *material) const
Returns pointer to material of given name or type.
Definition: KVedaLoss.cpp:262
KVedaLoss()
Default constructor.
Definition: KVedaLoss.cpp:69
virtual ~KVedaLoss()
Destructor.
Definition: KVedaLoss.cpp:86
static KVHashList * fMaterials
static list of all known materials
Definition: KVedaLoss.h:34
KVIonRangeTableMaterial * AddElementalMaterial(Int_t Z, Int_t A=0) const
Definition: KVedaLoss.cpp:197
TString fLocalMaterialsDirectory
Definition: KVedaLoss.h:35
void AddMaterial(KVIonRangeTableMaterial *) const
Definition: KVedaLoss.cpp:283
Bool_t ReadMaterials(const Char_t *path) const
Read and add range tables for materials in file.
Definition: KVedaLoss.cpp:138
Bool_t CheckIon(Int_t Z, Int_t A) const
Definition: KVedaLoss.cpp:59
KVIonRangeTableMaterial * AddMixedMaterial(const Char_t *, const Char_t *, Int_t, Int_t *, Int_t *, Int_t *, Double_t *, Double_t=-1.0) const
Use the RANGE tables to generate a new mixed material.
Definition: KVedaLoss.cpp:246
Bool_t AddRANGEMaterial(const Char_t *name) const
If the given material is defined in the RANGE tables, import it into VEDALOSS.
Definition: KVedaLoss.cpp:214
static Bool_t fgNewRangeInversion
static flag for using new KVedaLossInverseRangeFunction
Definition: KVedaLoss.h:44
static void SetIgnoreEnergyLimits(Bool_t yes=kTRUE)
Definition: KVedaLoss.cpp:36
void Print(Option_t *="") const
Definition: KVedaLoss.cpp:311
Bool_t CheckMaterialsList() const
Definition: KVedaLoss.h:38
virtual void ls(Option_t *option="") const
void SetName(const char *name)
virtual Int_t GetEntries() const
virtual void SetOwner(Bool_t enable=kTRUE)
virtual const char * GetValue(const char *name, const char *dflt) const
virtual const char * GetName() const
virtual const char * GetTitle() const
void Add(TObject *obj)
virtual void Error(const char *method, const char *msgfmt,...) const
virtual void Info(const char *method, const char *msgfmt,...) const
const char * Data() const
TString & ReplaceAll(const char *s1, const char *s2)
virtual int Chmod(const char *file, UInt_t mode)
virtual int mkdir(const char *name, Bool_t recursive=kFALSE)
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
TLine * line
const long double atm
Definition: KVUnits.h:79