// @(#)root/foam:$Id$
// Author: S. Jadach <mailto:Stanislaw.jadach@ifj.edu.pl>, P.Sawicki <mailto:Pawel.Sawicki@ifj.edu.pl>

#ifndef ROOT_TFoam
#define ROOT_TFoam

//////////////////////////////////////////////////////////////////////////////
//                                                                          //
// TFoam is the main class of the multi-dimensional general purpose         //
// Monte Carlo event generator (integrator) FOAM.                           //
//                                                                          //
//////////////////////////////////////////////////////////////////////////////

#ifndef ROOT_TObject
#include "TObject.h"
#endif

#include "TString.h"

class TH1D;
class TRefArray;
class TMethodCall;
class TRandom;
class TFoamIntegrand;
class TFoamMaxwt;
class TFoamVect;
class TFoamCell;

class TFoam : public TObject {
protected:
  // COMPONENTS //
    //-------------- Input parameters
   TString fName;             // Name of a given instance of the FOAM class
   TString fVersion;          // Actual version of the FOAM like (1.01m)
   TString fDate;             // Release date of FOAM
   Int_t   fDim;              // Dimension of the integration/simulation space
   Int_t   fNCells;           // Maximum number of cells
   Int_t   fRNmax;            // Maximum No. of the rand. numb. requested at once
   //-------------------
   Int_t   fOptDrive;         // Optimization switch =1,2 for variance or maximum weight optimization
   Int_t   fChat;             // Chat=0,1,2 chat level in output, Chat=1 normal level
   Int_t   fOptRej;           // Switch =0 for weighted events; =1 for unweighted events in MC
   //-------------------
   Int_t   fNBin;             // No. of bins in the edge histogram for cell MC exploration
   Int_t   fNSampl;           // No. of MC events, when dividing (exploring) cell
   Int_t   fEvPerBin;         // Maximum number of effective (wt=1) events per bin
   //-------------------  MULTI-BRANCHING ---------------------
   Int_t  *fMaskDiv;          //! [fDim] Dynamic Mask for  cell division
   Int_t  *fInhiDiv;          //! [fDim] Flags for inhibiting cell division
   Int_t   fOptPRD;           //  Option switch for predefined division, for quick check
   TFoamVect **fXdivPRD;      //! Lists of division values encoded in one vector per direction
   //-------------------  GEOMETRY ----------------------------
   Int_t   fNoAct;            // Number of active cells
   Int_t   fLastCe;           // Index of the last cell
   TFoamCell **fCells;           // [fNCells] Array of ALL cells
   //------------------ M.C. generation----------------------------
   TFoamMaxwt   *fMCMonit;    // Monitor of the MC weight for measuring MC efficiency
   Double_t   fMaxWtRej;      // Maximum weight in rejection for getting wt=1 events
   TRefArray *fCellsAct;      // Array of pointers to active cells, constructed at the end of foam build-up
   Double_t  *fPrimAcu;       // [fNoAct] Array of cumulative probability of all active cells
   TObjArray *fHistEdg;       // Histograms of wt, one for each cell edge
   TObjArray *fHistDbg;       // Histograms of wt, for debug
   TH1D      *fHistWt;        // Histogram of the MC wt

