CMS 3D CMS Logo

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