ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
foam_demo.C
Go to the documentation of this file.
1 // Demonstrate the TFoam class.
2 //
3 // To run this macro type from CINT command line
4 //
5 // root [0] gSystem->Load("libFoam.so")
6 // root [1] .x foam_demo.C+
7 //Author: Stascek Jadach
8 //____________________________________________________________________________
9 
10 #if defined(__CINT__) && !defined(__MAKECINT__)
11 {
12  std::cout << "Using ACliC to run this macro since it uses custom classes" << std::endl;
13  TString macroFileName = gSystem->UnixPathName(__FILE__);
14  gSystem->CompileMacro(macroFileName, "k");
15  foam_demo();
16 }
17 #else
18 
19 #include "Riostream.h"
20 #include "TFile.h"
21 #include "TFoam.h"
22 #include "TH1.h"
23 #include "TMath.h"
24 #include "TFoamIntegrand.h"
25 #include "TRandom3.h"
26 
27 class TFDISTR: public TFoamIntegrand {
28 public:
29  TFDISTR(){};
30  Double_t Density(int nDim, Double_t *Xarg){
31  // Integrand for mFOAM
32  Double_t Fun1,Fun2,R1,R2;
33  Double_t pos1=1e0/3e0;
34  Double_t pos2=2e0/3e0;
35  Double_t Gam1= 0.100e0; // as in JPC
36  Double_t Gam2= 0.100e0; // as in JPC
37  Double_t sPi = sqrt(TMath::Pi());
38  Double_t xn1=1e0;
39  Double_t xn2=1e0;
40  int i;
41  R1=0;
42  R2=0;
43  for(i = 0 ; i<nDim ; i++){
44  R1=R1+(Xarg[i] -pos1)*(Xarg[i] -pos1);
45  R2=R2+(Xarg[i] -pos2)*(Xarg[i] -pos2);
46  xn1=xn1*Gam1*sPi;
47  xn2=xn2*Gam2*sPi;
48  }
49  R1 = sqrt(R1);
50  R2 = sqrt(R2);
51  Fun1 = exp(-(R1*R1)/(Gam1*Gam1))/xn1; // Gaussian delta-like profile
52  Fun2 = exp(-(R2*R2)/(Gam2*Gam2))/xn2; // Gaussian delta-like profile
53  return 0.5e0*(Fun1+ Fun2);
54 }
55  ClassDef(TFDISTR,1) //Class of testing functions for FOAM
56 };
57 ClassImp(TFDISTR)
58 
59 Int_t foam_demo()
60 {
61  TFile RootFile("foam_demo.root","RECREATE","histograms");
62  long loop;
63  Double_t MCresult,MCerror,MCwt;
64 //=========================================================
65  long NevTot = 50000; // Total MC statistics
66  Int_t kDim = 2; // total dimension
67  Int_t nCells = 500; // Number of Cells
68  Int_t nSampl = 200; // Number of MC events per cell in build-up
69  Int_t nBin = 8; // Number of bins in build-up
70  Int_t OptRej = 1; // Wted events for OptRej=0; wt=1 for OptRej=1 (default)
71  Int_t OptDrive = 2; // (D=2) Option, type of Drive =0,1,2 for TrueVol,Sigma,WtMax
72  Int_t EvPerBin = 25; // Maximum events (equiv.) per bin in buid-up
73  Int_t Chat = 1; // Chat level
74 //=========================================================
75  TRandom *PseRan = new TRandom3(); // Create random number generator
76  TFoam *FoamX = new TFoam("FoamX"); // Create Simulator
77  TFoamIntegrand *rho= new TFDISTR();
78  PseRan->SetSeed(4357);
79 //=========================================================
80  cout<<"***** Demonstration Program for Foam version "<<FoamX->GetVersion()<<" *****"<<endl;
81  FoamX->SetkDim( kDim); // Mandatory!!!
82  FoamX->SetnCells( nCells); // optional
83  FoamX->SetnSampl( nSampl); // optional
84  FoamX->SetnBin( nBin); // optional
85  FoamX->SetOptRej( OptRej); // optional
86  FoamX->SetOptDrive( OptDrive); // optional
87  FoamX->SetEvPerBin( EvPerBin); // optional
88  FoamX->SetChat( Chat); // optional
89 //===============================
90  FoamX->SetRho(rho);
91  FoamX->SetPseRan(PseRan);
92  FoamX->Initialize(); // Initialize simulator
93  FoamX->Write("FoamX"); // Writing Foam on the disk, TESTING PERSISTENCY!!!
94 //===============================
95  long nCalls=FoamX->GetnCalls();
96  cout << "====== Initialization done, entering MC loop" << endl;
97 //======================================================================
98  //cout<<" About to start MC loop: "; cin.getline(question,20);
99  Double_t *MCvect =new Double_t[kDim]; // vector generated in the MC run
100 //======================================================================
101  TH1D *hst_Wt = new TH1D("hst_Wt" , "Main weight of Foam",25,0,1.25);
102  hst_Wt->Sumw2();
103 //======================================================================
104  for(loop=0; loop<NevTot; loop++){
105 //===============================
106  FoamX->MakeEvent(); // generate MC event
107 //===============================
108  FoamX->GetMCvect( MCvect);
109  MCwt=FoamX->GetMCwt();
110  hst_Wt->Fill(MCwt,1.0);
111  if(loop<15){
112  cout<<"MCwt= "<<MCwt<<", ";
113  cout<<"MCvect= ";
114  for ( Int_t k=0 ; k<kDim ; k++) cout<<MCvect[k]<<" "; cout<< endl;
115  }
116  if( ((loop)%100000)==0 ){
117  cout<<" loop= "<<loop<<endl;
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 } // end of Demo
148 
149 #endif
150 
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
Definition: TObject.cxx:823
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual void ls(Option_t *option="") const
List file contents.
Definition: TFile.cxx:1361
Random number generator class based on M.
Definition: TRandom3.h:29
ClassImp(TFDISTR)
Definition: foam_demo.C:57
virtual void GetWtParams(Double_t, Double_t &, Double_t &, Double_t &)
May be called optionally after the MC run.
Definition: TFoam.cxx:1304
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
virtual void MakeEvent()
User subprogram.
Definition: TFoam.cxx:1183
virtual void SetChat(Int_t Chat)
Definition: TFoam.h:131
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
virtual const char * UnixPathName(const char *unixpathname)
Convert from a Unix pathname to a local pathname.
Definition: TSystem.cxx:1020
double sqrt(double)
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual int CompileMacro(const char *filename, Option_t *opt="", const char *library_name="", const char *build_dir="", UInt_t dirmode=0)
This method compiles and loads a shared library containing the code from the file "filename"...
Definition: TSystem.cxx:2736
virtual void SetOptRej(Int_t OptRej)
Definition: TFoam.h:132
virtual void SetnBin(Int_t nBin)
Definition: TFoam.h:130
virtual void Finalize(Double_t &, Double_t &)
May be called optionally by the user after the MC run.
Definition: TFoam.cxx:1317
virtual void GetMCwt(Double_t &)
User may get weight MC weight using this method.
Definition: TFoam.cxx:1248
Double_t Sigma()
Definition: TMath.h:100
virtual const char * GetVersion() const
Definition: TFoam.h:139
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6573
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition: TFoam.cxx:1233
R__EXTERN TSystem * gSystem
Definition: TSystem.h:545
virtual Int_t Write(const char *name=0, Int_t opt=0, Int_t bufsiz=0)
Write memory objects to this file.
Definition: TFile.cxx:2248
virtual Double_t Density(Int_t ndim, Double_t *)=0
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition: TFoam.cxx:370
virtual Long_t GetnCalls() const
Definition: TFoam.h:143
ClassDef(TFoamIntegrand, 1)
virtual void SetnSampl(Long_t nSampl)
Definition: TFoam.h:129
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:613
Double_t Pi()
Definition: TMath.h:44
virtual void SetnCells(Long_t nCells)
Definition: TFoam.h:128
double Double_t
Definition: RtypesCore.h:55
virtual void SetPseRan(TRandom *PseRan)
Definition: TFoam.h:124
virtual void SetOptDrive(Int_t OptDrive)
Definition: TFoam.h:133
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8350
virtual void SetEvPerBin(Int_t EvPerBin)
Definition: TFoam.h:134
double exp(double)
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition: TFoam.cxx:1062
virtual void GetIntegMC(Double_t &, Double_t &)
User subprogram.
Definition: TFoam.cxx:1268
Definition: TFoam.h:29
virtual void Close(Option_t *option="")
Close a file.
Definition: TFile.cxx:898
virtual void SetkDim(Int_t kDim)
Definition: TFoam.h:127