VGM Version 5.3
Loading...
Searching...
No Matches
MaterialFactory.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The RootGM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// Class MaterialFactory
14// ------------------------
15// VGM material factory for Root.
16//
17// Author: Ivana Hrivnacova; IPN Orsay
18
21
23
33
34#include "TGeoElement.h"
35#include "TGeoManager.h"
36#include "TGeoMaterial.h"
37#include "TList.h"
38
39#include <cmath>
40
41const double RootGM::MaterialFactory::fgkTolerance = 1e-09;
42
43//_____________________________________________________________________________
45 : VGM::IMaterialFactory(),
46 BaseVGM::VMaterialFactory("Root_GM_Material_Factory")
47{
49
50 if (!gGeoManager) new TGeoManager("VGM Root geometry", "VGM Root geometry");
51}
52
53//_____________________________________________________________________________
55 : VGM::IMaterialFactory(rhs), BaseVGM::VMaterialFactory(rhs)
56{
58}
59
60//_____________________________________________________________________________
62{
63 //
64
65 // delete map singletons
67 // There is inconsistence in using the singleton maps
68 // via a factory which is not a singleton
69 // TO DO: to be improved later
70}
71
72//
73// private functions
74//
75
76//_____________________________________________________________________________
77VGM::IElement* RootGM::MaterialFactory::GetElement(double z, double a) const
78{
80
81 for (unsigned i = 0; i < Elements().size(); i++) {
82 VGM::IElement* element = Elements()[i];
83
84 if (std::abs(z - element->Z()) < fgkTolerance &&
85 std::abs(a - element->A()) < fgkTolerance)
86 return element;
87 }
88
89 return 0;
90}
91
92//_____________________________________________________________________________
93VGM::IElement* RootGM::MaterialFactory::GetElement(
94 const std::string& name) const
95{
97
98 for (unsigned i = 0; i < Elements().size(); i++) {
99 VGM::IElement* element = Elements()[i];
100
101 if (name == element->Name()) return element;
102 }
103
104 return 0;
105}
106
107//_____________________________________________________________________________
108VGM::IIsotope* RootGM::MaterialFactory::GetIsotope(double z, double n) const
109{
111
112 for (unsigned i = 0; i < Isotopes().size(); i++) {
113 VGM::IIsotope* isotope = Isotopes()[i];
114
115 if (z == isotope->Z() && n == isotope->N()) return isotope;
116 }
117
118 return 0;
119}
120
121//_____________________________________________________________________________
122bool RootGM::MaterialFactory::CompareIsotopes(const TGeoElement* tgeoElement,
123 const VGM::IsotopeVector& isotopes,
124 const VGM::RelAbundanceVector& relAbundances) const
125{
129
130 if (Int_t(isotopes.size()) != tgeoElement->GetNisotopes()) return false;
131
132 for (int i = 0; i < tgeoElement->GetNisotopes(); ++i) {
133
134 const TGeoIsotope* tgeoIsotope = tgeoElement->GetIsotope(i);
135 double tgeoRelAbundance = tgeoElement->GetRelativeAbundance(i);
136 bool match = false;
137
138 for (unsigned j = 0; j < isotopes.size(); ++j) {
139 VGM::IIsotope* vgmIsotope = isotopes[j];
140 if (std::abs(vgmIsotope->Z() - tgeoIsotope->GetZ()) < fgkTolerance &&
141 std::abs(vgmIsotope->N() - tgeoIsotope->GetN()) < fgkTolerance &&
142 std::abs(vgmIsotope->A() - tgeoIsotope->GetA()) < fgkTolerance &&
143 std::abs(relAbundances[j] - tgeoRelAbundance) < fgkTolerance) {
144 match = true;
145 break;
146 }
147 }
148 if (!match) return false;
149 }
150
151 // All isotopes were matched
152 return true;
153}
154
155//_____________________________________________________________________________
156void RootGM::MaterialFactory::ImportIsotopes(TGeoElement* element)
157{
159
160 int nofIsotopes = element->GetNisotopes();
161 if (nofIsotopes == 0) return;
162
163 for (Int_t i = 0; i < nofIsotopes; i++) {
164
165 TGeoIsotope* tgeoIsotope = element->GetIsotope(i);
166
167 VGM::IIsotope* vgmIsotope =
169
170 if (!vgmIsotope) {
171 if (Debug() > 0) {
173 std::cout << "Importing isotope: ";
174 if (Debug() > 1) std::cout << tgeoIsotope;
175 std::cout << std::endl;
177 tgeoIsotope->Print();
178 }
179
180 vgmIsotope = new RootGM::Isotope(tgeoIsotope);
181 IsotopeStore().push_back(vgmIsotope);
182 }
183 }
184}
185
186//_____________________________________________________________________________
187void RootGM::MaterialFactory::ImportElements(
188 TGeoMaterial* material, std::vector<VGM::IElement*>& elements)
189{
193
194 int nofElements = 1;
195 if (material->IsMixture())
196 nofElements = ((TGeoMixture*)material)->GetNelements();
197
198 for (Int_t i = 0; i < nofElements; i++) {
199
200 double z = material->GetZ();
201 double a = material->GetA();
202 if (material->IsMixture()) {
203 z = ((TGeoMixture*)material)->GetZmixt()[i];
204 a = ((TGeoMixture*)material)->GetAmixt()[i];
205 }
206
207 // Get element from element store if it already exists
208 VGM::IElement* vgmElement = GetElement(z, a);
209
210 // Do not take the element if its properties do not match
211 if (vgmElement && (std::abs(vgmElement->Z() - z) >= fgkTolerance ||
212 std::abs(vgmElement->A() - a) >= fgkTolerance)) {
213 vgmElement = 0;
214 }
215
216 // Create VGM element if it does not exist
217 if (!vgmElement) {
218 // Get element name & symbol from Root element table
219 TGeoElement* tgeoElement = material->GetElement(i);
220 std::string name = tgeoElement->GetTitle();
221 std::string symbol = tgeoElement->GetName();
222
223 bool isElementObject = std::abs(tgeoElement->Z() - z) < fgkTolerance &&
224 std::abs(tgeoElement->A() - a) < fgkTolerance;
225 std::string from;
226
227 if (isElementObject) {
228 ImportIsotopes(tgeoElement);
229 vgmElement = new RootGM::Element(tgeoElement);
230 from = "from element object";
231 }
232 else {
233 // The elements defined in the old way without using TGeoElement
234 // object
235 vgmElement = new RootGM::ElementNonGeo(name, symbol, z, a);
236 from = "from material";
237 }
238
239 if (Debug() > 0) {
241 std::cout << "Importing element: ";
242 if (Debug() > 1) std::cout << vgmElement << " ";
243 std::cout << from << std::endl;
245 tgeoElement->Print();
246 }
247
248 ElementStore().push_back(vgmElement);
249 }
250 elements.push_back(vgmElement);
251 }
252}
253
254//_____________________________________________________________________________
255void RootGM::MaterialFactory::ImportMaterial(TGeoMaterial* material)
256{
258
259 if (Debug() > 0) {
261 std::cout << "Importing material: ";
262 if (Debug() > 1) std::cout << material;
263 std::cout << std::endl;
265 // std::cout << *material << std::endl;
266 material->Print();
267 }
268
269 // Import elements
270 std::vector<VGM::IElement*> elements;
271 ImportElements(material, elements);
272
273 // Create material
274 VGM::IMaterial* vgmMaterial = new RootGM::Material(material, elements);
275 MaterialStore().push_back(vgmMaterial);
276}
277
278//_____________________________________________________________________________
279void RootGM::MaterialFactory::ImportMedium(TGeoMedium* medium)
280{
282
283 if (Debug() > 0) {
285 std::cout << "Importing medium: ";
286 if (Debug() > 1) std::cout << medium;
287 std::cout << std::endl;
289 // std::cout << *medium << std::endl;
290 medium->Print();
291 }
292
293 VGM::IMedium* vgmMedium = new RootGM::Medium(medium);
294 MediumStore().push_back(vgmMedium);
295}
296
297//
298// public functions
299//
300
301//_____________________________________________________________________________
303 const std::string& name, int z, int n, double a)
304{
305 // Create isotope if it does not yet exists
306
307 TGeoIsotope* tgeoIsotope =
308 TGeoElement::GetElementTable()->FindIsotope(name.data());
309
310 // Do not take the isotope if its properties do not match
311 if (tgeoIsotope && (std::abs(tgeoIsotope->GetZ() - z) >= fgkTolerance ||
312 std::abs(tgeoIsotope->GetN() - n) >= fgkTolerance ||
313 std::abs(tgeoIsotope->GetA() - a) >= fgkTolerance)) {
314 tgeoIsotope = 0;
315 }
316
317 VGM::IIsotope* vgmIsotope;
318 if (tgeoIsotope) {
319 vgmIsotope = RootGM::IsotopeMap::Instance()->GetIsotope(tgeoIsotope);
320 }
321 else {
322 vgmIsotope = new RootGM::Isotope(name, z, n, a);
323 IsotopeStore().push_back(vgmIsotope);
324 }
325
326 return vgmIsotope;
327}
328
329//_____________________________________________________________________________
331 const std::string& name, const std::string& symbol, double z, double a)
332{
333 // Create element if such element with specified properties does not
334 // yet exist
335
336 TGeoElement* tgeoElement =
337 TGeoElement::GetElementTable()->FindElement(name.data());
338
339 // Do not take the element if its properties do not match
340 if (tgeoElement && (std::abs(tgeoElement->Z() - z) >= fgkTolerance ||
341 std::abs(tgeoElement->A() - a) >= fgkTolerance)) {
342 tgeoElement = 0;
343 }
344
345 VGM::IElement* vgmElement;
346 if (tgeoElement) {
347 vgmElement = RootGM::ElementMap::Instance()->GetElement(tgeoElement);
348 if (!vgmElement) {
349 vgmElement = new RootGM::Element(tgeoElement);
350 ElementStore().push_back(vgmElement);
351 }
352 }
353 else {
354 vgmElement = new RootGM::Element(name, symbol, z, a);
355 ElementStore().push_back(vgmElement);
356 }
357 return vgmElement;
358}
359
360//_____________________________________________________________________________
362 const std::string& symbol, const VGM::IsotopeVector& isotopes,
363 const VGM::RelAbundanceVector& relAbundances)
364{
365 // Create element from isotopes
366
367 // Check first if the element with this name and isotopes already exists
368 VGM::IElement* vgmElement = GetElement(name);
369 if (vgmElement) {
370 TGeoElement* tgeoElement =
372 if (tgeoElement && CompareIsotopes(tgeoElement, isotopes, relAbundances)) {
373 return vgmElement;
374 }
375 }
376
377 // The element is not yet defined - create a new one
378 vgmElement = new RootGM::Element(name, symbol, isotopes, relAbundances);
379 ElementStore().push_back(vgmElement);
380
381 return vgmElement;
382}
383
384//_____________________________________________________________________________
386{
387 // Create element using TGeoElementTable
388 // if such element with specified properties does not yet exist
389
390 TGeoElementTable* elementTable = gGeoManager->GetElementTable();
391 TGeoElement* geoElement = elementTable->GetElement(z);
392 if (!geoElement) {
393 std::cerr << "No element with z=" << z << " defined." << std::endl;
394 return 0;
395 }
396
397 // Check first if the element with this name already exists
398 VGM::IElement* vgmElement = GetElement(z, geoElement->A());
399 if (vgmElement) return vgmElement;
400
401 // The element is not yet defined - create a new one
402 vgmElement = new RootGM::Element(geoElement);
403 ElementStore().push_back(vgmElement);
404
405 return vgmElement;
406}
407
408//_____________________________________________________________________________
410 double density, VGM::IElement* element, double radlen, double intlen)
411{
412 // Create material
413
414 VGM::IMaterial* vgmMaterial =
415 new RootGM::Material(name, density, element, radlen, intlen);
416
417 MaterialStore().push_back(vgmMaterial);
418 return vgmMaterial;
419}
420
421//_____________________________________________________________________________
423 double density, VGM::IElement* element, double radlen, double intlen,
424 VGM::MaterialState state, double temperature, double pressure)
425{
426 // Create material
427
428 VGM::IMaterial* vgmMaterial = new RootGM::Material(
429 name, density, element, radlen, intlen, state, temperature, pressure);
430
431 MaterialStore().push_back(vgmMaterial);
432 return vgmMaterial;
433}
434
435//_____________________________________________________________________________
437 double density, const VGM::ElementVector& elements,
438 const VGM::MassFractionVector& fractions)
439{
440 // Create material
441
442 VGM::IMaterial* vgmMaterial =
443 new RootGM::Material(name, density, elements, fractions);
444
445 MaterialStore().push_back(vgmMaterial);
446 return vgmMaterial;
447}
448
449//_____________________________________________________________________________
451 double density, const VGM::ElementVector& elements,
452 const VGM::MassFractionVector& fractions, VGM::MaterialState state,
453 double temperature, double pressure)
454{
455 // Create material
456
457 VGM::IMaterial* vgmMaterial = new RootGM::Material(
458 name, density, elements, fractions, state, temperature, pressure);
459
460 MaterialStore().push_back(vgmMaterial);
461 return vgmMaterial;
462}
463
464//_____________________________________________________________________________
466 double density, const VGM::ElementVector& elements,
467 const VGM::AtomCountVector& atomCounts)
468{
469 // Create material
470
471 VGM::IMaterial* vgmMaterial =
472 new RootGM::Material(name, density, elements, atomCounts);
473
474 MaterialStore().push_back(vgmMaterial);
475 return vgmMaterial;
476}
477
478//_____________________________________________________________________________
480 double density, const VGM::ElementVector& elements,
481 const VGM::AtomCountVector& atomCounts, VGM::MaterialState state,
482 double temperature, double pressure)
483{
484 // Create material
485
486 VGM::IMaterial* vgmMaterial = new RootGM::Material(
487 name, density, elements, atomCounts, state, temperature, pressure);
488
489 MaterialStore().push_back(vgmMaterial);
490 return vgmMaterial;
491}
492
493//_____________________________________________________________________________
495 int mediumId, VGM::IMaterial* material, int nofParameters, double* parameters)
496{
497 // Create medium
498
499 VGM::IMedium* vgmMedium =
500 new RootGM::Medium(name, mediumId, material, nofParameters, parameters);
501
502 MediumStore().push_back(vgmMedium);
503 return vgmMedium;
504}
505
506//_____________________________________________________________________________
508{
511
512 TList* materials = gGeoManager->GetListOfMaterials();
513 TIter next(materials);
514 while (TObject* obj = next()) {
515 TGeoMaterial* material = (TGeoMaterial*)obj;
516 ImportMaterial(material);
517 }
518
519 TList* media = gGeoManager->GetListOfMedia();
520 TIter next2(media);
521 while (TObject* obj = next2()) {
522 TGeoMedium* medium = (TGeoMedium*)obj;
523 ImportMedium(medium);
524 }
525
526 return true;
527}
TGeoElement * GetElement(VGM::IElement *iElement) const
static ElementMap * Instance()
VGM implementation for Root element which is not represented via TGeoElement object in Root geometry.
VGM implementation for Root element using TGeoElement object.
Definition Element.h:32
TGeoIsotope * GetIsotope(VGM::IIsotope *iIsotope) const
static IsotopeMap * Instance()
VGM implementation for Root isotope.
Definition Isotope.h:32
VGM material factory for Root.
virtual VGM::IMaterial * CreateMaterial(const std::string &name, double density, VGM::IElement *element, double radlen, double intlen)
Create a material.
virtual VGM::IElement * CreateElement(const std::string &name, const std::string &symbol, double z, double a)
Create a chemical element.
virtual bool Import()
Import native materials.
virtual VGM::IIsotope * CreateIsotope(const std::string &name, int z, int n, double a=0.)
Create a chemical isotope.
virtual VGM::IMedium * CreateMedium(const std::string &name, int mediumId, VGM::IMaterial *material, int nofParameters, double *parameters)
Create a tracking medium.
static MaterialMap * Instance()
VGM implementation for Root material.
Definition Material.h:32
VGM implementation for Root medium.
Definition Medium.h:32
The VGM interface to elements.
Definition IElement.h:34
virtual double Z() const =0
Return the effective atomic number.
virtual std::string Name() const =0
Return the name of this element.
virtual double A() const =0
Return the effective effective mass of a mole in g/mole.
The VGM interface to elements.
Definition IIsotope.h:28
virtual int N() const =0
Return the effective number of nucleons.
virtual double A() const =0
Return the effective effective mass of a mole in g/mole.
virtual int Z() const =0
Return the effective atomic number.
The VGM interface to materials.
Definition IMaterial.h:44
The VGM interface to tracking medium.
Definition IMedium.h:31
BaseVGM utilities.
Definition utilities.h:23
void DebugInfo()
Debug printing.
Definition utilities.cxx:27
VGM interfaces.
Definition VMedium.h:28
std::vector< IMaterial * > MaterialStore
std::vector< double > RelAbundanceVector
Definition IElement.h:31
std::vector< int > AtomCountVector
Definition IMaterial.h:33
std::vector< double > MassFractionVector
Definition IMaterial.h:32
std::vector< IElement * > ElementVector
Definition IMaterial.h:31
std::vector< IMedium * > MediumStore
std::vector< IIsotope * > IsotopeStore
std::vector< IIsotope * > IsotopeVector
Definition IElement.h:30
MaterialState
Definition IMaterial.h:36
std::vector< IElement * > ElementStore