CMS 3D CMS Logo

PrintMaterialBudgetInfo.cc
Go to the documentation of this file.
5 
15 
16 #include "G4Run.hh"
17 #include "G4PhysicalVolumeStore.hh"
18 #include "G4LogicalVolumeStore.hh"
19 #include "G4VPhysicalVolume.hh"
20 #include "G4LogicalVolume.hh"
21 #include "G4VSolid.hh"
22 #include "G4Material.hh"
23 #include "G4NavigationHistory.hh"
24 #include "G4Track.hh"
25 #include "G4VisAttributes.hh"
26 #include "G4UserLimits.hh"
27 #include "G4TransportationManager.hh"
28 #include "G4UnitsTable.hh"
29 #include "Randomize.hh"
30 
31 #include <iostream>
32 #include <fstream>
33 #include <set>
34 #include <vector>
35 #include <string>
36 
37 typedef std::map<G4VPhysicalVolume*, G4VPhysicalVolume*, std::less<G4VPhysicalVolume*> > mpvpv;
38 typedef std::multimap<G4LogicalVolume*, G4VPhysicalVolume*, std::less<G4LogicalVolume*> > mmlvpv;
39 
41  public Observer<const BeginOfJob*>,
42  public Observer<const BeginOfRun*> {
43 public:
45  ~PrintMaterialBudgetInfo() override;
46 
47 private:
48  void update(const BeginOfJob* job) override{};
49  void update(const BeginOfRun* run) override;
50  void dumpHeader(std::ostream& out = G4cout);
51  void dumpLaTeXHeader(std::ostream& out = G4cout);
52  void dumpHierarchyLeaf(G4VPhysicalVolume* pv,
53  G4LogicalVolume* lv,
54  unsigned int leafDepth,
55  std::ostream& weightOut = G4cout,
56  std::ostream& texOut = G4cout);
57  void printInfo(G4VPhysicalVolume* pv,
58  G4LogicalVolume* lv,
59  unsigned int leafDepth,
60  std::ostream& weightOut = G4cout,
61  std::ostream& texOut = G4cout);
62  void dumpElementMassFraction(std::ostream& elementOut = G4cout);
63  void dumpLaTeXFooter(std::ostream& out = G4cout);
64 
65 private:
67  int nchar;
69  G4VPhysicalVolume* theTopPV;
70  G4NavigationHistory fHistory;
72  unsigned int levelFound;
73  std::ofstream weightOutputFile;
74  std::ofstream elementOutputFile;
75  std::ofstream texOutputFile;
76  std::vector<std::string> elementNames;
77  std::vector<double> elementTotalWeight;
78  std::vector<double> elementWeightFraction;
79  //
82 };
83 
85  name = p.getUntrackedParameter<std::string>("Name", "*");
86  nchar = name.find('*');
87  name.assign(name, 0, nchar);
88  G4cout << "PrintMaterialBudget selected volume " << name << G4endl;
89  volumeFound = false;
90  std::string weightFileName = name + ".weight";
91  weightOutputFile.open(weightFileName.c_str());
92  std::string elementFileName = name + ".element";
93  elementOutputFile.open(elementFileName.c_str());
94  std::string texFileName = name + "_table.tex";
95  texOutputFile.open(texFileName.c_str());
96  G4cout << "PrintMaterialBudget output file " << weightFileName << G4endl;
97  G4cout << "PrintMaterialBudget output file " << elementFileName << G4endl;
98  G4cout << "PrintMaterialBudget output file " << texFileName << G4endl;
99  elementNames.clear();
100  elementTotalWeight.clear();
101  elementWeightFraction.clear();
102 }
103 
105 
107  G4Random::setTheEngine(new CLHEP::RanecuEngine);
108  // Physical Volume
109  theTopPV = G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume();
110  assert(theTopPV);
111  // Logical Volume
112  G4LogicalVolume* lv = theTopPV->GetLogicalVolume();
113  unsigned int leafDepth = 0;
114  // the first time fill the vectors of elements
115  if (elementNames.empty() && elementTotalWeight.empty() && elementWeightFraction.empty()) {
116  for (unsigned int iElement = 0; iElement < G4Element::GetNumberOfElements();
117  iElement++) { // first element in table is 0
118  elementNames.push_back("rr");
119  elementTotalWeight.push_back(0);
120  elementWeightFraction.push_back(0);
121  }
122  }
128  //
129 }
130 
132  out << "Geom."
133  << "\t"
134  << "Volume"
135  << "\t"
136  << "\t"
137  << "Copy"
138  << "\t"
139  << "Solid"
140  << "\t"
141  << "\t"
142  << "Material"
143  << "\t"
144  << "Density"
145  << "\t"
146  << "\t"
147  << "Mass"
148  << "\t"
149  << "\t" << G4endl;
150  out << "Level"
151  << "\t"
152  << "Name"
153  << "\t"
154  << "\t"
155  << "Number"
156  << "\t"
157  << "Name"
158  << "\t"
159  << "\t"
160  << "Name"
161  << "\t"
162  << "\t"
163  << "[g/cm3]"
164  << "\t"
165  << "\t"
166  << "[g] "
167  << "\t"
168  << "\t" << G4endl;
169 }
170 
172  out << "\\begin{table}[h!]" << G4endl << " \\caption{\\textsf {" << name << "} volume list.}" << G4endl
173  << " \\label{tab: " << name << "}" << G4endl << " \\begin{center}" << G4endl << " \\begin{tabular}{ccccccc}"
174  << G4endl << " \\hline" << G4endl;
175  out << " Geom."
176  << "\t & "
177  << " Volume"
178  << "\t & "
179  << " Copy"
180  << "\t & "
181  << " Solid"
182  << "\t & "
183  << " Material"
184  << "\t & "
185  << " Density"
186  << "\t & "
187  << " Mass"
188  << "\t \\\\ " << G4endl;
189  out << " Level"
190  << "\t & "
191  << " Name"
192  << "\t & "
193  << " Number"
194  << "\t & "
195  << " Name"
196  << "\t & "
197  << " Name"
198  << "\t & "
199  << " "
200  << "\t & "
201  << " "
202  << "\t \\\\ " << G4endl << " \\hline\\hline" << G4endl;
203 }
204 
206  out << " \\hline" << G4endl << " \\end{tabular}" << G4endl << " \\end{center}" << G4endl << "\\end{table}"
207  << G4endl;
208 }
209 
211  G4VPhysicalVolume* pv, G4LogicalVolume* lv, unsigned int leafDepth, std::ostream& weightOut, std::ostream& texOut) {
212  if (volumeFound && (leafDepth <= levelFound))
213  return;
214  if (volumeFound && (leafDepth > levelFound))
215  printInfo(pv, lv, leafDepth, weightOut, texOut);
216 
217  // choose mother volume
218  std::string lvname = lv->GetName();
219  lvname.assign(lvname, 0, nchar);
220  if (lvname == name) {
221  volumeFound = true;
222  levelFound = leafDepth;
223  printInfo(pv, lv, leafDepth, weightOut, texOut);
224  texOut << " \\hline" << G4endl;
225  }
226 
227  //----- Get LV daughters from list of PV daughters
228  mmlvpv lvpvDaughters;
229  std::set<G4LogicalVolume*> lvDaughters;
230  int NoDaughters = lv->GetNoDaughters();
231  while ((NoDaughters--) > 0) {
232  G4VPhysicalVolume* pvD = lv->GetDaughter(NoDaughters);
233  lvpvDaughters.insert(mmlvpv::value_type(pvD->GetLogicalVolume(), pvD));
234  lvDaughters.insert(pvD->GetLogicalVolume());
235  }
236 
237  std::set<G4LogicalVolume*>::const_iterator scite;
238  mmlvpv::const_iterator mmcite;
239 
240  //----- Dump daughters PV and LV
241  for (scite = lvDaughters.begin(); scite != lvDaughters.end(); scite++) {
242  std::pair<mmlvpv::iterator, mmlvpv::iterator> mmER = lvpvDaughters.equal_range(*scite);
243  //----- Dump daughters PV of this LV
244  for (mmcite = mmER.first; mmcite != mmER.second; mmcite++)
245  dumpHierarchyLeaf((*mmcite).second, *scite, leafDepth + 1, weightOut, texOut);
246  }
247 }
248 
250  G4VPhysicalVolume* pv, G4LogicalVolume* lv, unsigned int leafDepth, std::ostream& weightOut, std::ostream& texOut) {
251  double density = lv->GetMaterial()->GetDensity();
252  double weight = lv->GetMass(false, false);
253 
254  std::string volumeName = lv->GetName();
255  if (volumeName.size() < 8)
256  volumeName.append("\t");
257 
258  std::string solidName = lv->GetSolid()->GetName();
259  if (solidName.size() < 8)
260  solidName.append("\t");
261 
262  std::string materialName = lv->GetMaterial()->GetName();
263  if (materialName.size() < 8)
264  materialName.append("\t");
265 
266  //----- dump info
267  weightOut << leafDepth << "\t" << volumeName << "\t" << pv->GetCopyNo() << "\t" << solidName << "\t" << materialName
268  << "\t" << G4BestUnit(density, "Volumic Mass") << "\t" << G4BestUnit(weight, "Mass") << "\t" << G4endl;
269  //
270  texOut << "\t" << leafDepth << "\t & " << stringLaTeXUnderscore(volumeName) << "\t & " << pv->GetCopyNo() << "\t & "
271  << stringLaTeXUnderscore(solidName) << "\t & " << stringLaTeXUnderscore(materialName) << "\t & "
272  << stringLaTeXSuperscript(G4BestUnit(density, "Volumic Mass")) << "\t & "
273  << stringLaTeXSuperscript(G4BestUnit(weight, "Mass")) << "\t \\\\ " << G4endl;
274  //
275  for (unsigned int iElement = 0; iElement < (unsigned int)lv->GetMaterial()->GetNumberOfElements(); iElement++) {
276  // exclude Air in element weight fraction computation
277  if (materialName.find("Air")) {
278  std::string elementName = lv->GetMaterial()->GetElement(iElement)->GetName();
279  double elementMassFraction = lv->GetMaterial()->GetFractionVector()[iElement];
280  double elementWeight = weight * elementMassFraction;
281  unsigned int elementIndex = (unsigned int)lv->GetMaterial()->GetElement(iElement)->GetIndex();
282  elementNames[elementIndex] = elementName;
283  elementTotalWeight[elementIndex] += elementWeight;
284  }
285  }
286 }
287 
288 void PrintMaterialBudgetInfo::dumpElementMassFraction(std::ostream& elementOut) {
289  // calculate mass fraction
290  double totalWeight = 0.0;
291  double totalFraction = 0.0;
292  for (unsigned int iElement = 0; iElement < (unsigned int)elementTotalWeight.size(); iElement++) {
293  totalWeight += elementTotalWeight[iElement];
294  }
295  // calculate element mass fractions
296  for (unsigned int iElement = 0; iElement < (unsigned int)elementTotalWeight.size(); iElement++) {
297  elementWeightFraction[iElement] = elementTotalWeight[iElement] / totalWeight;
298  totalFraction += elementWeightFraction[iElement];
299  }
300  // header
301  elementOut << "Element"
302  << "\t\t"
303  << "Index"
304  << "\t"
305  << "Total Mass"
306  << "\t"
307  << "Mass Fraction "
308  << "\t" << G4endl;
309  // dump
310  for (unsigned int iElement = 0; iElement < (unsigned int)elementTotalWeight.size(); iElement++) {
311  if (elementNames[iElement] != "rr") {
312  if (elementNames[iElement].size() < 8)
313  elementNames[iElement].append("\t");
314  elementOut << elementNames[iElement] << "\t" << iElement << "\t"
315  << G4BestUnit(elementTotalWeight[iElement], "Mass") << "\t" << elementWeightFraction[iElement]
316  << G4endl;
317  }
318  }
319  elementOut << "\n\t\tTotal Weight without Air " << G4BestUnit(totalWeight, "Mass") << "\tTotal Fraction "
320  << totalFraction << G4endl;
321 }
322 
324  // To replace '\' with '\_' to compile LaTeX output
325  std::string stringoutput;
326 
327  for (unsigned int i = 0; i < stringname.length(); i++) {
328  if (stringname.substr(i, 1) == "_") {
329  stringoutput += "\\_";
330  } else {
331  stringoutput += stringname.substr(i, 1);
332  }
333  }
334 
335  return stringoutput;
336 }
337 
339  // To replace 'm3' with 'm$^3$' to compile LaTeX output
340  std::string stringoutput = stringname.substr(0, 1);
341 
342  for (unsigned int i = 1; i < stringname.length(); i++) {
343  if (stringname.substr(i - 1, 1) == "m" && stringname.substr(i, 1) == "3") {
344  stringoutput += "$^3$";
345  } else {
346  stringoutput += stringname.substr(i, 1);
347  }
348  }
349 
350  return stringoutput;
351 }
352 
355 
size
Write out results.
std::multimap< G4LogicalVolume *, G4VPhysicalVolume *, std::less< G4LogicalVolume * > > mmlvpv
#define DEFINE_SIMWATCHER(type)
void dumpLaTeXFooter(std::ostream &out=G4cout)
std::vector< std::string > elementNames
Definition: weight.py:1
assert(be >=bs)
void dumpLaTeXHeader(std::ostream &out=G4cout)
std::vector< double > elementWeightFraction
std::vector< double > elementTotalWeight
void dumpElementMassFraction(std::ostream &elementOut=G4cout)
void dumpHierarchyLeaf(G4VPhysicalVolume *pv, G4LogicalVolume *lv, unsigned int leafDepth, std::ostream &weightOut=G4cout, std::ostream &texOut=G4cout)
PrintMaterialBudgetInfo(edm::ParameterSet const &p)
def pv(vc)
Definition: MetAnalyzer.py:7
std::multimap< G4LogicalVolume *, G4VPhysicalVolume *, std::less< G4LogicalVolume * > > mmlvpv
std::string stringLaTeXSuperscript(std::string stringname)
std::string stringLaTeXUnderscore(std::string stringname)
void update(const BeginOfJob *job) override
This routine will be called when the appropriate signal arrives.
void printInfo(G4VPhysicalVolume *pv, G4LogicalVolume *lv, unsigned int leafDepth, std::ostream &weightOut=G4cout, std::ostream &texOut=G4cout)
std::map< G4VPhysicalVolume *, G4VPhysicalVolume *, std::less< G4VPhysicalVolume * > > mpvpv
void dumpHeader(std::ostream &out=G4cout)