47 {
48 cout << "JMarkovPathIntegrator" << endl
49 << "Written by Martijn Jongen" << endl
50 << endl ;
51
52 gRandom->SetSeed(0) ;
53
54 string ifname_paths ;
55 string ifname_model ;
56 string ofname ;
57 int nsamples ;
58 int nscat ;
59 int nbins ;
60
61 try {
63 zap[
"f"] =
make_field(ifname_paths,
"input file name (binary file containing JPhotonPaths)") ;
64 zap[
"m"] =
make_field(ifname_model,
"input file name (root file containing JScatteringModel)") ;
65 zap[
"o"] =
make_field(ofname,
"OPTIONAL file name for root output") =
"" ;
66 zap[
"n"] =
make_field(nscat,
"number of scatterings") ;
67 zap[
"N"] =
make_field(nsamples,
"number of samples") = 10000 ;
68 zap[
"nbins"] =
make_field(nbins,
"nbins of vertex distribution histograms") = 1000 ;
69
70 if (zap.
read(argc, argv) != 0) {
71 return 1 ;
72 }
73 }
74 catch(const exception &error) {
75 cout << error.what() << endl ;
76 cout << "Type '" << argv[0] << " -h!' to display the command-line options." << endl ;
77 exit(1) ;
78 }
79
80 cout << "SETTINGS: " << endl ;
81 cout << " Input file (paths) = '" << ifname_paths << "'" << endl ;
82 cout << " Input file (model) = '" << ifname_model << "'" << endl ;
83 cout << " Output file = '" << ofname << "'" << endl ;
84 cout << " nsamples = " << nsamples << endl ;
85 cout << " nscat = " << nscat << endl ;
86 cout << " nbins = " << nbins << endl ;
87 cout << endl ;
88
89 int nvert = nscat + 2 ;
90
91
92 cout << "Loading scattering model" << endl ;
94
95 TFile* f = new TFile(ifname_model.c_str(),"read") ;
96 if( f->IsZombie() ) {
97 cerr << "Could not open file '" << ifname_model << "'." << endl ;
98 exit(1) ;
99 }
101 if( sm == NULL ) {
102 cerr << "Could not read JScatteringModel from file!" << endl ;
103 exit(1) ;
104 }
105 cout << "JScatteringModel loaded" << endl
106 << " absorption length = " << sm->getAbsorptionLength() << " m" << endl
108 << endl ;
109
110
111
113 reader.open(ifname_paths.c_str()) ;
114 if( !reader.is_open() ) {
115 cerr << "FATAL ERROR: unable to open input file '" << ifname_paths << "'." << endl ;
116 exit(1) ;
117 }
118 int nread = 0 ;
120 cout << "Reading file" << endl ;
122 while( reader.hasNext() ) {
123 p = reader.next() ;
124 if( p->size() == (unsigned int)nvert )
125 paths.push_back(*p) ;
126 else
127 cout << p->size() << endl ;
128 ++nread ;
129 }
130 reader.close() ;
131
132 int npaths = paths.size() ;
133 if( npaths == 0 ) {
134 cerr << "FATAL ERROR: No JPhotonPaths with nscat=" << nscat << " in '" << ifname_paths << "'." << endl ;
135 exit(1) ;
136 }
137 cout << "Done reading file. Selected " << npaths << " / " << nread << " JPhotonPaths." << endl ;
138 cout << endl ;
139
140
143 double INF = 1.0/0.0 ;
144 double MININF = -1.0/0.0 ;
145
146 for( int i=0 ; i<nvert ; ++i ) {
149 }
150
151
153
154 for( int i=0; i<nvert; ++i ) {
156 min(minvals[i].getY(),p->at(i).getY()),
157 min(minvals[i].getZ(),p->at(i).getZ()) ) ;
159 max(maxvals[i].getY(),p->at(i).getY()),
160 max(maxvals[i].getZ(),p->at(i).getZ()) ) ;
161 }
162 }
163 cout << endl ;
164
165 cout << "Allocating histograms." << endl ;
166
170 for(
int n=0;
n<nvert; ++
n ) {
171 char hname[200] ;
172 char htitle[200] ;
173
174
175
176 double fac = 1.001 ;
177
178 sprintf(hname,
"hX_nscat_%i_vertex_%i", nscat,
n ) ;
179 sprintf(htitle,
"X of vertex %i (nscat = %i)",
n, nscat ) ;
180 hX[
n] =
new TH1F(hname,htitle,nbins,fac*minvals[
n].getX(),fac*maxvals[
n].getX()) ;
181
182 sprintf(hname,
"hY_nscat_%i_vertex_%i", nscat,
n ) ;
183 sprintf(htitle,
"Y of vertex %i (nscat = %i)",
n, nscat ) ;
184 hY[
n] =
new TH1F(hname,htitle,nbins,fac*minvals[
n].getY(),fac*maxvals[
n].getY()) ;
185
186 sprintf(hname,
"hZ_nscat_%i_vertex_%i", nscat,
n ) ;
187 sprintf(htitle,
"Z of vertex %i (nscat = %i)",
n, nscat ) ;
188 hZ[
n] =
new TH1F(hname,htitle,nbins,fac*minvals[
n].getZ(),fac*maxvals[
n].getZ()) ;
189 }
190 cout << endl ;
191
192
193 cout << "Filling histograms" << endl ;
195
196 for(
int n=0;
n<nvert; ++
n ) {
197 hX[
n]->Fill( p->at(
n).getX() ) ;
198 hY[
n]->Fill( p->at(
n).getY() ) ;
199 hZ[
n]->Fill( p->at(
n).getZ() ) ;
200 }
201 }
202 cout << endl ;
203
204
205 cout << "Normalizing histograms." << endl ;
206 for(
int n=0 ;
n<nvert ; ++
n ) {
207 hX[
n]->Scale( 1.0 / hX[
n]->Integral() ) ;
208 hY[
n]->Scale( 1.0 / hY[
n]->Integral() ) ;
209 hZ[
n]->Scale( 1.0 / hZ[
n]->Integral() ) ;
210 }
211 cout << endl ;
212
213
214
216 writer.
open(
"high_contribution_paths.paths") ;
217
218
219
220 cout << "Computing scattering probability" << endl ;
221 double Pscat = 0 ;
222 double Pscat_err ;
226 TH1F* hRhos ;
227 TH1F* hWs ;
228 TH1F* hIntegralConts ;
229 TH1F* hIntegralConts_zoom ;
230
231
233
235 hY[0]->GetMean(1),
236 hZ[0]->GetMean(1) ) ;
237
239 hY[nvert-1]->GetMean(1),
240 hZ[nvert-1]->GetMean(1) ) ;
241
242
243 for( int i=0 ; i<nsamples; ++i ) {
244 double w = 1 ;
245
246 for(
int n=1 ;
n<nvert-1 ; ++
n ) {
247 double x = hX[
n]->GetRandom() ;
248 double y = hY[
n]->GetRandom() ;
249 double z = hZ[
n]->GetRandom() ;
250 Int_t xbin = hX[
n]->GetXaxis()->FindBin(x) ;
251 Int_t ybin = hY[
n]->GetXaxis()->FindBin(y) ;
252 Int_t zbin = hZ[
n]->GetXaxis()->FindBin(z) ;
253 double wx = hX[
n]->GetBinContent(xbin) / hX[
n]->GetBinWidth(xbin) ;
254 double wy = hY[
n]->GetBinContent(ybin) / hY[
n]->GetBinWidth(ybin) ;
255 double wz = hZ[
n]->GetBinContent(zbin) / hZ[
n]->GetBinWidth(zbin) ;
256 w *= wx * wy * wz ;
258 }
259 double rho = sm->getRho(testpath) ;
260 rhos[i] = rho ;
261 ws[i] = w ;
262 integralConts[i] = rho/w ;
263 Pscat += rho / w ;
264
265 if( rho/w > 0.001 ) {
266
267
268
269
270
271
272
273
274
275
276 writer.
put( testpath ) ;
277 }
278 }
279 Pscat /= nsamples ;
281
282
284 {
285 double max ;
286 char hname[200] ;
287 char htitle[300] ;
288
289
290 max = *(max_element( rhos.begin(), rhos.end() )) ;
291 sprintf( hname, "hRhos_nscat%i", nscat ) ;
292 sprintf( htitle, "Path probability density for nscat = %i", nscat ) ;
293 hRhos = new TH1F(hname,htitle,100,0,1.01*max) ;
294
295
296 max = *(max_element( ws.begin(), ws.end() )) ;
297 sprintf( hname, "hWs_nscat%i", nscat ) ;
298 sprintf( htitle, "Path weights for nscat = %i", nscat ) ;
299 hWs = new TH1F(hname,htitle,100,0,1.01*max) ;
300
301
302 max = *(max_element( integralConts.begin(), integralConts.end() )) ;
303 sprintf( hname, "hIntegralConts_nscat%i", nscat ) ;
304 sprintf( htitle, "Contributions to the integral for nscat = %i", nscat ) ;
305 hIntegralConts = new TH1F(hname,htitle,100,0,1.01*max) ;
306
307
308 max = 10*Pscat ;
309 sprintf( hname, "hIntegralConts_nscat%i_zoom", nscat ) ;
310 sprintf( htitle, "Contributions to the integral for nscat = %i (zoom)", nscat ) ;
311 hIntegralConts_zoom = new TH1F(hname,htitle,100,0,1.01*max) ;
312
313 for( int i=0; i<nsamples; ++i ) {
314 hRhos->Fill( rhos[i] ) ;
315 hWs->Fill( ws[i] ) ;
316 hIntegralConts->Fill( integralConts[i] ) ;
317 hIntegralConts_zoom->Fill( integralConts[i] ) ;
318 }
319
320
321 double var = 0 ;
322 for( int i=0; i<nsamples; ++i ) {
323 var += (integralConts[i]-Pscat) * (integralConts[i]-Pscat) ;
324 }
325 var /= nsamples ;
326
328 cout <<
"sigma = " <<
sigma <<
" (standard deviation of integral contributions)" << endl ;
329 }
330 Pscat_err =
sigma / sqrt(nsamples) ;
331 cout << "Pscat(" << nscat << " scatterings) = " << Pscat << endl ;
332 cout << "Error estimate 1: sigma/sqrt(N) = "
333 <<
sigma <<
" / sqrt(" << nsamples <<
") = "
334 << Pscat_err << endl ;
335
336 cout << "MPI_PSCAT " << Pscat << endl
337 << "MPI_ERROR " << Pscat_err << endl
338 << endl ;
339
340
341 if( ofname != "" ) {
342 TFile* fout = new TFile(ofname.c_str(),"recreate") ;
343 char dname[200] ;
344 sprintf( dname, "nscat%i", nscat ) ;
345 gDirectory->mkdir(dname)->cd() ;
346
347 hRhos->Write() ;
348 hWs->Write() ;
349 hIntegralConts->Write() ;
350 hIntegralConts_zoom->Write() ;
351
352 for(
int n=0;
n<nvert; ++
n ) {
356 }
357 fout->cd() ;
358 fout->Close() ;
359 delete fout ;
360 cout << "Output written to '" << ofname << "'." << endl ;
361 cout << endl ;
362 }
363
364 cout << "Done!" << endl ;
365 return 0 ;
366}
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Data structure for position in three dimensions.
virtual void open(const char *file_name) override
Open file.
virtual void close()
Close file.
virtual bool put(const T &object)=0
Object output.
Virtual base class for a scattering model.
virtual double getScatteringLength()
Utility class to parse command line options.
int read(const int argc, const char *const argv[])
Parse the program's command line options.