CMS 3D CMS Logo

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