   Double_t *fMCvect;         // [fDim] Generated MC vector for the outside user
   Double_t  fMCwt;           // MC weight
   Double_t *fRvec;           // [fRNmax] random number vector from r.n. generator fDim+1 maximum elements
   //----------- Procedures
   TFoamIntegrand *fRho;      //! Pointer to the user-defined integrand function/distribution
   TMethodCall *fMethodCall;  //! ROOT's pointer to user-defined global distribution function
   TRandom         *fPseRan;  // Pointer to user-defined generator of pseudorandom numbers
   //----------- Statistics and MC results
   Long_t   fNCalls;          // Total number of the function calls
   Long_t   fNEffev;          // Total number of effective events (wt=1) in the foam buildup
   Double_t fSumWt, fSumWt2;  // Total sum of wt and wt^2
   Double_t fSumOve;          // Total Sum of overveighted events
   Double_t fNevGen;          // Total number of the generated MC events
   Double_t fWtMax, fWtMin;   // Maximum/Minimum MC weight
   Double_t fPrime;           // Primary integral R' (R=R'<wt>)
   Double_t fMCresult;        // True Integral R from MC series
   Double_t fMCerror;         // and its error
   //----------  working space for CELL exploration -------------
   Double_t *fAlpha;          // [fDim] Internal parameters of the hyperrectangle
   //////////////////////////////////////////////////////////////////////////////////////////////
   //                                     METHODS                                              //
   //////////////////////////////////////////////////////////////////////////////////////////////
public:
   TFoam();                          // Default constructor (used only by ROOT streamer)
   TFoam(const Char_t*);             // Principal user-defined constructor
   virtual ~TFoam();                 // Default destructor
   TFoam(const TFoam&);              // Copy Constructor  NOT USED
   // Initialization
   virtual void Initialize();                // Initialization of the FOAM (grid, cells, etc), mandatory!
   virtual void Initialize(TRandom *, TFoamIntegrand *); // Alternative initialization method, backward compatibility
   virtual void InitCells();                 // Initializes first cells inside original cube
   virtual Int_t  CellFill(Int_t, TFoamCell*);  // Allocates new empty cell and return its index
   virtual void Explore(TFoamCell *Cell);       // Exploration of the new cell, determine <wt>, wtMax etc.
   virtual void Carver(Int_t&,Double_t&,Double_t&);// Determines the best edge, wt_max reduction
   virtual void Varedu(Double_t [], Int_t&, Double_t&,Double_t&); // Determines the best edge, variace reduction
   virtual void MakeAlpha();                 // Provides random point inside hyperrectangle
   virtual void Grow();                      // Adds new cells to FOAM object until buffer is full
   virtual Long_t PeekMax();                 // Choose one active cell, used by Grow and also in MC generation
   virtual Int_t  Divide(TFoamCell *);       // Divide iCell into two daughters; iCell retained, taged as inactive
   virtual void MakeActiveList();            // Creates table of active cells
   virtual void GenerCel2(TFoamCell *&);     // Chose an active cell the with probability ~ Primary integral
   // Generation
   virtual Double_t Eval(Double_t *);        // Evaluates value of the distribution function
   virtual void     MakeEvent();             // Makes (generates) single MC event
   virtual void     GetMCvect(Double_t *);   // Provides generated randomly MC vector
   virtual void     GetMCwt(Double_t &);     // Provides generated MC weight
   virtual Double_t GetMCwt();               // Provides generates MC weight
   virtual Double_t MCgenerate(Double_t *MCvect);// All three above function in one
   // Finalization
   virtual void GetIntegMC(Double_t&, Double_t&);// Provides Integrand and abs. error from MC run
   virtual void GetIntNorm(Double_t&, Double_t&);// Provides normalization Inegrand
   virtual void GetWtParams(Double_t, Double_t&, Double_t&, Double_t&);// Provides MC weight parameters
   virtual void Finalize(  Double_t&, Double_t&);  // Prints summary of MC integration
   virtual TFoamIntegrand  *GetRho(){return fRho;} // Gets pointer of the distribut. (after restoring from disk)
   virtual TRandom *GetPseRan() const {return fPseRan;}   // Gets pointer of r.n. generator (after restoring from disk)
   virtual void SetRhoInt(void *Rho);              // Set new integrand distr. in interactive mode
   virtual void SetRhoInt(Double_t (*fun)(Int_t, Double_t *));    // Set new integrand distr. in compiled mode
   virtual void SetRho(TFoamIntegrand *Rho);       // Set new integrand distr. in compiled mode
   virtual void ResetRho(TFoamIntegrand *Rho);                // Set new distribution, delete old
   virtual void SetPseRan(TRandom *PseRan){fPseRan=PseRan;}   // Set new r.n. generator
   virtual void ResetPseRan(TRandom *PseRan);                 // Set new r.n.g, delete old
   // Getters and Setters
   virtual void SetkDim(Int_t kDim){fDim = kDim;}            // Sets dimension of cubical space
   virtual void SetnCells(Long_t nCells){fNCells =nCells;}  // Sets maximum number of cells
   virtual void SetnSampl(Long_t nSampl){fNSampl =nSampl;}  // Sets no of MC events in cell exploration
   virtual void SetnBin(Int_t nBin){fNBin = nBin;}          // Sets no of bins in histogs in cell exploration
   virtual void SetChat(Int_t Chat){fChat = Chat;}          // Sets option Chat, chat level
   virtual void SetOptRej(Int_t OptRej){fOptRej =OptRej;}   // Sets option for MC rejection
   virtual void SetOptDrive(Int_t OptDrive){fOptDrive =OptDrive;}  // Sets optimization switch
   virtual void SetEvPerBin(Int_t EvPerBin){fEvPerBin =EvPerBin;}  // Sets max. no. of effective events per bin
   virtual void SetMaxWtRej(Double_t MaxWtRej){fMaxWtRej=MaxWtRej;}  // Sets max. weight for rejection
   virtual void SetInhiDiv(Int_t, Int_t );            // Set inhibition of cell division along certain edge
   virtual void SetXdivPRD(Int_t, Int_t, Double_t[]); // Set predefined division points
   // Getters and Setters
   virtual const char *GetVersion() const {return fVersion.Data();}// Get version of the FOAM
   virtual Int_t    GetTotDim() const { return fDim;}              // Get total dimension
   virtual Double_t GetPrimary() const {return fPrime;}            // Get value of primary integral R'
   virtual void GetPrimary(Double_t &prime) {prime = fPrime;}      // Get value of primary integral R'
   virtual Long_t GetnCalls() const {return fNCalls;}            // Get total no. of the function calls
   virtual Long_t GetnEffev() const {return fNEffev;}            // Get total no. of effective wt=1 events
   // Debug
   virtual void CheckAll(Int_t);     // Checks correctness of the entire data structure in the FOAM object
   virtual void PrintCells();        // Prints content of all cells
   virtual void RootPlot2dim(Char_t*);   // Generates C++ code for drawing foam
   virtual void LinkCells(void);         // Void function for backward compatibility
   // Inline
private:
   Double_t Sqr(Double_t x) const { return x*x;}      // Square function
   //////////////////////////////////////////////////////////////////////////////////////////////
   ClassDef(TFoam,1);   // General purpose self-adapting Monte Carlo event generator
};

