//-----------------------------------------------------------
// Simple example illustrating how to use the C++ interpreter	
// to fill histograms in a loop and show the graphics results
// This program is a variant of the tutorial "hsumTimer".
//-----------------------------------------------------------


void Example(Int_t nfill=10000, UInt_t seed=0)
{
  Float_t  	xs1, xs2, xmain;
  TFile    	*hfile;
  TTree    	*tree;
  TBranch  	*xmainBranch;
  TBranch  	*xs1Branch;
  TBranch  	*xs2Branch;
  TCanvas  	*theCanvas;
  TH1F		*totalHistogram;
  TH1F		*mainHistogram;
  TH1F		*s1Histogram;
  TH1F		*s2Histogram; 
   
  // Create a file
  hfile = new TFile("Example.root","RECREATE","ROOT file");
   
  // Create the tree branches, one for each variable
  tree        = new TTree("myTree","Example ROOT tree");
  xmainBranch = tree->Branch("xmain", &xmain, "xmain");
  xs1Branch   = tree->Branch("xs1",   &xs1,   "xs1");
  xs2Branch   = tree->Branch("xs2",   &xs2,   "xs2");
  
  // Create the canvas
  theCanvas = new TCanvas("TheCanvas","Example",200,10,600,400);
  theCanvas->SetGrid();
  
  // Create some histograms
  totalHistogram  = new TH1F("totalHistogram","Total Distribution",100,-4,4);
  mainHistogram   = new TH1F("mainHistogram ","Main Contributor",  100,-4,4);
  s1Histogram     = new TH1F("s1Histogram",   "First Signal",      100,-4,4);
  s2Histogram     = new TH1F("s2Histogram",   "Second Signal",     100,-4,4);
   
  // this makes sure that the sum of squares of weights will be stored
  totalHistogram->Sumw2();
  
  totalHistogram->SetMarkerStyle(21);
  totalHistogram->SetMarkerSize(0.7);
  totalHistogram->SetMaximum(nfill/20.);
  
  // Draw error bars with perpendicular lines at the edges
  totalHistogram->Draw("e1");
  
  mainHistogram->SetFillColor(16);  
  // The "same" option superimposes the histogram
  // on previous picture in the same pad
  mainHistogram->Draw("same");
  
  s2Histogram->SetFillColor(46);
  s2Histogram->Draw("same");
  
  s1Histogram->SetFillColor(42);
  s1Histogram->Draw("same");
  
  // Refresh the canvas to draw the axis and grid
  theCanvas->Update();
  
  // Fill histograms randomly
  gRandom->SetSeed(seed);
  
  for (Int_t i=0; i< nfill; i++) {
  	 // generate values
     xmain = gRandom->Gaus(-1,1.5);
     xs1   = gRandom->Gaus(-0.5,0.5);
     xs2   = gRandom->Landau(1,0.15);
	 
	 // fill the histograms
     mainHistogram->Fill(xmain);
     s1Histogram->Fill(xs1,0.3);
     s2Histogram->Fill(xs2,0.2);
     totalHistogram->Fill(xmain);
     totalHistogram->Fill(xs1,0.3);
     totalHistogram->Fill(xs2,0.2);
	 
	 // write the variables to the tree
     tree->Fill();
  }
  
  // Draw the canvas
  theCanvas->Modified();
  theCanvas->Update();
  
   
  // add the canvas 
  hfile->Append(theCanvas);

  
  // save the file
  hfile->Write();
  
  // For batch programs we would want to delete the file we created.
  // this cleans up the memory.  It does two things:
  //     1. closes the file
  //     2. removes the file and all it's objects from memory
  // Because it removes all the objects associated with the file 
  // from memory the histograms are no longer accesible 
  // and hence no longer displayed. Histograms and trees are automatically
  // associated with a file. In this example the Canvas was explicitly
  // stored, so it will also dissappear. In order to view the histograms 
  // created here, the "delete hfile" line is commented out.
  
//delete hfile;
}

