Draw an histo with Tree Problem

Hey,

I don’t understand why i can’t draw my histo

void OMline()
{

TCanvas *c1 = new TCanvas();

TFile *input = new TFile("/home/dupuy/Documents/code_dupuy/Exercices/fichier root/valeurspics2.root","READ");

TTree *tree = (TTree*)input->Get("valeurspics1_tree;1");

double Constant1, Mean1, Sigma1, pics;
int om_number1;

tree->SetBranchAddress("om_number1", &om_number1);
tree->SetBranchAddress("Constant1", &Constant1);
tree->SetBranchAddress("Mean1", &Mean1);
tree->SetBranchAddress("Sigma1", &Sigma1);
tree->SetBranchAddress("pics", &pics);

int entries = tree->GetEntries();

cout << entries << endl;

TH2D *hist7 = new TH2D("hist7", "",1000, 0, 10, 1000, 0, 500000);

for(int i=0; i < entries; i++)  // rentre les valeurs de la branche dans notre variable
    {
      tree->GetEntry(i);

      if (om_number1 == 301)
      {
      hist7->Fill(Mean1,pics);
      }
    }

hist7->Draw();
hist7->GetXaxis()->SetTitle("time (s)");
hist7->GetYaxis()->SetTitle("charge (U.A)");
hist7->GetXaxis()->SetTitleSize(0.05);
hist7->GetYaxis()->SetTitleSize(0.05);
hist7->GetXaxis()->SetLabelSize(0.05);
hist7->GetYaxis()->SetLabelSize(0.05);

}

valeurspics2.root (6.6 KB)

  1. the tree variables were not properly declared
  2. om_number1 is never equal to 301
  3. the histogram limits are wrong
  4. 1000x1000 bins ? really ?
  5. the tree name is valeurspics2_tree

after this fixes I get:

void OMline()
{

   TCanvas *c1 = new TCanvas();

   TFile *input = new TFile("valeurspics2.root");

   TTree *tree = (TTree*)input->Get("valeurspics2_tree");

   double Constant1, Mean1, Sigma1;
   int pics;
   double om_number1;

   tree->SetBranchAddress("om_number1", &om_number1);
   tree->SetBranchAddress("Constant1", &Constant1);
   tree->SetBranchAddress("Mean1", &Mean1);
   tree->SetBranchAddress("Sigma1", &Sigma1);
   tree->SetBranchAddress("pics", &pics);

   int entries = tree->GetEntries();

   cout << entries << endl;

   TH2D *hist7 = new TH2D("hist7", "",40, 0, 120000, 40, 0, 6);

   for(int i=0; i < entries; i++) {
      tree->GetEntry(i);
      hist7->Fill(Mean1,(double)pics);
   }


   hist7->Draw("col");
   hist7->GetXaxis()->SetTitle("time (s)");
   hist7->GetYaxis()->SetTitle("charge (U.A)");
   hist7->GetXaxis()->SetTitleSize(0.05);
   hist7->GetYaxis()->SetTitleSize(0.05);
   hist7->GetXaxis()->SetLabelSize(0.05);
   hist7->GetYaxis()->SetLabelSize(0.05);

}

The plot is:

Thank you so much. I did a bit of anything x)