#endif
 TFoam.h:1
 TFoam.h:2
 TFoam.h:3
 TFoam.h:4
 TFoam.h:5
 TFoam.h:6
 TFoam.h:7
 TFoam.h:8
 TFoam.h:9
 TFoam.h:10
 TFoam.h:11
 TFoam.h:12
 TFoam.h:13
 TFoam.h:14
 TFoam.h:15
 TFoam.h:16
 TFoam.h:17
 TFoam.h:18
 TFoam.h:19
 TFoam.h:20
 TFoam.h:21
 TFoam.h:22
 TFoam.h:23
 TFoam.h:24
 TFoam.h:25
 TFoam.h:26
 TFoam.h:27
 TFoam.h:28
 TFoam.h:29
 TFoam.h:30
 TFoam.h:31
 TFoam.h:32
 TFoam.h:33
 TFoam.h:34
 TFoam.h:35
 TFoam.h:36
 TFoam.h:37
 TFoam.h:38
 TFoam.h:39
 TFoam.h:40
 TFoam.h:41
 TFoam.h:42
 TFoam.h:43
 TFoam.h:44
 TFoam.h:45
 TFoam.h:46
 TFoam.h:47
 TFoam.h:48
 TFoam.h:49
 TFoam.h:50
 TFoam.h:51
 TFoam.h:52
 TFoam.h:53
 TFoam.h:54
 TFoam.h:55
 TFoam.h:56
 TFoam.h:57
 TFoam.h:58
 TFoam.h:59
 TFoam.h:60
 TFoam.h:61
 TFoam.h:62
 TFoam.h:63
 TFoam.h:64
 TFoam.h:65
 TFoam.h:66
 TFoam.h:67
 TFoam.h:68
 TFoam.h:69
 TFoam.h:70
 TFoam.h:71
 TFoam.h:72
 TFoam.h:73
 TFoam.h:74
 TFoam.h:75
 TFoam.h:76
 TFoam.h:77
 TFoam.h:78
 TFoam.h:79
 TFoam.h:80
 TFoam.h:81
 TFoam.h:82
 TFoam.h:83
 TFoam.h:84
 TFoam.h:85
 TFoam.h:86
 TFoam.h:87
 TFoam.h:88
 TFoam.h:89
 TFoam.h:90
 TFoam.h:91
 TFoam.h:92
 TFoam.h:93
 TFoam.h:94
 TFoam.h:95
 TFoam.h:96
 TFoam.h:97
 TFoam.h:98
 TFoam.h:99
 TFoam.h:100
 TFoam.h:101
 TFoam.h:102
 TFoam.h:103
 TFoam.h:104
 TFoam.h:105
 TFoam.h:106
 TFoam.h:107
 TFoam.h:108
 TFoam.h:109
 TFoam.h:110
 TFoam.h:111
 TFoam.h:112
 TFoam.h:113
 TFoam.h:114
 TFoam.h:115
 TFoam.h:116
 TFoam.h:117
 TFoam.h:118
 TFoam.h:119
 TFoam.h:120
 TFoam.h:121
 TFoam.h:122
 TFoam.h:123
 TFoam.h:124
 TFoam.h:125
 TFoam.h:126
 TFoam.h:127
 TFoam.h:128
 TFoam.h:129
 TFoam.h:130
 TFoam.h:131
 TFoam.h:132
 TFoam.h:133
 TFoam.h:134
 TFoam.h:135
 TFoam.h:136
 TFoam.h:137
 TFoam.h:138
 TFoam.h:139
 TFoam.h:140
 TFoam.h:141
 TFoam.h:142
 TFoam.h:143
 TFoam.h:144
 TFoam.h:145
 TFoam.h:146
 TFoam.h:147
 TFoam.h:148
 TFoam.h:149
 TFoam.h:150
 TFoam.h:151
 TFoam.h:152
 TFoam.h:153
 TFoam.h:154
 TFoam.h:155
 TFoam.h:156
 TFoam.h:157