Histograms in loop

Hi, while using root 5.34 in the cluster of the national lab where I work, I found a constant error when trying to fill some histograms declared in a .C file apart of the main macro.C, and defined in a loop (by the way, it used to work fine with earlier versions of root, 5.28 or so, as I recall). The loop looks like this.

#include "TFile.h"              
#include "TH1.h"                              
#include "TH2.h"       
#include "histo_booking.h" 
#include "skim_assym.h"

TH2F *hmass_p[10];
char *mass_p = new char[10];

  for(Int_t i = 0; i <=10; i++){

    sprintf(mass_p,"mass_p_%d",i);                               //proton mass checking
    hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);
}

I get a segmentation violation all the time, whereas if I just use a histogram like

  hmass_p = new TH1F("mass_p","mass_p",333,0.0,1.5);

Everything works fine.

What could be wrong?

Thanks.

FYI: Posting code? Read this first!

You declare an array of 10 TH2F:

TH2F *hmass_p[10];

And then you create 11 TH1F:

   for (Int_t i = 0; i <=10; i++) { // == 11
      hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);

Cheers, Bertrand.

Sorry, It was my mistake in writing this post, it is like this:

#include "TFile.h"              
#include "TH1.h"                              
#include "TH2.h"       
#include "histo_booking.h" 
#include "skim_assym.h"

TH1F *hmass_p[10];
char *mass_p = new char[10];

  for(Int_t i = 0; i <=10; i++){

    sprintf(mass_p,"mass_p_%d",i);                               //proton mass checking
    hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);
}

But still I get a segmentation violation all the time, whereas if I just use a histogram like

  hmass_p = new TH1F("mass_p","mass_p",333,0.0,1.5);

it works

Please read my post carefully, it’s not about the type, it’s about the number of histograms!

I just corrected the number of histos in order to match them in the loop, but to no avail. Still segmentation fault.

#include "TFile.h"              
#include "TH1.h"                              
#include "TH2.h"       
#include "histo_booking.h" 
#include "skim_assym.h"

TH1F *hmass_p[11];
char *mass_p = new char[10];

  for(Int_t i = 0; i <=10; i++){

    sprintf(mass_p,"mass_p_%d",i);                               //proton mass checking
    hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);
}

The original code from a couple years ago that worked was like this

#include "TFile.h"              
#include "TH1.h"                              
#include "TH2.h"       
#include "histo_booking.h" 
#include "skim_assym.h"

void histo_booking(void){

  for(int i = 0; i <=10; i++){

    char mass_p[50];
    sprintf(mass_p,"mass_p_%d",i);                               //proton mass checking
    hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);
   }
}

This works just fine for me:

#include "TH1.h"

TH1F *hmass_p[11];
char *mass_p = new char[10];

void histo_booking()
{
   for (Int_t i = 0; i <=10; i++) {
      sprintf(mass_p,"mass_p_%d",i);  //proton mass checking
      hmass_p[i] = new TH1F(mass_p,mass_p,333,0.0,1.5);
   }
}

Doesn’t do it for me. Could it be the 5.34 version? I had different problems with other macros last year when running them with v 5.34 but they ran fine when I upgraded to v 6.10.

Cheers.

No problem with ROOT v5-34-00-patches (5.34/37)…

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