Plot 4 different histograms from 4 roots file at one canvas

Am trying to 4 different histograms from 4 different root files but it give me error;
Error in : Trying to access a pointer that points to an invalid memory address…
Execution of your code was aborted.

Could you post a small macro reproducing your problem because it is actually very difficult to help you with such information.

if you want am posting my code here;

#include "TStyle.h"
#include "TFile.h"
#include "TTree.h"
#include "TH1.h"
#include "TLegend.h"
#include "TCanvas.h"
#include "TROOT.h"
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include "TChain.h"
#include <iomanip>
#include <fstream>
#include <utility>


void  Macro() {
  TFile *file = new TFile("peaks_OT_2017.root");
  TFile *f = new TFile("decos_OT_2017.root");
TFile *f1 = new TFile("decomodification_2017.root");
TFile *f2 = new TFile("peakmodification_2017.root");
 if(!file) {
        std::cout<<"FILE CAN'T BE OPENED"<<std::endl;
              }

         f->cd("prod/SensorType;1");
    file->cd("prod/SensorType;1");
   gDirectory->ls();


      TH2D *h1 = (TH2D*)gDirectory->Get("h_per_sensor");

        TH2D *h2 = (TH2D*)gDirectory->Get("h_per_sensor");
       TH2D *h3 = (TH2D*)gDirectory->Get("h_per_sensor");
 TH2D *h4 = (TH2D*)gDirectory->Get("h_per_sensor");
    TCanvas * c11 = new TCanvas();
 h1->SetLineColor(kBlue);
 h1->SetMarkerStyle(20);
  h2->SetLineColor(kRed);
h2->SetMarkerStyle(21);
 h3->SetLineColor(kGreen);
h3->SetMarkerStyle(33);
 h4->SetLineColor(kYellow);
h4->SetMarkerStyle(34);
         // h1->Scale(1./h1->GetEntries()  );
           //       h2->Scale(1./h2->GetEntries()  );


           h1->Draw();
           h2->Draw("same");
h3->Draw("same");
h4->Draw("same");
 c11->Update();
}
void  Macro() {
   TFile *file = new TFile("peaks_OT_2017.root");
   file->cd("prod/SensorType");
   TH2D *h1 = (TH2D*)gDirectory->Get("h_per_sensor");

   TFile *f = new TFile("decos_OT_2017.root");
   f->cd("prod/SensorType");
   TH2D *h2 = (TH2D*)gDirectory->Get("h_per_sensor");
   
   TFile *f1 = new TFile("decomodification_2017.root");
   f1->cd("prod/SensorType");
   TH2D *h3 = (TH2D*)gDirectory->Get("h_per_sensor");
   
   TFile *f2 = new TFile("peakmodification_2017.root");
   f2->cd("prod/SensorType");
   TH2D *h4 = (TH2D*)gDirectory->Get("h_per_sensor");

   TCanvas * c11 = new TCanvas();
   h1->SetLineColor(kBlue);
   h1->SetMarkerStyle(20);
   h2->SetLineColor(kRed);
   h2->SetMarkerStyle(21);
   h3->SetLineColor(kGreen);
   h3->SetMarkerStyle(33);
   h4->SetLineColor(kYellow);
   h4->SetMarkerStyle(34);

   h1->Draw();
   h2->Draw("same");
   h3->Draw("same");
   h4->Draw("same");
}

but still it give me same error.

thank You but there is a one problem my code is not picking up values of h1,h2,h3 and h4
if u print it it will give me value zero.

Do all your files contain the prod/SensorType directory with the histogram h_per_sensor inside ?

As always in these cases, I have to give the general advice NOT to use the Get function (at least not in combination with an unsafe C style cast). Always use GetObject instead. You can even wrap GetObject in a templated function so that you can use it as a function.

Reason: Get + C style casts lack error checking.

You can use:

template <typename T>
T* safe_get(TDirectory *d, const char *name) {
  T *o;
  d->GetObject(name, o);
  if (!o) cerr << "Cannot find object " << name << "!\n";
  return o;
}

(or similar, untested)

Then replace all Get calls, for example replace

TH2D *h1 = (TH2D*)gDirectory->Get("h_per_sensor");

with

auto h1 = safe_get<TH2D>(gDirectory, "h_per_sensor");

Note: you can also omit the “cd” and look for the object “path/name”.

2 Likes

Thank you Couet, if i forget for instant h_per_sensor if i took one plot then its also give me error like now my code is

void  Macro1() {
   TFile *file = new TFile("peaks_OT_2017.root");
   file->cd("prod/SensorType");
   TH2D *h1 = (TH2D*)gDirectory->Get("localx_rhlocalx_IB1");
cout <<"h1 pointer : "<<h1<<endl;
   TFile *f = new TFile("decos_OT_2017.root");
   f->cd("prod/SensorType");
   TH2D *h2 = (TH2D*)gDirectory->Get("localx_rhlocalx_IB1");

   TFile *f1 = new TFile("decomodification_2017.root");
   f1->cd("prod/SensorType");
   TH2D *h3 = (TH2D*)gDirectory->Get("localx_rhlocalx_IB1");

   TFile *f2 = new TFile("peakmodification_2017.root");
   f2->cd("prod/SensorType");
   TH2D *h4 = (TH2D*)gDirectory->Get("localx_rhlocalx_IB1");

   TCanvas * c11 = new TCanvas();
   h1->SetLineColor(kBlue);
   h1->SetMarkerStyle(20);
   h2->SetLineColor(kRed);
   h2->SetMarkerStyle(21);
   h3->SetLineColor(kGreen);
   h3->SetMarkerStyle(33);
   h4->SetLineColor(kYellow);
   h4->SetMarkerStyle(34);

   h1->Draw();
   h2->Draw("same");
   h3->Draw("same");
   h4->Draw("same");
}

basically there are 14 plots in prod/sensortype i plot all of them . but now just for sake or easiness i want to take just 1 plot from everyfile but it give me same error.

thank you sir, i ll try it may be it will help ful.

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