Logo ROOT  
Reference Guide
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+
#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 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;
}
Author
Stascek Jadach

Definition in file foam_demo.C.

R2
#define R2(v, w, x, y, z, i)
Definition: sha1.inl:137
TFoam::GetIntegMC
virtual void GetIntegMC(Double_t &, Double_t &)
User method.
Definition: TFoam.cxx:1211
TFoamIntegrand::Density
virtual Double_t Density(Int_t ndim, Double_t *)=0
TFoam::GetVersion
virtual const char * GetVersion() const
Definition: TFoam.h:127
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TFoam::MakeEvent
virtual void MakeEvent()
User method.
Definition: TFoam.cxx:1126
TH1D
1-D histogram with a double per channel (see TH1 documentation)}
Definition: TH1.h:615
exp
double exp(double)
Int_t
int Int_t
Definition: RtypesCore.h:45
TFoam::SetnBin
virtual void SetnBin(Int_t nBin)
Definition: TFoam.h:118
TFoam::GetWtParams
virtual void GetWtParams(Double_t, Double_t &, Double_t &, Double_t &)
May be called optionally after the MC run.
Definition: TFoam.cxx:1247
TSystem::Load
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
Definition: TSystem.cxx:1852
TH1::Print
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6641
TFile.h
TFoam.h
TFoam::SetChat
virtual void SetChat(Int_t Chat)
Definition: TFoam.h:119
TFoamIntegrand.h
TFoam::Finalize
virtual void Finalize(Double_t &, Double_t &)
May be called optionally by the user after the MC run.
Definition: TFoam.cxx:1260
TMath::Sigma
constexpr Double_t Sigma()
Stefan-Boltzmann constant in .
Definition: TMath.h:283
TFoam::SetOptRej
virtual void SetOptRej(Int_t OptRej)
Definition: TFoam.h:120
TMath::Pi
constexpr Double_t Pi()
Definition: TMath.h:43
TFoam
Definition: TFoam.h:21
TRandom
Definition: TRandom.h:27
TRandom3
Definition: TRandom3.h:27
TFoamIntegrand
Definition: TFoamIntegrand.h:9
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
TFoam::SetnSampl
virtual void SetnSampl(Long_t nSampl)
Definition: TFoam.h:117
TRandom::SetSeed
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:597
TFoam::GetMCvect
virtual void GetMCvect(Double_t *)
User may get generated MC point/vector with help of this method.
Definition: TFoam.cxx:1176
TRandom3.h
sqrt
double sqrt(double)
TFoam::GetMCwt
virtual void GetMCwt(Double_t &)
User may get weight MC weight using this method.
Definition: TFoam.cxx:1191
TFoam::Initialize
virtual void Initialize()
Basic initialization of FOAM invoked by the user.
Definition: TFoam.cxx:323
TFile
Definition: TFile.h:54
TFoam::SetOptDrive
virtual void SetOptDrive(Int_t OptDrive)
Definition: TFoam.h:121
gSystem
R__EXTERN TSystem * gSystem
Definition: TSystem.h:559
Double_t
double Double_t
Definition: RtypesCore.h:59
TObject::Write
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:795
TH1::Sumw2
virtual void Sumw2(Bool_t flag=kTRUE)
Create structure to store sum of squares of weights.
Definition: TH1.cxx:8475
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
TFoam::SetkDim
virtual void SetkDim(Int_t kDim)
Definition: TFoam.h:115
TFoam::SetnCells
virtual void SetnCells(Long_t nCells)
Definition: TFoam.h:116
TFoam::SetRho
virtual void SetRho(TFoamIntegrand *Rho)
User may use this method to set the distribution object.
Definition: TFoam.cxx:1022
TFoam::SetPseRan
virtual void SetPseRan(TRandom *PseRan)
Definition: TFoam.h:112
R1
#define R1(v, w, x, y, z, i)
Definition: sha1.inl:134
Riostream.h
TH1.h
TFoam::GetnCalls
virtual Long_t GetnCalls() const
Definition: TFoam.h:131
TFoam::SetEvPerBin
virtual void SetEvPerBin(Int_t EvPerBin)
Definition: TFoam.h:122
TMath.h