GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseNucleus.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef ROOT_TNamed
24 #include <TNamed.h>
25 #endif
26 
27 #ifndef ROOT_TXMLNode
28 #include <TXMLNode.h>
29 #endif
30 
31 #ifndef ROOT_TDOMParser
32 #include <TDOMParser.h>
33 #endif
34 
35 #ifndef ROOT_TXMLAttr
36 #include <TXMLAttr.h>
37 #endif
38 
39 #if ROOT_VERSION_CODE >= ROOT_VERSION(5,15,0)
40 #include "TList.h"
41 #endif
42 
43 #ifndef GW_ENV_H
44 #include "Env.h"
45 #endif
46 
47 #ifndef Gw_BaseNucleus
48 #include "BaseNucleus.h"
49 #endif
50 
51 #include <iostream>
52 #include <cstdlib>
53 
54 using namespace std;
55 using namespace Gw;
56 
58 
59 TMap *BaseNucleus::gTable = new TMap();
60 
61 //Defaut constructor
62 BaseNucleus::BaseNucleus(): fA(0), fN(0), fZ(0)
63 {
64 }
65 
66 //Constructor
67 BaseNucleus::BaseNucleus(Int_t A, Int_t Z, Int_t N)
68 {
69  SetAZN(A,Z,N);
70 }
71 
72 //Constructor
74 {
75  fA = fZ = fN = 0;
76 }
77 
78 bool BaseNucleus::SetAZN(Int_t A, Int_t Z)
79 {
80  if ( A < 0 || Z < 0 || A < Z ) return false;
81  else {
82  fA = A; fZ = Z; fN = fA - fZ;
83  }
84  return true;
85 }
86 
87 
88 bool BaseNucleus::SetAZN(Int_t A, Int_t Z, Int_t N)
89 {
90  if ( A < 0 || Z < 0 || N < 0 ) return false;
91 
92  fA = A; fZ = Z; fN = N;
93 
94  // now check if it is consistant
95  if ( N != (A-Z) ) {
96  cout << " - ! - in BaseNucleus::Set N != A - Z " << endl;
97  fN = fA - fZ;
98  }
99  return true;
100 }
101 
102 bool BaseNucleus::SetAZN(const char *s, Int_t A)
103 {
104  if ((s == NULL) || A < 0) return false;
105 
106  if (gTable->GetSize() == 0) { //test gTable
107  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
108  //return -1;
109  LoadTable();
110  }
111  Int_t Z = WhichZ(s);
112  if ( (Z < 0) || (A-Z < 0) ) return false;
113  SetAZN(A,Z,A-Z);
114  return true;
115 }
116 
117 bool BaseNucleus::SetAZN(const char *s)
118 {
119  if (s == NULL) return false;
120 
121  if (gTable->GetSize() == 0) {
122  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
123  //return -1;
124  LoadTable();
125  }
126  Int_t Z = WhichZ(s);
127  if ( Z < 0) return false;
128 
129  BaseNucleus *bn = (BaseNucleus *)gTable->GetValue(s);
130  SetA(bn->GetA());
131  SetZ(bn->GetZ());
132  SetN(bn->GetN());
133 
134  return true;
135 }
136 
137 const char *BaseNucleus::WhichSymbol(Int_t Z){
138  if (gTable->GetSize() == 0) {
139  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
140  //return 0;
141  LoadTable();
142  }
143  TMapIter *iter = (TMapIter *)gTable->MakeIterator(); //Iterator to loop on gTable
144  TNamed *str;
145  BaseNucleus *bn;
146  while ((str = (TNamed *)iter->Next())){ //loop on gTable
147  bn = (BaseNucleus *)gTable->GetValue(str);
148  if (bn->fZ == Z) {
149  return str->GetName();
150  }
151  }
152 
153  cout << " - ! - No Nucleus with Z = "<< Z << " found in the table - Please check Z" << endl;
154  return "";
155 }
156 
157 const char *BaseNucleus::WhichSymbol(Int_t Z, Int_t A){
158  if (gTable->GetSize() == 0) {
159  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
160  //return 0;
161  LoadTable();
162  }
163  TMapIter *iter = (TMapIter *)gTable->MakeIterator(); //Iterator to loop on gTable
164  TNamed *str;
165  BaseNucleus *bn;
166  while ((str = (TNamed *)iter->Next())){ //loop on gTable
167  bn = (BaseNucleus *)gTable->GetValue(str);
168  if (bn->fZ == Z) {
169  return Form("%d%s", A, str->GetName() );
170  }
171  }
172 
173  cout << " - ! - No Nucleus with Z = "<< Z << " found in the table - Please check Z" << endl;
174  return "";
175 }
176 
177 
178 Int_t BaseNucleus::WhichZ(const char *s){
179  if (s == NULL) return -1;
180  if (gTable->GetSize() == 0) {
181  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
182  //return -1;
183  LoadTable();
184  }
185  TString sym(s);
186  BaseNucleus *bn = (BaseNucleus *)gTable->GetValue(s);
187  if (bn == NULL) {
188  cout << " - !- No Nucleus named "<< s << " found in the table - Please check the symbol !!!" << endl;
189  return -1;
190  } else {
191  return bn->GetZ();
192  }
193 }
194 
195 const char* BaseNucleus::WhichFullName(const char *s){
196  if (s == NULL) return "";
197  if (gTable->GetSize() == 0) {
198  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
199  //return -1;
200  LoadTable();
201  }
202  TMapIter *iter = (TMapIter *)gTable->MakeIterator(); //Iterator to loop on gTable
203  TNamed *str;
204  while ((str = (TNamed *)iter->Next())){ //loop on gTable
205  if (strcmp(str->GetName(), s) == 0) {
206  return str->GetTitle();
207  }
208  }
209  cout << " - ! - No Nucleus with symbol = "<< s<< " found in the table - Please check the symbol" << endl;
210  return "";
211 }
212 
213 const char* BaseNucleus::WhichFullName(Int_t Z){
214  if (gTable->GetSize() == 0) {
215  //cout << "MapTable undefined, please use Gw::BaseNucleus::LoadTable()" <<endl;
216  //return 0;
217  LoadTable();
218  }
219  TMapIter *iter = (TMapIter *)gTable->MakeIterator(); //Iterator to loop on gTable
220  TNamed *str;
221  BaseNucleus *bn;
222  while ((str = (TNamed *)iter->Next())){ //loop on gTable
223  bn = (BaseNucleus *)gTable->GetValue(str);
224  if (bn->fZ == Z) {
225  return str->GetTitle();
226  }
227  }
228 
229  cout << " - ! - No Nucleus with Z = "<< Z << " found in the table - Please check Z" << endl;
230  return "";
231 }
232 
233 bool BaseNucleus::LoadTable(const char *xmlfile)
234 {
235  // method to Fill gTable, i.e. a map to give the correspondance between Z and the atomic symbol.
236  // The input file is a xml file (default is file "TableNucleus.xml", stored in /where/I/install/gammaware/ndb directory
237  // For each element, a couple of key/value is defined
238  // key = TNamed to store the symbol (name) and the full name (title)
239  // value = BaseNucleus object (default is the most abundant stable nucleus)
240 
241  TString strxml = xmlfile;
242  if (strxml.IsNull()) { //Set the default xml file
243  strxml = Gw::Env::GetPath("NdbPath"); if ( strxml.EndsWith("/") == kFALSE ) strxml += "/";
244  strxml += "TableNucleus.xml";
245  }
246  TDOMParser *domParser = new TDOMParser();
247  Int_t parsecode = domParser->ParseFile(strxml.Data()); //Check validity of the xmlfile respect to the corresponding .dtd file
248 
249  TMap table;
250 
251  if (parsecode < 0) {
252  cout << domParser->GetParseCodeMessage(parsecode) << endl;
253  return false;
254  }
255 
256  TXMLNode *node = domParser->GetXMLDocument()->GetRootNode();
257 
258  TXMLNode *node2 = NULL;
259  TXMLNode *node3 = NULL;
260 
261  TNamed *str = NULL;
262  BaseNucleus *bn = NULL;
263 
264  TString sym,name,strtmp;
265 
266  node = node->GetChildren();
267 
268  //Begin the reading
269  for (; node; node = node->GetNextNode()) {
270  if (node->GetNodeType() == TXMLNode::kXMLElementNode) { // First Element Node before the rootnode
271  if (strcmp(node->GetNodeName(), "nucleus") == 0) {
272  if (node->HasAttributes()) {
273  TList *attrList = node->GetAttributes();
274  TXMLAttr *attr = 0;
275  TIter next(attrList);
276  while ((attr=(TXMLAttr*)next())) {
277  if (strcmp(attr->GetName(), "symbol") == 0) {
278  sym = attr->GetValue();
279  }
280  if (strcmp(attr->GetName(), "name") == 0) {
281  name = attr->GetValue();
282  }
283  }
284  str = new TNamed(sym.Data(),name.Data());
285  //cout << str->GetName() << " " << endl;
286  }
287  node2 = node->GetChildren();
288  for ( ; node2; node2 = node2->GetNextNode()) {
289  if (node2->GetNodeType() == TXMLNode::kXMLElementNode) { // Element Node
290  if (strcmp(node2->GetNodeName(), "Z") == 0) {
291  node3 = node2->GetChildren();
292  strtmp = node3->GetContent();
293  bn = new BaseNucleus( strtmp.Atoi(), strtmp.Atoi() , 0); //A BaseNucleus with Z=A, N=0 is declared (Z is required in the xmlfile)
294  }
295 
296  if (strcmp(node2->GetNodeName(), "A") == 0) {
297  node3 = node2->GetChildren();
298  strtmp = node3->GetContent();
299  bn->SetA(strtmp.Atoi());
300  }
301 
302  if (strcmp(node2->GetNodeName(), "N") == 0) {
303  node3 = node2->GetChildren();
304  strtmp = node3->GetContent();
305  bn->SetN(strtmp.Atoi());
306  }
307 
308 
309  }
310  }
311  // Fill the map
312  table.Add(str,bn);
313  }
314  }
315  }
316 
317 
318  if (gTable->GetSize() != 0) { // A table was previously defined, delete all elements of gTable
319  gTable->DeleteAll(); // Objects are NOT deleted, TMap is not owner
320 
321  }
322  //copy all elements of the new table in gTable
323  TMapIter *iter = (TMapIter *)table.MakeIterator();
324  while ((str = (TNamed *)iter->Next())){
325  bn = (BaseNucleus *)table.GetValue(str);
326  gTable->Add(str,bn);
327  }
328 
329  //cout << "Table loaded from file \"" << strxml.Data() <<"\""<<endl;
330  return true;
331 
332 }
bool SetAZN(Int_t A, Int_t Z)
Definition: BaseNucleus.cpp:78
static const char * WhichFullName(const char *s)
return the full name corresponding to a symbol if it is found in the correspondence table...
void SetA(Int_t A)
Definition: BaseNucleus.h:141
BaseNucleus()
Set any components to 0.
Definition: BaseNucleus.cpp:62
virtual ~BaseNucleus()
Definition: BaseNucleus.cpp:73
static const char * WhichSymbol(Int_t Z)
return the symbol corresponding to Z if the nucleus is found in the correspondence table ...
void SetN(Int_t N)
Definition: BaseNucleus.h:143
void SetZ(Int_t Z)
Definition: BaseNucleus.h:142
static bool LoadTable(const char *xmlfile="")
Load the correspondence table between the symbol of the nucleus and Z from a XML file.
static Int_t WhichZ(const char *s)
return the number of protons (Z) corresponding to a symbol if the nucleus is found in the corresponde...
Int_t Z[3000]
Int_t GetZ() const
Definition: BaseNucleus.h:139
Base class for a nucleus which means its composition.
Definition: BaseNucleus.h:46
Int_t A[3000]
Int_t GetA() const
Definition: BaseNucleus.h:138
Int_t GetN() const
Definition: BaseNucleus.h:140
header file for a BaseNucleus
ClassImp(BaseNucleus)
ADF::LogMessage & endl(ADF::LogMessage &log)
to get information about the gammaware configuration
static const char * GetPath(const char *)
to get a particular GW environment variable
Definition: Env.cpp:47