void MCintegral() { // In a Compton scattering experiment, a cylindrical aluminium target is put in front // of a punctual source of gamma rays, and the detector is put on the other side. // In order to normalize the results, one needs the effective volume, defined by // the intersection between the cylinder and the cone defined by the source and the detector. // // Compute that volume by MC means, in the following two configurations: // a) the cylinder is 10cm high, r=2cm. It is standing vertically at equal distance of the // source and of the detector, such that the gamma rays enters on the curved side. // The detector window is a circle of r=2cm put at 4m away from the source. // b) The distance between source and detector is reduced to 10cm and the cylinder // is replaced by a smaller one, of radius equal to 1cm. Float_t x,y,z; int count = 0; int N=100000; TH3F* h = new TH3F("volume","volume",40,-2,2,40,-2,2,40,-2,2); for(int i=0;iUniform(-2,2); y = gRandom->Uniform(-2,2); z = gRandom->Uniform(-2,2); // count the fraction in the target volume //if((x*x+y*y)<4 && sqrt(x*x+z*z)<(1-y/200.) ) h->Fill(x,y,z); if((x*x+y*y)<1 && sqrt(x*x+z*z)<(1-y/10.) ) h->Fill(x,y,z); } // extract the result h->Draw(); std::cout << "volume is " << h->GetEntries()/N*64. << " cm^3. " << std::endl; // in case a, the result (12.2464cm^3) differs by ~2% from the approximation of a cylinder (4pi) // case b, would be more difficult to approximate. // an alternative (more efficient) approach would be to generate the // numbers directly in the cylinder volume. But beware on the way to // generate uniformly the numbers. Uniform in r,phi,z is wrong as it // will be more dense in the center. }