Read histogram from root file and do it a canvas


Please read tips for efficient and successful posting and posting code

ROOT Version: 5.34/36
Platform: UBUNTU 16.04
Compiler: Not Provided


I’m trying to read an histograms in a root file and plot in only one canvas all histograms to compare. In a simple way I do this code:

#include "TROOT.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TBrowser.h"
#include "TH1.h"
#include "TF1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include "TBranch.h"
#include "Rtypes.h"

void mat1(){  /// fecha linha 175 com semi sphere

/// varaibles declarations
  gROOT->cd();

  //int nbins = 5000;
  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);
  
  TFile *f = new TFile ("enerDepQ.root", "READ");
  TH1F * h1 = (TH1F*)f->Get("edepAg");
  TH1F * h2 = (TH1F*)f->Get("edepI");
  TH1F * h3 = (TH1F*)f->Get("edepAIR");
  TH1F * h4 = (TH1F*)f->Get("edepTi");
  TH1F * h5 = (TH1F*)f->Get("edepWAT");
  TH1F * h6 = (TH1F*)f->Get("edepWorld");
  TH1F * h7 = (TH1F*)f->Get("edepOOW");
  TH1F * h8 = (TH1F*)f->Get("edepTOTAL");//*/

  f->ls();

   h1->SetDirectory(0);  
	h1->SetFillColor(0);
   	h1->SetLineColor(kBlack);      	
   
   h2->SetDirectory(0);
	h2->SetFillColor(0);
   	h2->SetLineColor(kRed);

   h3->SetDirectory(0);
	h3->SetFillColor(0);
	h3->SetLineColor(kGreen);

   h4->SetDirectory(0);
	h4->SetFillColor(0);
  	h4->SetLineColor(kBlue);

   h5->SetDirectory(0);
	h5->SetFillColor(0);
   	h5->SetLineColor(kAzure-4); 		
    
   h6->SetDirectory(0);
	h6->SetFillColor(0);
	h6->SetLineColor(kMagenta);

   h7->SetDirectory(0);
	h7->SetFillColor(0);
   	h7->SetLineColor(kCyan); 

   h8->SetDirectory(0);
	h8->SetFillColor(0);
	h8->SetLineColor(kViolet-4);//*/

  h1->Draw("HIST L");
  h2->Draw("same");
  h3->Draw("same");
  h4->Draw("same");
  h5->Draw("same");
  h6->Draw("same");
  h7->Draw("same");
  h8->Draw("same");//*/ 

  gPad->SetLogy();

  gPad->BuildLegend();
  
}

enerDepQ.root (15.1 KB)

What I get is a plot mat1.pdf that the range of axis Y (counts) was the first histogram and not rearrange the axis when including the others data. How can I fix it? Anyone could help me?

Thanks

Best regards

mat1.pdf (237.3 KB)

THStack

Thanks @Wile_E_Coyote. before do that I present above I do it with the function [THStack]g(https://root.cern.ch/doc/master/classTHStack.html), but it seems that the result is so different regarding the graphic. Bellow I upload the example with THStack that I do and the code:

#include "TROOT.h"
#include "TMath.h"
#include "TCanvas.h"
#include "TFile.h"
#include "TTree.h"
#include "TGraph.h"
#include "TBrowser.h"
#include "TH1.h"
#include "TF1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TStopwatch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include "TBranch.h"
#include "Rtypes.h"

void mat(){ 

/// varaibles declarations
  gROOT->cd();

  gStyle->SetOptStat(1111111);
  TCanvas * c1 = new TCanvas("c1", "c1", 800, 600);

  THStack *hs = new THStack("edep","Edep from the materials;Edep (MeV);Counts");
   
  TFile *f = new TFile("enerDepQ.root", "READ");
  TH1F * h1 = (TH1F*)f->Get("edepAg");
  TH1F * h2 = (TH1F*)f->Get("edepI");
  TH1F * h3 = (TH1F*)f->Get("edepAIR");
  TH1F * h4 = (TH1F*)f->Get("edepTi");
  TH1F * h5 = (TH1F*)f->Get("edepWAT");
  TH1F * h6 = (TH1F*)f->Get("edepWorld");
  TH1F * h7 = (TH1F*)f->Get("edepOOW");
  TH1F * h8 = (TH1F*)f->Get("edepTOTAL");

  f->ls();

   h1->SetDirectory(0);  
	h1->SetFillColor(0);
   	h1->SetLineColor(kBlack);      	
   
   h2->SetDirectory(0);
	h2->SetFillColor(0);
   	h2->SetLineColor(kRed);

   h3->SetDirectory(0);
	h3->SetFillColor(0);
	h3->SetLineColor(kGreen);

   h4->SetDirectory(0);
	h4->SetFillColor(0);
  	h4->SetLineColor(kBlue);

   h5->SetDirectory(0);
	h5->SetFillColor(0);
   	h5->SetLineColor(kAzure-4); 		
    
   h6->SetDirectory(0);
	h6->SetFillColor(0);
	h6->SetLineColor(kMagenta);

   h7->SetDirectory(0);
	h7->SetFillColor(0);
   	h7->SetLineColor(kCyan); 

   h8->SetDirectory(0);
	h8->SetFillColor(0);
	h8->SetLineColor(kViolet-4);


   hs->Add(h1);
   hs->Add(h2);
   hs->Add(h3);
   hs->Add(h4);
   hs->Add(h5);
   hs->Add(h6);
   hs->Add(h7);
   hs->Add(h8); //*/

   // Make the legend:
  auto legend = new TLegend(0.1,0.7,0.48,0.9);
  // Adds legend entries:
  legend->AddEntry(h1, "edepAg");
  legend->AddEntry(h2, "edepI");
  legend->AddEntry(h3, "edepAIR");
  legend->AddEntry(h4, "edepTi");
  legend->AddEntry(h5, "edepWAT");
  legend->AddEntry(h6, "edepWorld");
  legend->AddEntry(h7, "edepOOW");
  legend->AddEntry(h8, "edepTOTAL");
  legend->Draw();
  hs->Draw("HIST L");

  gPad->SetLogy();

  c1->BuildLegend();

}

This provide me the figure mat.pdf (207.8 KB). My question here is: It seems to me that some results are sum with the others, this because the line in 0.0355 seems to be the sum of every plot, how can I fix this?

Thanks

Best regards

hs->Draw("NOSTACK HIST");

Thanks @Wile_E_Coyote! It works fine now!

Best regards!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.