Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
foam_demo.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_FOAM
3 /// \notebook -nodraw
4 /// Demonstrate the TFoam class.
5 ///
6 /// To run this macro type from CINT command line
7 ///
8 /// ~~~{.cpp}
9 /// root [0] gSystem->Load("libFoam.so")
10 /// root [1] .x foam_demo.C+
11 /// ~~~
12 ///
13 /// \macro_code
14 ///
15 /// \author Stascek Jadach
16 
17 
18 #include "Riostream.h"
19 #include "TFile.h"
20 #include "TFoam.h"
21 #include "TH1.h"
22 #include "TMath.h"
23 #include "TFoamIntegrand.h"
24 #include "TRandom3.h"
25 
26 class TFDISTR: public TFoamIntegrand {
27 public:
28  TFDISTR(){};
29  Double_t Density(int nDim, Double_t *Xarg){
30  // Integrand for mFOAM
31  Double_t Fun1,Fun2,R1,R2;
32  Double_t pos1=1e0/3e0;
33  Double_t pos2=2e0/3e0;
34  Double_t Gam1= 0.100e0; // as in JPC
35  Double_t Gam2= 0.100e0; // as in JPC
36  Double_t sPi = sqrt(TMath::Pi());
37  Double_t xn1=1e0;
38  Double_t xn2=1e0;
39  int i;
40  R1=0;
41  R2=0;
42  for(i = 0 ; i<nDim ; i++){
43  R1=R1+(Xarg[i] -pos1)*(Xarg[i] -pos1);
44  R2=R2+(Xarg[i] -pos2)*(Xarg[i] -pos2);
45  xn1=xn1*Gam1*sPi;
46  xn2=xn2*Gam2*sPi;
47  }
48  R1 = sqrt(R1);
49  R2 = sqrt(R2);
50  Fun1 = exp(-(R1*R1)/(Gam1*Gam1))/xn1; // Gaussian delta-like profile
51  Fun2 = exp(-(R2*R2)/(Gam2*Gam2))/xn2; // Gaussian delta-like profile
52  return 0.5e0*(Fun1+ Fun2);
53 }
54  ClassDef(TFDISTR,1) //Class of testing functions for FOAM
55 };
56 ClassImp(TFDISTR)
57 
58 Int_t foam_demo()
59 {
60  TFile RootFile("foam_demo.root","RECREATE","histograms");
61  long loop;
62  Double_t MCresult,MCerror,MCwt;
63  //-----------------------------------------
64  long NevTot = 50000; // Total MC statistics
65  Int_t kDim = 2; // total dimension
66  Int_t nCells = 500; // Number of Cells
67  Int_t nSampl = 200; // Number of MC events per cell in build-up
68  Int_t nBin = 8; // Number of bins in build-up
69  Int_t OptRej = 1; // Wted events for OptRej=0; wt=1 for OptRej=1 (default)
70  Int_t OptDrive = 2; // (D=2) Option, type of Drive =0,1,2 for TrueVol,Sigma,WtMax
71  Int_t EvPerBin = 25; // Maximum events (equiv.) per bin in buid-up
72  Int_t Chat = 1; // Chat level
73  //-----------------------------------------
74  TRandom *PseRan = new TRandom3(); // Create random number generator
75  TFoam *FoamX = new TFoam("FoamX"); // Create Simulator
76  TFoamIntegrand *rho= new TFDISTR();
77  PseRan->SetSeed(4357);
78  //-----------------------------------------
79  cout<<"***** Demonstration Program for Foam version "<<FoamX->GetVersion()<<" *****"<<endl;
80  FoamX->SetkDim( kDim); // Mandatory!!!
81  FoamX->SetnCells( nCells); // optional
82  FoamX->SetnSampl( nSampl); // optional
83  FoamX->SetnBin( nBin); // optional
84  FoamX->SetOptRej( OptRej); // optional
85  FoamX->SetOptDrive( OptDrive); // optional
86  FoamX->SetEvPerBin( EvPerBin); // optional
87  FoamX->SetChat( Chat); // optional
88  //-----------------------------------------
89  FoamX->SetRho(rho);
90  FoamX->SetPseRan(PseRan);
91  FoamX->Initialize(); // Initialize simulator
92  FoamX->Write("FoamX"); // Writing Foam on the disk, TESTING PERSISTENCY!!!
93  //-----------------------------------------
94  long nCalls=FoamX->GetnCalls();
95  cout << "====== Initialization done, entering MC loop" << endl;
96  //-----------------------------------------
97  /*cout<<" About to start MC loop: "; cin.getline(question,20);*/
98  Double_t *MCvect =new Double_t[kDim]; // vector generated in the MC run
99  //-----------------------------------------
100  TH1D *hst_Wt = new TH1D("hst_Wt" , "Main weight of Foam",25,0,1.25);
101  hst_Wt->Sumw2();
102  //-----------------------------------------
103  for(loop=0; loop<NevTot; loop++){
104  /*===============================*/
105  FoamX->MakeEvent(); // generate MC event
106  /*===============================*/
107  FoamX->GetMCvect( MCvect);
108  MCwt=FoamX->GetMCwt();
109  hst_Wt->Fill(MCwt,1.0);
110  if(loop<15){
111  cout<<"MCwt= "<<MCwt<<", ";
112  cout<<"MCvect= ";
113  for ( Int_t k=0 ; k<kDim ; k++) cout<<MCvect[k]<<" "; cout<< endl;
114  }
115  if( ((loop)%100000)==0 ){
116  cout<<" loop= "<<loop<<endl;
117  }
118  }
119 
120  //-----------------------------------------
121 
122  cout << "====== Events generated, entering Finalize" << endl;
123 
124  hst_Wt->Print("all");
125  Double_t eps = 0.0005;
126  Double_t Effic, WtMax, AveWt, Sigma;
127  Double_t IntNorm, Errel;
128  FoamX->Finalize( IntNorm, Errel); // final printout
129  FoamX->GetIntegMC( MCresult, MCerror); // get MC intnegral
130  FoamX->GetWtParams(eps, AveWt, WtMax, Sigma); // get MC wt parameters
131  Effic=0; if(WtMax>0) Effic=AveWt/WtMax;
132  cout << "================================================================" << endl;
133  cout << " MCresult= " << MCresult << " +- " << MCerror << " RelErr= "<< MCerror/MCresult << endl;
134  cout << " Dispersion/<wt>= " << Sigma/AveWt << endl;
135  cout << " <wt>/WtMax= " << Effic <<", for epsilon = "<<eps << endl;
136  cout << " nCalls (initialization only) = " << nCalls << endl;
137  cout << "================================================================" << endl;
138 
139  delete [] MCvect;
140  //
141  RootFile.ls();
142  RootFile.Write();
143  RootFile.Close();
144  cout << "***** End of Demonstration Program *****" << endl;
145 
146  return 0;
147 }
148 
149