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