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 TFile* f =
new TFile(outFileName.c_str(),
"recreate");
162 for (
unsigned int isl=0; isl<nslice; isl++) {
164 double sz = nslice > 1 ?
165 mmin[index[0]] + isl*( mmax[index[0]] - mmin[index[0]] )/( nslice - 1 ) :
166 (mmin[index[0]] + mmax[index[0]])/2. ;
168 p0.
array()[ index[0] ] = sz;
169 p1.
array()[ index[0] ] = sz;
171 cout <<
"scanning slice " << isl <<
" at "+XYZ+
" = " << sz << endl;
173 TString dirn =
"Slice"; dirn+=isl;
177 std::map < std::string , TH2F* > scanmap;
180 TString hn =
"slice"; hn+=isl; hn+=
"_X0";
181 TString hnn =
"X0 "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
183 for (
int j=1; j<=2; j++) {
184 if ( mmax[index[j]] - mmin[index[j]] < 1e-4 ) {
185 cout <<
"ERROR: max and min of axis are the same!" << endl;
195 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
196 scanmap[
"x0"] = h2slice;
198 hn =
"slice"; hn+=isl; hn+=
"_lambda";
199 hnn =
"lambda "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
200 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
201 scanmap[
"lambda"] = h2slice;
205 hn =
"slice"; hn+=isl; hn+=
"_Bx";
206 hnn =
"Bx[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
207 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
208 scanmap[
"Bx"] = h2slice;
210 hn =
"slice"; hn+=isl; hn+=
"_By";
211 hnn =
"By[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
212 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
213 scanmap[
"By"] = h2slice;
215 hn =
"slice"; hn+=isl; hn+=
"_Bz";
216 hnn =
"Bz[T] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
217 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
218 scanmap[
"Bz"] = h2slice;
220 hn =
"slice"; hn+=isl; hn+=
"_Ex";
221 hnn =
"Ex[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
222 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
223 scanmap[
"Ex"] = h2slice;
225 hn =
"slice"; hn+=isl; hn+=
"_Ey";
226 hnn =
"Ey[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
227 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
228 scanmap[
"Ey"] = h2slice;
230 hn =
"slice"; hn+=isl; hn+=
"_Ez";
231 hnn =
"Ez[V/m] "; hnn += XYZ; hnn+=
"="; hnn += Form(
"%7.3f",sz); hnn+=
" [cm]";
232 h2slice =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
233 scanmap[
"Ez"] = h2slice;
237 for (
int ix=1; ix<=h2slice->GetNbinsX(); ix++) {
239 double xmin = h2slice->GetXaxis()->GetBinLowEdge(ix);
240 double xmax = h2slice->GetXaxis()->GetBinUpEdge(ix);
242 for (
int iy=1; iy<=h2slice->GetNbinsY(); iy++) {
244 double ymin = h2slice->GetYaxis()->GetBinLowEdge(iy);
245 double ymax = h2slice->GetYaxis()->GetBinUpEdge(iy);
251 posV[index[1]] = 0.5*(xmin+xmax);
252 posV[index[2]] = 0.5*(ymin+ymax);
256 scanmap[
"Bx"]->SetBinContent(ix, iy, fieldV[0] / dd4hep::tesla );
257 scanmap[
"By"]->SetBinContent(ix, iy, fieldV[1] / dd4hep::tesla );
258 scanmap[
"Bz"]->SetBinContent(ix, iy, fieldV[2] / dd4hep::tesla );
261 scanmap[
"Ex"]->SetBinContent(ix, iy, fieldV[0] / ( dd4hep::volt/dd4hep::meter ) );
262 scanmap[
"Ey"]->SetBinContent(ix, iy, fieldV[1] / ( dd4hep::volt/dd4hep::meter ) );
263 scanmap[
"Ez"]->SetBinContent(ix, iy, fieldV[2] / ( dd4hep::volt/dd4hep::meter ) );
269 double sum_lambda(0);
271 double sum_length(0);
273 std::map < std::string , float > materialmap;
275 for (
unsigned int jx=0; jx<2*mm_count; jx++) {
277 double xcom = xmin + (1+jx)*( xmax - xmin )/(mm_count+1.);
278 p0.
array()[index[1]] = xcom; p0.
array()[index[2]] = ymin;
279 p1.
array()[index[1]] = xcom; p1.
array()[index[2]] = ymax;
281 double ycom = ymin + (jx-mm_count+1)*( ymax - ymin )/(mm_count+1.);
282 p0.
array()[index[1]] = xmin; p0.
array()[index[2]] = ycom;
283 p1.
array()[index[1]] = xmax; p1.
array()[index[2]] = ycom;
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;
307 scanmap[
"x0"]->SetBinContent(ix, iy, sum_x0/sum_length);
308 scanmap[
"lambda"]->SetBinContent(ix, iy, sum_lambda/sum_length);
310 for ( std::map < std::string , float >::iterator jj = materialmap.begin(); jj!=materialmap.end(); jj++) {
311 if ( scanmap.find( jj->first )==scanmap.end() ) {
312 hn =
"slice"; hn+=isl; hn+=
"_"+jj->first;
313 hnn = jj->first; hnn +=
" "+XYZ; hnn+=
"=";
315 hnn += Form(
"%7.3f",sz);
317 scanmap[jj->first] =
new TH2F( hn, hnn, nbins, mmin[index[1]], mmax[index[1]], nbins, mmin[index[2]], mmax[index[2]] );
319 scanmap[jj->first]->SetBinContent(ix, iy, jj->second / sum_length );
327 for ( std::map < std::string , TH2F* >::iterator jj = scanmap.begin(); jj!=scanmap.end(); jj++) {
328 jj->second->SetOption(
"zcol");
329 jj->second->GetXaxis()->SetTitle(labx);
330 jj->second->GetYaxis()->SetTitle(laby);
339 int main(
int argc,
char** argv) {
344 std::cout <<
"Got uncaught exception: " << e.what() << std::endl;
347 std::cout <<
"Got UNKNOWN uncaught exception." << std::endl;