Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
foam_demo.C File Reference

Detailed Description

View in nbviewer Open in SWAN Demonstrate the TFoam class.

To run this macro type from CINT command line

root [0] gSystem->Load("libFoam.so")
root [1] .x foam_demo.C+
R__EXTERN TSystem * gSystem
Definition TSystem.h:559
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition TSystem.cxx:1855
#include "Riostream.h"
#include "TFile.h"
#include "TFoam.h"
#include "TH1.h"
#include "TMath.h"
#include "TFoamIntegrand.h"
#include "TRandom3.h"
class TFDISTR: public TFoamIntegrand {
public:
TFDISTR(){};
Double_t Density(int nDim, Double_t *Xarg){
// Integrand for mFOAM
Double_t Fun1,Fun2,R1,R2;
Double_t pos1=1e0/3e0;
Double_t pos2=2e0/3e0;
Double_t Gam1= 0.100e0; // as in JPC
Double_t Gam2= 0.100e0; // as in JPC
Double_t sPi = sqrt(TMath::Pi());
Double_t xn1=1e0;
Double_t xn2=1e0;
int i;
R1=0;
R2=0;
for(i = 0 ; i<nDim ; i++){
R1=R1+(Xarg[i] -pos1)*(Xarg[i] -pos1);
R2=R2+(Xarg[i] -pos2)*(Xarg[i] -pos2);
xn1=xn1*Gam1*sPi;
xn2=xn2*Gam2*sPi;
}
R1 = sqrt(R1);
R2 = sqrt(R2);
Fun1 = exp(-(R1*R1)/(Gam1*Gam1))/xn1; // Gaussian delta-like profile
Fun2 = exp(-(R2*R2)/(Gam2*Gam2))/xn2; // Gaussian delta-like profile
return 0.5e0*(Fun1+ Fun2);
}
ClassDef(TFDISTR,1) //Class of testing functions for FOAM
};
ClassImp(TFDISTR)
Int_t foam_demo()
{
TFile RootFile("foam_demo.root","RECREATE","histograms");
long loop;
Double_t MCresult,MCerror,MCwt;
//-----------------------------------------
long NevTot = 50000; // Total MC statistics
Int_t kDim = 2; // total dimension
Int_t nCells = 500; // Number of Cells
Int_t nSampl = 200; // Number of MC events per cell in build-up
Int_t nBin = 8; // Number of bins in build-up
Int_t OptRej = 1; // Wted events for OptRej=0; wt=1 for OptRej=1 (default)
Int_t OptDrive = 2; // (D=2) Option, type of Drive =0,1,2 for TrueVol,Sigma,WtMax
Int_t EvPerBin = 25; // Maximum events (equiv.) per bin in buid-up
Int_t Chat = 1; // Chat level
//-----------------------------------------
TRandom *PseRan = new TRandom3(); // Create random number generator
TFoam *FoamX = new TFoam("FoamX"); // Create Simulator
TFoamIntegrand *rho= new TFDISTR();
PseRan->SetSeed(4357);
//-----------------------------------------
cout<<"***** Demonstration Program for Foam version "<<FoamX->GetVersion()<<" *****"<<endl;
FoamX->SetkDim( kDim); // Mandatory!!!
FoamX->SetnCells( nCells); // optional
FoamX->SetnSampl( nSampl); // optional
FoamX->SetnBin( nBin); // optional
FoamX->SetOptRej( OptRej); // optional
FoamX->SetOptDrive( OptDrive); // optional
FoamX->SetEvPerBin( EvPerBin); // optional
FoamX->SetChat( Chat); // optional
//-----------------------------------------
FoamX->SetRho(rho);
FoamX->SetPseRan(PseRan);
FoamX->Initialize(); // Initialize simulator
FoamX->Write("FoamX"); // Writing Foam on the disk, TESTING PERSISTENCY!!!
//-----------------------------------------
long nCalls=FoamX->GetnCalls();
cout << "====== Initialization done, entering MC loop" << endl;
//-----------------------------------------
/*cout<<" About to start MC loop: "; cin.getline(question,20);*/
Double_t *MCvect =new Double_t[kDim]; // vector generated in the MC run
//-----------------------------------------
TH1D *hst_Wt = new TH1D("hst_Wt" , "Main weight of Foam",25,0,1.25);
hst_Wt->Sumw2();
//-----------------------------------------
for(loop=0; loop<NevTot; loop++){
/*===============================*/
FoamX->MakeEvent(); // generate MC event
/*===============================*/
FoamX->GetMCvect( MCvect);
MCwt=FoamX->GetMCwt();
hst_Wt->Fill(MCwt,1.0);
if(loop<15){
cout<<"MCwt= "<<MCwt<<", ";
cout<<"MCvect= ";
for ( Int_t k=0 ; k<kDim ; k++) cout<<MCvect[k]<<" "; cout<< endl;
}
if( ((loop)%100000)==0 ){
cout<<" loop= "<<loop<<endl;
}
}
//-----------------------------------------
cout << "====== Events generated, entering Finalize" << endl;
hst_Wt->Print("all");
Double_t eps = 0.0005;
Double_t Effic, WtMax, AveWt, Sigma;
Double_t IntNorm, Errel;
FoamX->Finalize( IntNorm, Errel); // final printout
FoamX->GetIntegMC( MCresult, MCerror); // get MC intnegral
FoamX->GetWtParams(eps, AveWt, WtMax, Sigma); // get MC wt parameters
Effic=0; if(WtMax>0) Effic=AveWt/WtMax;
cout << "================================================================" << endl;
cout << " MCresult= " << MCresult << " +- " << MCerror << " RelErr= "<< MCerror/MCresult << endl;
cout << " Dispersion/<wt>= " << Sigma/AveWt << endl;
cout << " <wt>/WtMax= " << Effic <<", for epsilon = "<<eps << endl;
cout << " nCalls (initialization only) = " << nCalls << endl;
cout << "================================================================" << endl;
delete [] MCvect;
//
RootFile.ls();
RootFile.Write();
RootFile.Close();
cout << "***** End of Demonstration Program *****" << endl;
return 0;
}
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
#define ClassDef(name, id)
Definition Rtypes.h:325
#define ClassImp(name)
Definition Rtypes.h:364
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:54
Abstract class representing n-dimensional real positive integrand function.
virtual Double_t Density(Int_t ndim, Double_t *)=0
TFoam is the main class of the multi-dimensional general purpose Monte Carlo event generator (integra...
Definition TFoam.h:21
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition TFoam.cxx:1176
virtual void MakeEvent()
User method.
Definition TFoam.cxx:1126
virtual void SetEvPerBin(Int_t EvPerBin)
Definition TFoam.h:122
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition TFoam.cxx:323
virtual void SetnSampl(Long_t nSampl)
Definition TFoam.h:117
virtual void GetIntegMC(Double_t &, Double_t &)
User method.
Definition TFoam.cxx:1211
virtual const char * GetVersion() const
Definition TFoam.h:127
virtual void GetMCwt(Double_t &)
User may get weight MC weight using this method.
Definition TFoam.cxx:1191
virtual void SetChat(Int_t Chat)
Definition TFoam.h:119
virtual void SetOptDrive(Int_t OptDrive)
Definition TFoam.h:121
virtual void SetnCells(Long_t nCells)
Definition TFoam.h:116
virtual Long_t GetnCalls() const
Definition TFoam.h:131
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition TFoam.cxx:1022
virtual void Finalize(Double_t &, Double_t &)
May be called optionally by the user after the MC run.
Definition TFoam.cxx:1260
virtual void SetOptRej(Int_t OptRej)
Definition TFoam.h:120
virtual void SetPseRan(TRandom *PseRan)
Definition TFoam.h:112
virtual void SetnBin(Int_t nBin)
Definition TFoam.h:118
virtual void SetkDim(Int_t kDim)
Definition TFoam.h:115
virtual void GetWtParams(Double_t, Double_t &, Double_t &, Double_t &)
May be called optionally after the MC run.
Definition TFoam.cxx:1247
1-D histogram with a double per channel (see TH1 documentation)}
Definition TH1.h:618
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition TH1.cxx:6964
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition TH1.cxx:3351
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition TH1.cxx:8850
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:868
Random number generator class based on M.
Definition TRandom3.h:27
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition TRandom.cxx:608
VecExpr< UnaryOp< Sqrt< T >, VecExpr< A, T, D >, T >, T, D > sqrt(const VecExpr< A, T, D > &rhs)
constexpr Double_t Sigma()
Stefan-Boltzmann constant in .
Definition TMath.h:277
constexpr Double_t Pi()
Definition TMath.h:37
#define R1(v, w, x, y, z, i)
Definition sha1.inl:134
#define R2(v, w, x, y, z, i)
Definition sha1.inl:137
Author
Stascek Jadach

Definition in file foam_demo.C.