示例#1
0
Int_t foam_demopers()
{

  gSystem->Load("libFoam");

  // need to load the foam_demo tutorial for the definition of the function
  TString macroName = gSystem->UnixPathName(__FILE__);
  macroName.ReplaceAll("foam_demopers.C","foam_demo.C");
  gROOT->ProcessLine(TString::Format(".L %s+",macroName.Data()));

  //******************************************
  cout<<"====================== TestVector ================================"<<endl;
  TFile fileA("foam_demo.root");
  fileA.cd();
  cout<<"------------------------------------------------------------------"<<endl;
  fileA.ls();
  cout<<"------------------------------------------------------------------"<<endl;
  fileA.Map();
  cout<<"------------------------------------------------------------------"<<endl;
  fileA.ShowStreamerInfo();
  cout<<"------------------------------------------------------------------"<<endl;
  fileA.GetListOfKeys()->Print();
  cout<<"------------------------------------------------------------------"<<endl;
  //*******************************************
  TFoam  *FoamX = (TFoam*)fileA.Get("FoamX");
  //*******************************************
//  FoamX->PrintCells();
  FoamX->CheckAll(1);

 //N.B. the integrand functions need to be reset
 // because cannot be made persistent
#ifdef __CINT__
 // this can be done only in CINT
 TFoamIntegrand    *rho= new TFDISTR();
#else
   // this should be done with AClic or Cling
   TFoamIntegrand * rho = (TFoamIntegrand*) gROOT->ProcessLine("return new TFDISTR();");
#endif
 FoamX->SetRho(rho);

 Double_t *MCvect =new Double_t[2]; // 2-dim vector generated in the MC run

 for(long loop=0; loop<50000; loop++){
    FoamX->MakeEvent();            // generate MC event
    FoamX->GetMCvect( MCvect);     // get generated vector (x,y)
    Double_t x=MCvect[0];
    Double_t y=MCvect[1];
    if(loop<10) cout<<"(x,y) =  ( "<< x <<", "<< y <<" )"<<endl;
    }// loop
  //
  Double_t IntNorm, Errel;
  FoamX->Finalize(   IntNorm, Errel);     // final printout
  Double_t MCresult, MCerror;
  FoamX->GetIntegMC( MCresult, MCerror);  // get MC integral, should be one
  cout << " MCresult= " << MCresult << " +- " << MCerror <<endl;
  cout<<"===================== TestPers FINISHED ======================="<<endl;
  return 0;
}
示例#2
0
Int_t foam_kanwa(){
  cout<<"--- kanwa started ---"<<endl;
  TH2D  *hst_xy = new TH2D("hst_xy" ,  "x-y plot", 50,0,1.0, 50,0,1.0);
  Double_t *MCvect =new Double_t[2]; // 2-dim vector generated in the MC run
  //
  TRandom     *PseRan   = new TRandom3();  // Create random number generator
  PseRan->SetSeed(4357);
  TFoam   *FoamX    = new TFoam("FoamX");   // Create Simulator
  FoamX->SetkDim(2);         // No. of dimensions, obligatory!
  FoamX->SetnCells(500);     // Optionally No. of cells, default=2000
  FoamX->SetRhoInt(Camel2);  // Set 2-dim distribution, included below
  FoamX->SetPseRan(PseRan);  // Set random number generator
  FoamX->Initialize();       // Initialize simulator, may take time...
  //
  // visualising generated distribution
  TCanvas *cKanwa = new TCanvas("cKanwa","Canvas for plotting",600,600);
  cKanwa->cd();
  // From now on FoamX is ready to generate events
  int nshow=5000;
  for(long loop=0; loop<100000; loop++){
    FoamX->MakeEvent();            // generate MC event
    FoamX->GetMCvect( MCvect);     // get generated vector (x,y)
    Double_t x=MCvect[0];
    Double_t y=MCvect[1];
    if(loop<10) cout<<"(x,y) =  ( "<< x <<", "<< y <<" )"<<endl;
    hst_xy->Fill(x,y);
    // live plot
    if(loop == nshow){
      nshow += 5000;
      hst_xy->Draw("lego2");
      cKanwa->Update();
    }
  }// loop
  //
  hst_xy->Draw("lego2");  // final plot
  cKanwa->Update();
  //
  Double_t MCresult, MCerror;
  FoamX->GetIntegMC( MCresult, MCerror);  // get MC integral, should be one
  cout << " MCresult= " << MCresult << " +- " << MCerror <<endl;
  cout<<"--- kanwa ended ---"<<endl;

  return 0;
}//kanwa
示例#3
0
void Foam::Init()
{
	//Retrieve the maxima and minima, to use in the coordinate transforms
	for (unsigned int continuousIndex = 0; continuousIndex < continuousNames.size(); ++continuousIndex )
	{
		double maximum = generationBoundary->GetConstraint( *continuousNames_ref[continuousIndex] )->GetMaximum();
		double minimum = generationBoundary->GetConstraint( *continuousNames_ref[continuousIndex] )->GetMinimum();
		minima.push_back(minimum);
		ranges.push_back( maximum - minimum );
	}

	double rand = rootRandom->Rndm();

	//Make a Foam generator for each combination
	for( unsigned int combinationIndex = 0; combinationIndex < discreteCombinations.size(); ++combinationIndex )
	{
		//Make the data point for the combination
		DataPoint * init_temporaryDataPoint = new DataPoint(allNames);
		for( unsigned int discreteIndex = 0; discreteIndex < discreteNames.size(); ++discreteIndex )
		{
			IConstraint* thisConst=NULL;
			try
			{
				thisConst=generationBoundary->GetConstraint( *discreteNames_ref[discreteIndex] );
			}
			catch(...)
			{
				cout << endl;
				exit(52);
			}
			string unit = thisConst->GetUnit();
			init_temporaryDataPoint->SetObservable( discreteNames[discreteIndex], discreteCombinations[combinationIndex][discreteIndex], unit );
		}
		for (unsigned int continuousIndex = 0; continuousIndex < continuousNames.size(); ++continuousIndex )
		{
			IConstraint* thisConst=NULL;
			try
			{
				thisConst=generationBoundary->GetConstraint( *continuousNames_ref[continuousIndex] );
			}
			catch(...)
			{
				cout << endl;
				exit(53);
			}
			string unit = thisConst->GetUnit();
			init_temporaryDataPoint->SetObservable( continuousNames[continuousIndex], 0.0, unit );
		}

		//init_temporaryDataPoint->Print();
		//Make the function wrapper
		IntegratorFunction* combinationFunction = new IntegratorFunction( InputPDF, init_temporaryDataPoint, continuousNames, discreteNames, minima, ranges, generationBoundary );

		//cout << InputPDF->GET_ID() << endl;
		//if( InputPDF->GetCacheStatus() )	cout << "HAS CACHED FOAM" << endl;
		//else	cout << "HAS NOT CACHED FOAM" << endl;

		TFile* MC_Cache = NULL;
		TFoam * foamGenerator = NULL;

		if( !InputPDF->GetMCCacheStatus() )
		{
			//      Generate unique name for this foam instance
			TString Name("Foam-");
			Name.Append(InputPDF->GetLabel());
			Name.Append("-");
			Name+=combinationIndex;
			Name.Append("_");Name+=rand;
			string CleanName=Name.Data(); replace( CleanName.begin(), CleanName.end(), '.', '_' ); Name=CleanName.c_str();
			TString FoamName("Foam_");FoamName+=combinationIndex;
			//      Create a root file to store the TFoam universe
			TString RootName(Name);
			RootName.Append(".root");

			cout << "FOAM NOT Cached, Generating Foam:\t" << Name << endl;
			MC_Cache = new TFile( RootName, "RECREATE" );
			MC_Cache->Write( "", TObject::kOverwrite );
			/*
			if( debug != NULL )
			{
				if( debug->DebugThisClass("Foam") )
				{
					cout << "File Created" << endl;
				}
			}
			*/
			//Initialise Foam
			foamGenerator = new TFoam( Name );
			foamGenerator->SetkDim( Int_t(continuousNames.size()) );
			foamGenerator->SetPseRan( rootRandom );
			foamGenerator->SetRho( combinationFunction );	//	Can afford to Boot Foam's ability if we're using just one cached instance :D
			foamGenerator->SetnCells( 1000 );	//	1000	Total number of bins to construct
			foamGenerator->SetnSampl( 200 );	//	200	Samples to take when constructing bins
			foamGenerator->SetnBin( 8 );		//	8	Bins along each axis
			foamGenerator->SetOptRej( 1 );		//	1/0	Don't/Use Weighted Distribution
			foamGenerator->SetOptDrive( 2 );	//	1/2	Best Varience/Weights
			foamGenerator->SetEvPerBin( 25 );	//	25	Weights per bin... This doesn't Saturate as object is written before generating events
			foamGenerator->SetChat( 0 );		//	0	verbosity
			foamGenerator->SetMaxWtRej( 1.1 );	//	1.1	Unknown what effect this has, something to do with weights
			/*
			if( debug != NULL )
			{
				if( debug->DebugThisClass("Foam") )
				{
					cout << "Initializing Foam" << endl;
				}
			}
			*/
			foamGenerator->Initialize();
			//			foamGenerator->ResetPseRan( rootRandom );
			//			foamGenerator->ResetRho( combinationFunction );	//	Can afford to Boot Foam's ability if we're using just one cached instance :D
			//			foamGenerator->Initialize();
			//	As we haven't cached yet, write to file
			foamGenerator->Write( Name, TObject::kOverwrite );
			cout << "Storing TFOAM TObject in:\t\t" << RootName << endl;
			/*
			if( debug != NULL )
			{
				if( debug->DebugThisClass("Foam") )
				{
					cout << "Adding Cache" << endl;
				}
			}
			*/
			InputPDF->AddCacheObject( Name.Data() );
			MC_Cache->Write( "", TObject::kOverwrite );
			InputPDF->Can_Remove_Cache( true );
			//MC_Cache->Close();
		}
		else
		{
			TString Name = InputPDF->GetMCCacheNames()[combinationIndex];
			TString RootName(Name); RootName.Append(".root");
			TString FoamName("Foam_");FoamName+=combinationIndex;
			rootRandom = RapidFitRandom::GetRandomFunction();
			cout << "FOAM Cached, Re-Aquiring TFOAM TObject:\t" << Name << endl;

			ifstream input_file;
			input_file.open( RootName.Data(), ifstream::in );
			input_file.close();
			bool access_fail = input_file.fail();

			if( access_fail )
			{
				cout << "\nWe have Failed to re-aquire the TFoam File, Regenerating one for the Fit\n" << endl;
				InputPDF->Remove_Cache();

				//      Generate unique name for this foam instance
				Name="Foam-";
				Name.Append(InputPDF->GetLabel());
				Name.Append("-");
				Name+=combinationIndex;
				Name.Append("_");Name+=rand;
				string CleanName=Name.Data(); replace( CleanName.begin(), CleanName.end(), '.', '_' ); Name=CleanName.c_str();
				FoamName="Foam_";FoamName+=combinationIndex;
				//      Create a root file to store the TFoam universe
				RootName=Name;
				RootName.Append(".root");

				cout << "FOAM NOT Cached, Generating Foam:\t" << Name << endl;
				MC_Cache = new TFile( RootName, "RECREATE" );
				MC_Cache->Write( "", TObject::kOverwrite );
				//Initialise Foam
				foamGenerator = new TFoam( Name );
				foamGenerator->SetkDim( Int_t(continuousNames.size()) );
				foamGenerator->SetPseRan( rootRandom );
				foamGenerator->SetRho( combinationFunction );   //      Can afford to Boot Foam's ability if we're using just one cached instance :D
				foamGenerator->SetnCells( 1000 );       //      1000    Total number of bins to construct
				foamGenerator->SetnSampl( 200 );        //      200     Samples to take when constructing bins
				foamGenerator->SetnBin( 8 );            //      8       Bins along each axis
				foamGenerator->SetOptRej( 1 );          //      1/0     Don't/Use Weighted Distribution
				foamGenerator->SetOptDrive( 2 );        //      1/2     Best Varience/Weights
				foamGenerator->SetEvPerBin( 25 );       //      25      Weights per bin... This doesn't Saturate as object is written before generating events
				foamGenerator->SetChat( 0 );            //      0       verbosity
				foamGenerator->SetMaxWtRej( 1.1 );      //      1.1     Unknown what effect this has, something to do with weights
				foamGenerator->Initialize();
				//                      foamGenerator->ResetPseRan( rootRandom );
				//                      foamGenerator->ResetRho( combinationFunction ); //      Can afford to Boot Foam's ability if we're using just one cached instance :D
				//                      foamGenerator->Initialize();
				//      As we haven't cached yet, write to file
				//MC_Cache->Write( "", TObject::kOverwrite );
				foamGenerator->Write( Name, TObject::kOverwrite );
				cout << "Storing TFOAM TObject in:\t\t" << RootName << endl;
				InputPDF->AddCacheObject( Name.Data() );
				MC_Cache->Write( "", TObject::kOverwrite );
				InputPDF->Can_Remove_Cache( true );
			}
			else
			{
				MC_Cache = new TFile( RootName, "READ" );
				//gDirectory->ls();
				//Cached_Files.back()->Map();
				//Cached_Files.back()->ShowStreamerInfo();
				//Cached_Files.back()->GetListOfKeys()->Print();
				foamGenerator = (TFoam*) MC_Cache->Get( Name );
				//	This code has been production testeted enough such that I think this is pointlessly scaring users
				//cout << "Checking Consistancy, will crash if this fails... this is unavoidable" << endl;
				//cout << MC_Cache << endl;
				//cout << foamGenerator << endl;
				if( MC_Cache == NULL || foamGenerator == NULL )
				{
					cerr << "Error re-aquiring TFOAM object. This is unrecoverable" << endl;
					cout << "Error re-aquiring TFOAM object. This is unrecoverable" << endl;
					exit(-6542);
				}
				foamGenerator->ResetPseRan( rootRandom );
				foamGenerator->ResetRho( combinationFunction );
                                foamGenerator->SetnCells( 1000 );       //      1000    Total number of bins to construct
                                foamGenerator->SetnSampl( 200 );        //      200     Samples to take when constructing bins
                                foamGenerator->SetnBin( 8 );            //      8       Bins along each axis
                                foamGenerator->SetOptRej( 1 );          //      1/0     Don't/Use Weighted Distribution
                                foamGenerator->SetOptDrive( 2 );        //      1/2     Best Varience/Weights
                                foamGenerator->SetEvPerBin( 25 );       //      25      Weights per bin... This doesn't Saturate as object is written before generating events
                                foamGenerator->SetChat( 0 );            //      0       verbosity
                                foamGenerator->SetMaxWtRej( 1.1 );      //      1.1     Unknown what effect this has, something to do with weights
				foamGenerator->CheckAll(0);
				//	Make one event here to check everything was processed correctly
				foamGenerator->MakeEvent();
				cout << "Check OK" << endl;
			}
		}

		Open_Files.push_back( MC_Cache );
		//Debug
		//char filename[100];
		//sprintf( filename, "DebugFoamPlot.%d.c", combinationIndex );
		//foamGenerator->RootPlot2dim(filename);
		//foamGenerator->PrintCells();

		//Store the foam generator
		foamGenerators.push_back( foamGenerator );
		storedIntegrator.push_back( combinationFunction );
		delete init_temporaryDataPoint;
	}

	InputPDF->SetMCCacheStatus( true );
}