2-D histogram disappears but title stays

Hello,

I wrote a macro in order to plot a 2 dimensionnal histogram from a root file, but when it’s done drawing it, the graphic along with the axis disappear and the title stays.
I couldn’t find out why…

Here is the macro :

#include <TStyle.h>
#include <TCanvas.h>
#include <fstream.h>
#include <iostream.h>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <TH2.h>
#include <string.h>
#include <time.h>

void histo2dfromroot() {

  gROOT->Reset();
  
  TFile f("HVout_06_25_2004_09_00_14_AM.root");
  TTree *T = HVtree;
  TBranch *br;

  TCanvas *c = new TCanvas("canvas", "histo2d", 50, 50, 900, 700);

  struct thvinfo{
    int sts;
    float vm,im;
    time_t t;
  };
  thvinfo info;
  
  int modmin = 50;
  int modmax = 51;
  
  vector<float> Ivect;
  vector<int> chvect; 
    
  for ( int mod = modmin ; mod <= modmax ; mod++ ){
    for ( int ch = 0 ; ch < 16 ; ch++){
      int index = mod*100+ch;
      T->SetBranchAddress( Form( "channel_%d", index), &info);
      br = HVtree->GetBranch( Form( "channel_%d", index));
      int n = br->GetEntries();
      for (int k = 0 ; k < n ; k++){
	br->GetEntry(k);
	Ivect.push_back(info.im);
	chvect.push_back(index);
      }    
    }
  } 
  
  float *Current = new float[Ivect.size()];
  int *Channel = new int[Ivect.size()];
  
  int Nbins = ((modmax - modmin) + 1)*16;
  float binmin = (modmin*16) - 0.5;
  float binmax = (modmax*16+16) - 0.5;

  TH2F *h = new TH2F("histo2d","histo2d",Nbins,binmin,binmax,2000,-0.005,1.995);
  
  for (unsigned m=0 ; m<Ivect.size() ; m++){
    Current[m] = Ivect[m];
    Channel[m] = chvect[m];
    h->Fill(Channel[m],Current[m]);
  }
    
  h->Draw("lego2");

  c->Update();

}

Thank you.

We cannot try your example. You have not sent the file HVout_06_25_2004_09_00_14_AM.root. When you post a macro make sur we can execute it. Also when you send “long” macros it is better to attached them to the post than putting it in the text body. Code in the text body does not always work because phpBB2 tries sometimes to interpreted it as HTML and therefore copy/paste is not working. This force us to make tedious editing. Thanks in advance.

My apologies, I have attached the macro.
As for the root file, I get the “tried to upload an empty file” error from the forum, so considering that it seems your are at CERN, you can acces it through the path :

/afs/cern.ch/user/a/arfaoui/public/HVout_06_25_2004_09_00_14_AM.root

I’m sorry for making your job so hard !
In advance, thank you.
graph2d.C (1.95 KB)

I’ve attached the wrong macro!
There it is !
Sorry for being such a pain in the a**!
histo2dfromroot.C (1.42 KB)

You should access the data file the following way:

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

thank you it works !

what exactly is, in this case, the difference between :

TFile f(“file.root”);

and

TFile *f = new TFile(“file.root”);

?

anyway thank you again for your precious help ! :wink:

When you create TFile by reference (not a pointer) it exists only inside the macro and is deleted when the macro exits. TFile is the owner of the objects you ploted and therefore they are deleted also. Accessing TFile via a pointer is the way to go.

[quote=“spyne”]what exactly is, in this case, the difference between :

TFile f(“file.root”);

and

TFile *f = new TFile(“file.root”);

?[/quote]
You can add one line to break the “unwanted” :blush: relationship between your data file and your histogram. Instead of yours TH2F *h = new TH2F("histo2d","histo2d",Nbins,binmin,binmax,2000,-0.005,1.995);
you need TH2F *h = new TH2F("histo2d","histo2d",Nbins,binmin,binmax,2000,-0.005,1.995); h->SetDirectiory(0);
it is explained by
root.cern.ch/root/htmldoc/TH1.ht … tDirectory
This way, either TFile instantiation would be Ok. However, your original one is better. It does close the data-file eliminating the further “unwanted” relation between the file and all histograms you may want to create afterwards.

Thank you. It does help me understand a little more how root works.
Cheers!