// c/c++
//#include <stdlib.h>
#include <iostream>


//root

#include "TFile.h"
#include "TTree.h"
#include "TH1D.h"
#include "TH1F.h"

void FillHisto_test(){
  char Filename[256];
  char *directionName;
  char *Pi0FW;

  double W2=5.;
  int FW=1;
  int NBINS=100;
  int BINMIN=-2;
  int BINMAX=2;

  if(FW==0)
    {
      Pi0FW="bw";
      directionName="backward";
    }
  else if (FW==1)
    {
      Pi0FW="fw";
      directionName="forward";
    }
    sprintf(Filename, "/home/moraespi/VariableCosThetaGammaStar/Rootfiles/SimuJan2014/electrons/epempi0-W2-%.0f-Delta0-%s-LargeQ2-Merged.root", W2, Pi0FW);

  cout << "File: "<< Filename<< endl;
  
  // TCanvas *myCanvas;
  // myCanvas=new TCanvas("C", "C", 1);
  TFile *t=new TFile(Filename);
  
  TTree *epempi0Tuple  = (TTree*)t->Get("epempi0Tuple");
 
  // // Project method
  TH1D *Reco;
  Reco = new TH1D ("Reco", "Reco", NBINS, BINMIN, BINMAX);
  epempi0Tuple->Project("Reco", "feppx");//, "eppid>3");
  // Reco->Draw();

  double Total_int=0; 
  double Integral=0;
  double GetEntries=0;
  for (int i=0; i<NBINS+2; i++ )
    {
      Total_int=Total_int+Reco->GetBinContent(i);
    }

  for (int j=1; j<NBINS+1; j++ )
    {
      Integral=Integral+Reco->GetBinContent(j);
    }
  GetEntries=Reco->GetEntries();
  cout << "Number of events in the Histogram using Project: "<<endl;
  cout << " Total_int (0-9)\t Integral (1-8)\t\t GetEntries"<<endl;
  cout << Total_int <<"\t\t\t"<<Integral<<"\t\t\t"<<GetEntries<<endl;
    t->Close();
  

// // Branch Addresses method
  
  TFile *t2=new TFile(Filename);
  
  TTree *epempi0TupleReco  = (TTree*)t2->Get("epempi0Tuple");
    
  float feppx;
  int eppid;

  epempi0TupleReco->SetBranchAddress("feppx", &feppx);
  epempi0TupleReco->SetBranchAddress("eppid", &eppid);

  TH1F *Reco2;
  Reco2 = new TH1F("Fill", "Fill", NBINS, BINMIN, BINMAX);

  long NEntriesReco=(long)epempi0TupleReco->GetEntries();

   double value=0;
   for (int k=0; k<NEntriesReco; k++)
     {
       epempi0TupleReco->GetEntry(k);
       if (k % 100000 == 0 && k != 0) 
	 {      	
	   cout<<"*** Reco Loop *** Getting Entry: "<< k << endl;
	 }		  
       

      // if (eppid>3)
      // 	{
	  value=feppx;
  	  Reco2->Fill(value);
	  //  	}
     }

  double Total_intFill=0; 
  double IntegralFill=0;
  double GetEntriesFill=0;
  for (int i=0; i<NBINS+2; i++ )
    {
      Total_intFill=Total_intFill+Reco2->GetBinContent(i);
    }

  for (int j=1; j<NBINS+1; j++ )
    {
      IntegralFill=IntegralFill+Reco2->GetBinContent(j);
    }
  GetEntriesFill=Reco2->GetEntries();

  cout << "Number of events in the Histogram looping over TTree: "<<endl;
  cout << " Total_intF (0-9)\t IntegralF (1-8)\t\t GetEntriesF"<<endl;
  cout << Total_intFill <<"\t\t\t"<<IntegralFill<<"\t\t\t"<<GetEntriesFill<<endl;

  // //myCanvas->cd();
  // Reco2->SetLineColor(kRed); 
  // Reco2->Draw();
  // //myCanvas->SaveAs("Test_Canvas.eps");
  
    t2->Close();

}
    
