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