-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate_material_tables.cpp
217 lines (194 loc) · 7.93 KB
/
generate_material_tables.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <cstdlib>
#include <iostream>
#include <fmt/format.h>
#include "g4dRIChOptics.hh"
#include "surfaceEnums.h"
#include <TCanvas.h>
#include <TGraphErrors.h>
#include <TAxis.h>
#include <TFile.h>
///////////////////////////////////
// SETTINGS
const int aerOptModel = 3; // aerogel optical model used to estimate the refractive Index
const double filter_thr = 300*nm; // wavelength filter cutoff
const bool vsWavelength = true; // if true, make plots vs. wavelength
const std::string xmlOutput = "out/optical_materials_drich.xml";
const TString root_file_name = "out/optical_materials_drich.root";
///////////////////////////////////
// other global vars
std::FILE *xmlFile;
// ===========================================================================
// structures for preferred units
class UnitDef {
public:
UnitDef(double divisor_, std::string name_) : divisor(divisor_), name(name_), xml(""), title("") {
if(name!="") {
xml = "*" + name;
title = " [" + name + "]";
}
}
double divisor;
std::string name, xml, title;
};
// ===========================================================================
// extends g4dRIChOptics class with plots and XML printing
template<class MAT> class MaterialTable {
public:
MAT *mpt;
std::string name;
MaterialTable(MAT *mpt_, std::string name_) : mpt(mpt_), name(name_) {
if(name=="C2F6" or name=="C4F10") PreferredUnits = &PreferredUnits2;
else PreferredUnits = &PreferredUnits1;
}
~MaterialTable() { if(mpt!=nullptr) delete mpt; };
// print XML matrices, for `optical_materials.xml`
void PrintXML(bool isSurface=false, G4String detectorName="DRICH") {
fmt::print(xmlFile,"\n<!-- {:_^60} -->\n\n",name);
// function to print a row
auto PrintRow = [] (int indentation, UnitDef units) {
return [indentation,&units] (G4double energy, G4double value) {
fmt::print(xmlFile,"{:{}}{:<#.6g}{} {:>#.7g}{}\n",
"", indentation,
energy/eV, "*eV",
value/units.divisor, units.xml
);
};
};
if(isSurface) {
auto surf = mpt->getSurface();
fmt::print(xmlFile,"{:4}<opticalsurface name=\"{}_{}\" model=\"{}\" finish=\"{}\" type=\"{}\">\n",
"",
mpt->getLogicalVName(),
detectorName,
surfaceEnum::GetModel(surf),
surfaceEnum::GetFinish(surf),
surfaceEnum::GetType(surf)
);
for(const auto& propName : mpt->getMaterialPropertyNames()) {
fmt::print(xmlFile,"{:6}<property name=\"{}\" coldim=\"2\" values=\"\n", "", propName);
mpt->loopMaterialPropertyTable(propName,PrintRow(8,PreferredUnits->at(propName)));
fmt::print(xmlFile,"{:8}\"/>\n","");
}
fmt::print(xmlFile,"{:4}</opticalsurface>\n","");
} else {
for(const auto& propName : mpt->getMaterialPropertyNames()) {
fmt::print(xmlFile,"{:4}<matrix name=\"{}__{}_{}\" coldim=\"2\" values=\"\n",
"",
propName,
mpt->getMaterialName(),
detectorName
);
mpt->loopMaterialPropertyTable(propName,PrintRow(6,PreferredUnits->at(propName)));
fmt::print(xmlFile,"{:6}\"/>\n","");
}
}
} // PrintXML
// draw plots of material property tables
void DrawPlots() {
TString canvName("materialProperties_"+name);
TString pngName("out/"+canvName+".png");
int ncols = 2;
int nrows = 1+(mpt->getMaterialPropertyTableSize()-1)/ncols;
if(nrows==1) ncols=mpt->getMaterialPropertyTableSize();
auto canv = new TCanvas(canvName,canvName,ncols*800,nrows*600);
canv->Divide(ncols,nrows);
int pad=0;
for(const auto& propName : mpt->getMaterialPropertyNames()) {
fmt::print("Plotting property {}\n", propName);
auto units = PreferredUnits->at(propName);
canv->cd(++pad);
canv->GetPad(pad)->SetGrid(1,1);
canv->GetPad(pad)->SetLeftMargin(0.2);
canv->GetPad(pad)->SetRightMargin(0.15);
auto graph = new TGraphErrors();
graph->SetName(TString("graph_"+name+"_"+propName));
std::string xTitle = vsWavelength ? "wavelength [nm]" : "energy [eV]";
graph->SetTitle(TString(name+" "+propName+units.title+";"+xTitle));
auto makeGraph = [&graph,&units] (G4double energy, G4double value) {
auto xval = vsWavelength ? g4dRIChOptics::e2wl(energy)/nm : energy/eV;
graph->AddPoint(xval, value / units.divisor);
};
mpt->loopMaterialPropertyTable(propName,makeGraph, vsWavelength);
graph->SetMarkerStyle(kFullCircle);
graph->SetMarkerColor(kAzure);
graph->GetXaxis()->SetLabelSize(0.05);
graph->GetYaxis()->SetLabelSize(0.05);
graph->GetXaxis()->SetTitleOffset(1.3);
graph->Draw("AP");
graph->Write();
}
canv->SaveAs(pngName);
}
// sets of preferred units
const std::map<G4String,UnitDef> *PreferredUnits;
const std::map<G4String,UnitDef> PreferredUnits1 = {
{ "RINDEX", UnitDef( 1., "" )},
{ "GROUPVEL", UnitDef( mm/ns, "mm/ns" )},
{ "RAYLEIGH", UnitDef( mm, "mm" )},
{ "ABSLENGTH", UnitDef( mm, "mm" )},
{ "REFLECTIVITY", UnitDef( 1., "" )},
{ "REALRINDEX", UnitDef( 1., "" )},
{ "IMAGINARYRINDEX", UnitDef( 1., "" )},
{ "EFFICIENCY", UnitDef( 1., "" )}
};
const std::map<G4String,UnitDef> PreferredUnits2 = {
{ "RINDEX", UnitDef( 1., "" )},
{ "GROUPVEL", UnitDef( mm/ns, "mm/ns" )},
{ "RAYLEIGH", UnitDef( m, "m" )},
{ "ABSLENGTH", UnitDef( m, "m" )},
{ "REFLECTIVITY", UnitDef( 1., "" )},
{ "REALRINDEX", UnitDef( 1., "" )},
{ "IMAGINARYRINDEX", UnitDef( 1., "" )},
{ "EFFICIENCY", UnitDef( 1., "" )}
};
}; // class MaterialTable
// ===========================================================================
int main(int argc, char** argv) {
// start XML file and ROOT file
xmlFile = std::fopen(xmlOutput.c_str(),"w");
auto root_file = new TFile(root_file_name,"RECREATE");
// build detector by text file
fmt::print("[+] read model text file\n");
G4tgbVolumeMgr *volmgr = G4tgbVolumeMgr::GetInstance();
auto model_file = G4String("text/drich-materials.txt");
volmgr->AddTextFile(model_file);
fmt::print("[+] construct detector from text file\n");
G4VPhysicalVolume *vesselPhysVol = volmgr->ReadAndConstructDetector();
fmt::print("[+] done construction\n");
// produce material property tables ///////////////////////
// aerogel
MaterialTable Aerogel(new g4dRIChAerogel("Aerogel"),"Aerogel");
Aerogel.mpt->setOpticalParams(aerOptModel);
Aerogel.PrintXML();
Aerogel.DrawPlots();
// acrylic filter
MaterialTable Acrylic(new g4dRIChFilter("Acrylic"),"Acrylic");
Acrylic.mpt->setOpticalParams(filter_thr);
Acrylic.PrintXML();
Acrylic.DrawPlots();
// gas
// - C2F6
MaterialTable C2F6(new g4dRIChGas("C2F6"),"C2F6");
C2F6.mpt->setOpticalParams();
C2F6.PrintXML();
C2F6.DrawPlots();
// - C4F10
MaterialTable C4F10(new g4dRIChGas("C4F10"),"C4F10");
C4F10.mpt->setOpticalParams();
C4F10.PrintXML(false,"PFRICH");
C4F10.DrawPlots();
// mirror surface
MaterialTable MirrorSurface(new g4dRIChMirror("MirrorSurface"),"MirrorSurface");
MirrorSurface.mpt->setOpticalParams("ciDRICH");
MirrorSurface.PrintXML(true);
MirrorSurface.DrawPlots();
// photo sensor surface
MaterialTable SensorSurface(new g4dRIChPhotosensor("SensorSurface"),"SensorSurface");
SensorSurface.mpt->setOpticalParams("ciDRICH");
SensorSurface.PrintXML(true);
SensorSurface.DrawPlots();
// cleanup
std::fclose(xmlFile);
root_file->Close();
fmt::print("\nwrote XML nodes to {}\n\n",xmlOutput);
} // main