Problem Drawing TH1I

hi,

I am not able to draw a spectrum which I am sure that I fill correctly and I do not know why

here there is the code

[code]#include “TTreeIndex.h”
#include “TTree.h”
#include “TFile.h”
#include <stdio.h>
#include
#include
#include <string.h>
#include “TCanvas.h”
#include “TH1.h”
#include “TVirtualIndex.h”
#include “TTreeFormula.h”

using namespace std;

Int_t TNT2_PulserAnalysis(TString s = “pippo”)
{
Int_t j = 0, k = 0, m = 0;

Int_t           Channel_Number;		// number of the channel in the selected TNT card. These numbers could be equal for all cards. 
Int_t			Card_Number;		// number of the card used in the DAQ
Long64_t        Energy;				// energy is the channel position in the spectra 
Long64_t		Event_Time;			// the time when the event happens  
Long64_t        Trigger_Number;		// trigger associated to that event

if(s == "pippo") {
	cout << "nessun file da analizzare" << endl;
	return 0;
	}
TFile f(s);
if (f.IsZombie()) {
   std::cout << "Error opening file" << std::endl;
   exit(-1);
}
cout << "aperto il file" << endl;
TTree *myTree = (TTree*) f.Get("TNTtree");
myTree->SetBranchAddress("Card_Number",&Card_Number);
myTree->SetBranchAddress("Channel_Number",&Channel_Number);
myTree->SetBranchAddress("Energy",&Energy);
myTree->SetBranchAddress("Event_Time",&Event_Time);
myTree->SetBranchAddress("Trigger_Number",&Trigger_Number);

myTree->BuildIndex("Event_Time");
TTreeIndex *index = (TTreeIndex*)myTree->GetTreeIndex();
cout << "indicizzato" << endl;

//Spettri
TH1I *h[8];
for(j = 0 ; j < 8 ; j++){
	h[j] = new TH1I(Form("h%d",j),Form("Spectrum channel %d",j),2000,0,1999);
}
// spectra creation end

for(Long64_t i = 0; i < 10000 /*index->GetN() - 1*/ ; i++ ) {
	Long64_t local = myTree->LoadTree( index->GetIndex()[i] );
	myTree->GetEntry(local);
	
	cout << "I: " << local << "\t" << Card_Number << "\t" << Channel_Number << "\t" << Energy << "\t" << Event_Time << "\t" << Trigger_Number << endl;

	switch(Card_Number){
		case (232):
			switch(Channel_Number){
				case (1):
					h[Channel_Number-1]->Fill(Energy);
					cout << "filled with " << Energy << " spectrum " << Channel_Number-1 << endl;
					break;
				case (2):
					h[Channel_Number-1]->Fill(Energy);
					break;
				case (3):
					h[Channel_Number-1]->Fill(Energy);
					break;
				case (4):
					h[Channel_Number-1]->Fill(Energy);
					break;
			}
		case (216):
			switch(Channel_Number){
				case (1):
					h[Channel_Number+3]->Fill(Energy);
					break;
				case (2):
					h[Channel_Number+3]->Fill(Energy);
					break;
				case (3):
					h[Channel_Number+3]->Fill(Energy);
					break;
				case (4):
					h[Channel_Number+3]->Fill(Energy);
					break;
			}
	}
}
h[0]->Draw();

for(j = 0 ; j < 2000 ; j++)
	cout << h[0]->GetBinContent(j) << endl;
	
/*// Preparo i Canvas
TCanvas *c1[2];
m = 0;
for(j = 0 ; j < 2 ; j++){
	c1[j] = new TCanvas(Form("c%d",j+1),Form("All spectrum card %d",j+1),800,800);
	c1[j]->Divide(2,2);
	for (k = 0 ; k < 4 ; k++){
		c1[j]->cd(k+1);
		h[m]->Draw("HISTO");
		cout << "disegno " << j << " " << k << endl;
		m++;
	}
}*/
// fine Canvas
		
return 1;

}[/code]

in particular in the final part of the I check calling the GetBinContent function and the print is what I expected to be, but when I call the function Draw nothing is show in the canvas

why?

thanks a lot for your help
ciao
Antonio

You macro gives:

root [1]  .x TNT2_PulserAnalysis.C
nessun file da analizzare
(Int_t)0
root [2] 

Is it what it is supposed to do ? I am not sure to understand what this message means… Something like
"File not found" ? …

hi,

the macro will analyse a .root file with 20Million of events
I did not attach the file. I hoped that the problem was clearly visible in the code.
here there is a link to the file I want to analyse.

dropbox.com/s/jxcttpnucinf4 … rTest.root

the command to run is
TNT2_PulserAnalysis(“TNT_pulserTest.root”)

I found a small error in the macro: I missed to put the break and the end of the primary switch. This does not solve my problem.

In fact I would like to have the drawing of 8 spectra in two canvas as result of the macro.

ciao
Antonio

I simplified your macro to make it work.
You can start from that now:


#include "TTreeIndex.h"
#include "TTree.h"
#include "TFile.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include "TCanvas.h"
#include "TH1.h"
#include "TVirtualIndex.h"
#include "TTreeFormula.h"

void TNT2_PulserAnalysis()
{
   Int_t j = 0, k = 0, m = 0;
   Int_t     Channel_Number; // number of the channel in the selected TNT card. These numbers could be equal for all cards.
   Int_t     Card_Number;    // number of the card used in the DAQ
   Long64_t  Energy;         // energy is the channel position in the spectra
   Long64_t  Event_Time;     // the time when the event happens
   Long64_t  Trigger_Number; // trigger associated to that event

   TFile *f = new TFile("TNT_pulserTest.root");

   TTree *myTree = (TTree*) f->Get("TNTtree");
   myTree->SetBranchAddress("Card_Number",&Card_Number);
   myTree->SetBranchAddress("Channel_Number",&Channel_Number);
   myTree->SetBranchAddress("Energy",&Energy);
   myTree->SetBranchAddress("Event_Time",&Event_Time);
   myTree->SetBranchAddress("Trigger_Number",&Trigger_Number);

   myTree->BuildIndex("Event_Time");
   TTreeIndex *index = (TTreeIndex*)myTree->GetTreeIndex();
   cout << "indicizzato" << endl;

   TH1I *h[8];
   for(j = 0 ; j < 8 ; j++){
      h[j] = new TH1I(Form("h%d",j),Form("Spectrum channel %d",j),2000,0,1999);
   }


   for (Long64_t i = 0; i < 10000 /*index->GetN() - 1*/ ; i++ ) {
      Long64_t local = myTree->LoadTree( index->GetIndex()[i] );
      myTree->GetEntry(local);

      switch(Card_Number){
         case (232):
            switch(Channel_Number){
               case (1):
                  h[Channel_Number-1]->Fill(Energy);
                  cout << "filled with " << Energy << " spectrum " << Channel_Number-1 << endl;
                  break;
               case (2):
                  h[Channel_Number-1]->Fill(Energy);
                  break;
               case (3):
                  h[Channel_Number-1]->Fill(Energy);
                  break;
               case (4):
                  h[Channel_Number-1]->Fill(Energy);
                  break;
            }
         case (216):
            switch(Channel_Number){
               case (1):
                  h[Channel_Number+3]->Fill(Energy);
                  break;
               case (2):
                  h[Channel_Number+3]->Fill(Energy);
                  break;
               case (3):
                  h[Channel_Number+3]->Fill(Energy);
                  break;
               case (4):
                  h[Channel_Number+3]->Fill(Energy);
                  break;
            }
      }
   }
   h[0]->Draw();
}

thank you!