GSI Forum
GSI Helmholtzzentrum für Schwerionenforschung

Home » PANDA » PandaRoot » Analysis » Different results for same information extracted in different ways
Re: Different results for same information extracted in different ways [message #17489 is a reply to message #17464] Mon, 10 November 2014 15:30 Go to previous messageGo to previous message
Mamen is currently offline  Mamen
Messages: 55
Registered: January 2009
Location: Mainz
continuous participant
From: *kph.uni-mainz.de
Dear All,

I'm getting more and more confuse about the behavior of root. In order to clarify how a histogram is filled using "->Project()" or looping over a TTree and using "->Fill()" I created a small macro:

// 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();

}


As you can see, changing the value of "W2" and "FW" I can select which of the four root files to read:

epempi0-W2-10-Delta0-bw-LargeQ2-Merged.root
epempi0-W2-10-Delta0-fw-LargeQ2-Merged.root
epempi0-W2-5-Delta0-bw-LargeQ2-Merged.root
epempi0-W2-5-Delta0-fw-LargeQ2-Merged.root

I have many problems with this little macro:

1.- If I run

> root FillHisto_test.C+

it crashes for all four root files.

2.- If I run:

> root FillHisto_test.C

it runs only for the file epempi0-W2-5-Delta0-fw-LargeQ2-Merged.root.

3.- I don't get the same number of events in the Histograms using ->Project() or looping over the TTree and doing ->Fill().
(I principally want to understand why, but problems 1.- and 2.- are difficulting me the process).

I am using the following root version:
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.34/05  14 February 2013   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.34/05 (tags/v5-34-05@48582, Nov 05 2013, 17:07:52 on linuxx8664gcc)

CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.


and I attach to this post:

a.- The macro FillHisto_test.C.

b.- The four root files I want to read, downloadable under:
https://fileshare.zdv.uni-mainz.de/7lGNaukAXiRNxqfPERB7yw.repo
Username: PandaRoot
Password: v3UyxFX8

c.- A file Output.txt with the outputs of the macro run with the four files, compiled and not compiled.

If somebody has any ideas, or can help me in some way, I would be very, very thankful.

Thanks a lot in advance.
Mamen

 
Read Message icon9.gif
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: vertex reconstruction without fitting?
Next Topic: Vertex fitter for two consecutive decays
Goto Forum:
  


Current Time: Fri Nov 15 03:50:04 CET 2024

Total time taken to generate the page: 0.00659 seconds