52 Handler() { SetErrorHandler(Handler::print); }
53 static void print(
int level, Bool_t abort,
const char *location,
const char *msg) {
54 if ( level > kInfo || abort )
::printf(
"%s: %s\n", location, msg);
57 std::cout <<
" usage: graphicalScan compact.xml axis xMin xMax yMin yMax zMin zMax nSlices nBins nSamples FieldOrMaterial OutfileName" << std::endl
58 <<
" axis (X, Y, or Z) : perpendicular to the slices" << std::endl
59 <<
" xMin xMax yMin yMax zMin zMax : range of scans " << std::endl
60 <<
" nSlices : number of slices (equally spaced along chose axis)" << std::endl
61 <<
" nBins : number of bins along each axis of histograms" << std::endl
62 <<
" nSamples : the number of times each bin is sampled " << std::endl
63 <<
" FieldOrMaterial : scan field or material? F = field, M = material, FM or MF = both" << std::endl
64 <<
" OutfileName : output root file name" << std::endl
65 <<
" -> produces graphical scans of material and/or fields defined in a compact xml description"
77 std::string inFile = argv[1];
79 std::string XYZ = argv[2];
82 unsigned int index[3] = {99,99,99};
83 if ( XYZ==
"x" || XYZ==
"X" ) {
89 }
else if ( XYZ==
"y" || XYZ==
"Y" ) {
95 }
else if ( XYZ==
"z" || XYZ==
"Z" ) {
102 cout <<
"invalid XYZ" << endl;
106 double x0,y0,z0,x1,y1,z1;
107 unsigned int nslice, nbins, mm_count;
108 std::stringstream sstr;
110 argv[3] <<
" " << argv[4] <<
" " << argv[5] <<
" " <<
111 argv[6] <<
" " << argv[7] <<
" " << argv[8] <<
" " <<
112 argv[9] <<
" " << argv[10] <<
" " << argv[11] <<
"NONE";
113 sstr >> x0 >> x1 >> y0 >> y1 >> z0 >> z1 >> nslice >> nbins >> mm_count ;
114 if ( !sstr.good() ) {
119 std::string FM = argv[12];
120 std::string outFileName = argv[13];
122 if ( x0>x1 ) {
double temp=x0; x0=x1; x1=temp; }
123 if ( y0>y1 ) {
double temp=y0; y0=y1; y1=temp; }
124 if ( z0>z1 ) {
double temp=z0; z0=z1; z1=temp; }
126 if ( ! (nbins>0 && nbins<USHRT_MAX && nslice>0 && nslice<USHRT_MAX) ) {
127 cout <<
"funny # bins/slices " << endl;
131 bool scanField(
false);
132 bool scanMaterial(
false);
133 if ( FM==
"f" || FM==
"F" ) {
135 }
else if ( FM==
"m" || FM==
"M" ) {
137 }
else if ( FM==
"fm" || FM==
"FM" || FM==
"mf" || FM==
"MF" ) {
141 cout <<
"invalid field/material flag: use one of f/F/m/M/fm/FM/mf/MF" << endl;
145 double mmin[3]={x0,y0,z0};
146 double mmax[3]={x1,y1,z1};
156 bool found_tessellated =
false;
157 TFile* f =
new TFile(outFileName.c_str(),
"recreate");
161 for (
unsigned int isl=0; isl<nslice; isl++) {
163 double sz = nslice > 1 ?
164 mmin[index[0]] + isl*( mmax[index[0]] - mmin[index[0]] )/( nslice - 1 ) :
165 (mmin[index[0]] + mmax[index[0]])/2. ;
167 p0.
array()[ index[0] ] = sz;
168 p1.
array()[ index[0] ] = sz;
170 cout <<
"scanning slice " << isl <<
" at "+XYZ+
" = " << sz << endl;
172 TString dirn =
"Slice"; dirn+=isl;
176 std::map < std::string , TH2F* > scanmap;
179 TString hn =
"slice"; hn+=isl; hn+=
"_X0";
180 TString hnn =
"X0 "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
182 for (
int j=1; j<=2; j++) {
183 if ( mmax[index[j]] - mmin[index[j]] < 1e-4 ) {
184 cout <<
"ERROR: max and min of axis are the same!" << endl;
194 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
195 scanmap[
"x0"] = h2slice;
197 hn =
"slice"; hn+=isl; hn+=
"_lambda";
198 hnn =
"lambda "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
199 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
200 scanmap[
"lambda"] = h2slice;
204 hn =
"slice"; hn+=isl; hn+=
"_Bx";
205 hnn =
"Bx[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
206 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
207 scanmap[
"Bx"] = h2slice;
209 hn =
"slice"; hn+=isl; hn+=
"_By";
210 hnn =
"By[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
211 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
212 scanmap[
"By"] = h2slice;
214 hn =
"slice"; hn+=isl; hn+=
"_Bz";
215 hnn =
"Bz[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
216 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
217 scanmap[
"Bz"] = h2slice;
219 hn =
"slice"; hn+=isl; hn+=
"_Ex";
220 hnn =
"Ex[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
221 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
222 scanmap[
"Ex"] = h2slice;
224 hn =
"slice"; hn+=isl; hn+=
"_Ey";
225 hnn =
"Ey[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
226 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
227 scanmap[
"Ey"] = h2slice;
229 hn =
"slice"; hn+=isl; hn+=
"_Ez";
230 hnn =
"Ez[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
231 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
232 scanmap[
"Ez"] = h2slice;
236 for (
int ix=1; ix<=h2slice->GetNbinsX(); ix++) {
238 double xmin = h2slice->GetXaxis()->GetBinLowEdge(ix);
239 double xmax = h2slice->GetXaxis()->GetBinUpEdge(ix);
241 for (
int iy=1; iy<=h2slice->GetNbinsY(); iy++) {
243 double ymin = h2slice->GetYaxis()->GetBinLowEdge(iy);
244 double ymax = h2slice->GetYaxis()->GetBinUpEdge(iy);
250 posV[index[1]] = 0.5*(xmin+xmax);
251 posV[index[2]] = 0.5*(ymin+ymax);
255 scanmap[
"Bx"]->SetBinContent(ix, iy, fieldV[0] / dd4hep::tesla );
256 scanmap[
"By"]->SetBinContent(ix, iy, fieldV[1] / dd4hep::tesla );
257 scanmap[
"Bz"]->SetBinContent(ix, iy, fieldV[2] / dd4hep::tesla );
260 scanmap[
"Ex"]->SetBinContent(ix, iy, fieldV[0] / ( dd4hep::volt/dd4hep::meter ) );
261 scanmap[
"Ey"]->SetBinContent(ix, iy, fieldV[1] / ( dd4hep::volt/dd4hep::meter ) );
262 scanmap[
"Ez"]->SetBinContent(ix, iy, fieldV[2] / ( dd4hep::volt/dd4hep::meter ) );
268 double sum_lambda(0);
270 double sum_length(0);
271 std::map < std::string , float > materialmap;
273 for (
unsigned int jx=0; jx<2*mm_count; jx++) {
275 double xcom = xmin + (1+jx)*( xmax - xmin )/(mm_count+1.);
276 p0.
array()[index[1]] = xcom; p0.
array()[index[2]] = ymin;
277 p1.
array()[index[1]] = xcom; p1.
array()[index[2]] = ymax;
279 double ycom = ymin + (jx-mm_count+1)*( ymax - ymin )/(mm_count+1.);
280 p0.
array()[index[1]] = xmin; p0.
array()[index[2]] = ycom;
281 p1.
array()[index[1]] = xmax; p1.
array()[index[2]] = ycom;
285 const auto& places = scan.
places;
287 for(
unsigned i=0,n=materials.size();i<n;++i){
288 TGeoMaterial* mat = materials[i].first->GetMaterial();
289 double length = materials[i].second;
290 sum_length += length;
291 double nx0 = length / mat->GetRadLen();
293 double nLambda = length / mat->GetIntLen();
294 sum_lambda += nLambda;
296 std::string mname = mat->GetName();
297 if ( materialmap.find( mname )!=materialmap.end() ) {
298 materialmap[mname]+=length;
300 materialmap[mname]=length;
304 if( !found_tessellated ) {
305 for(
const auto& p : places ) {
306 Volume volume(p.first.volume());
308 if ( shape->IsA() == TGeoTessellated::Class() ) {
309 found_tessellated =
true;
315 scanmap[
"x0"]->SetBinContent(ix, iy, sum_x0/sum_length);
316 scanmap[
"lambda"]->SetBinContent(ix, iy, sum_lambda/sum_length);
318 for ( std::map < std::string , float >::iterator jj = materialmap.begin(); jj!=materialmap.end(); jj++) {
319 if ( scanmap.find( jj->first )==scanmap.end() ) {
320 hn =
"slice"; hn+=isl; hn+=
"_"+jj->first;
321 hnn = jj->first; hnn +=
" "+XYZ; hnn+=
"=";
323 hnn += Form(
"%7.3f",sz);
325 scanmap[jj->first] =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
327 scanmap[jj->first]->SetBinContent(ix, iy, jj->second / sum_length );
333 for ( std::map < std::string , TH2F* >::iterator jj = scanmap.begin(); jj!=scanmap.end(); jj++) {
334 jj->second->SetOption(
"zcol");
335 jj->second->GetXaxis()->SetTitle(labx);
336 jj->second->GetYaxis()->SetTitle(laby);
340 if ( scanMaterial && found_tessellated ) {
341 const char* line =
" +------------------------------------------------------------"
342 "--------------------------------------------------------------------------------------\n";
344 ::printf(
" | WARNING: Tessellated shape were encountered during the volume traversal.\n");
345 ::printf(
" | WARNING: The results of the material scan(s) are unreliable!\n");
354 int main(
int argc,
char** argv) {
359 std::cout <<
"Got uncaught exception: " << e.what() << std::endl;
362 std::cout <<
"Got UNKNOWN uncaught exception." << std::endl;