Glib detected

Hi ROOTers,

I have a problem when I launch a macro. It gives me **glib detected **, and then a list of backtrace and memory map and root crash.
In the code I try to read some root files, each containing a tree with a number of variables. For each file, I have to take some of these variables and draw them in different canvases.
I try to do this with 3 for loops, in order to read the files, get the variables and draw them.
Is there a way to fix my problems?
Thanks in advance!!

test2.c (3.54 KB)

Hi,

You should try something like this:

[code] TH1F *h[14];
THStack *hs[14];
TCanvas *c[14];
TLegend *leg[14];

for (j = 0; j<14; j++) {
sprintf(varname, “%s”, var[j].c_str());
h[j] = new TH1F(varname, varname, NBINS, 0.0, xmax[j]);
hs[j] = new THStack(Form(“hs%d”,j), “hstack title…”);
c[j] = new TCanvas(varname, titolo, 500, 300);
leg[j] = new TLegend(0.85, 0.25, 0.95, 0.75);

  for (i = 0; i < 10; i++) {
     sprintf(filename, "%s", v[i].c_str());
     sprintf(titolo, "%s", bs[i].c_str());

[…]
[/code]You should try to use ACLiC ([color=#0000FF].x test2.C+[/color]), the compiler will tell you most of the errors in your code, and use gdb to see where are the problems…

Cheers, Bertrand.

Hi Bertrand!

Thank you very much for your help! I corrected my code with your suggestions.
It seems that my problems were fixed, except for one.
As you saw in the attached file, I define a vector of colors:

TColor *colore[10]
colore[0] = gROOT->GetColor(2) ;

colore[9] = colore[0] = gROOT->GetColor(41) ;

which I use in a for cycle to set the color for each histograms.
But when I execute my program, I get this message:

Error: Can’t call TH1F::SetLineColor(colore[j]) in current scope /home/xxxxx/prova.cpp:138:
Possible candidates are…
(in TH1F)
(in TH1)
public: virtual void TAttLine::SetLineColor(Color_t lcolor);
*** Interpreter error recovered ***

Is there a solution for this?

Thanks for help!

Well, a [color=#0000FF]Color_t[/color] is not a [color=#0000FF]TColor*[/color]. I think the simplest way would be to define colore as following:

Color_t colore[10]; colore[0] = (Color_t)2; .. colore[9] = colore[0] = (Color_t)41;
Cheers, Bertrand.

Now it should work. But I have a more basic question (I’m actually new with ROOT).
The for loops I try to use are not so satisfactory, so I expose my main problem.
I have 10 root files, each containing a tree with tens of variables. I have to take some of these variables (15 + 3 used as flags) and draw them, each on a different canvas.
I attach the code which I’d like to replace with a simpler one (it’s extremely long and a simple change in it takes me very much time!)
How can I set the for loops?

Thanks for any suggestion.

test.cpp (40.1 KB)

Hi,

You should read the Tree chapter in the users guide and the many examples in $ROOTSYS/tutorials/tree
You could do something like (for example):

TFile *f1 = TFile::Open("/home/manuel/500pb_cut1/DYJetsToLL_TuneD6T_M-50_7TeV-madgraph-tauola.root", "READ"); TH1D *h101 = new TH1D("h101","h101",NBINS,HEMIACO_XMIN,HEMIACO_XMAX) ; TTree *t1 = (TTree*)f1->Get("Analysis"); if (t1) t1->Draw("HemiAco>>h101"); [...]
And if all your files have the same data structure, you can also create a TChain (again, read the doc and see the examples)

Cheers, Bertrand.

Hi Bertrand,

sorry for answering too late. I solved my problem by use of what you suggested me in your first reply. It was more suitable for what I needed to do.
But now the problem of “glib detected” rises again in another code.
As usual, I have to read some files and take the same variable from all of them. This variable is actually an array.
This time I use no for loops, but something like this (sorry for its length) :

#include “TTree.h”
#include “TFile.h”

void function()
{
gROOT->SetStyle(“Plain”) ;

Int_t var1, var2 ,var3 ;
Float_t Pt[2] ; // array
Double_t fs = (Double_t) 43.2/500 ; //scale factor

TFile *f1 = new TFile(“file1.root”)
TTree f1 = (TTree)f1->Get(“tree”)
t1->SetBranchAddress(“var1”,&var1) ;
t1->SetBranchAddress(“var2”,&var2) ;
t1->SetBranchAddress(“Pt”,Pt) ;
TH1F *h1 = new TH1F(“h1”,“h1”,100,0,300) ;

Int_t nentries = (Int_t)t1->GetEntries();
for (Int_t i = 0 ; i<nentries ; i++)
{
t1->GetEntry(i);
if(var1>0 && var2==2) h1->Fill(Pt[0]) ; //fill with the first element of the array
}

TFile *f1 = new TFile(“file1.root”)
TTree f1 = (TTree)f1->Get(“tree”)
t1->SetBranchAddress(“var1”,&var1) ;
t1->SetBranchAddress(“var2”,&var2) ;
t1->SetBranchAddress(“Pt”,Pt) ;
TH1F *h2 = new TH1F(“h1”,“h2”,100,0,300) ;

Int_t nentries = (Int_t)t2->GetEntries();
for (Int_t i = 0 ; i<nentries ; i++)
{
t2->GetEntry(i);
if(var1>0 && var2==2) h2->Fill(Pt[0]) ; //fill with the first element of the array
}

TFile *f10 = new TFile(“file1.root”)
TTree f10 = (TTree)f10->Get(“tree”)
t10->SetBranchAddress(“var1”,&var1) ;
t10->SetBranchAddress(“var2”,&var2) ;
t10->SetBranchAddress(“Pt”,Pt) ;
TH1D *h10 = new TH1D(“h10”,“h10”,100,0,300) ;

Int_t nentries = (Int_t)t10->GetEntries();
for (Int_t i = 0 ; i<nentries ; i++)
{
t10->GetEntry(i);
if(var1>0 && var2==2) h10->Fill(Pt[0]) ; //fill with the first element of the array
}
TCanvas *c1 = new TCanvas(“c1”,“c1”,1)
THStack *hs = new THStack(“hs”,“hs”) ;
h1->Scale(fs) ;
h1->SetFillColor(2)
h2->Scale(fs) ;
h2->SetFillColor(3) ;


hs->Add(h1) ;
hs->Add(h2) ;



hs->Draw() ;
TLegend *leg = new TLegend(0.45,0.45,0.9,0.9)
leg->AddEntry(h1, “title1”, “f” );
leg->AddEntry(h2, “title2”, “f” );
leg->Draw() ;

}

The unbelievable thing is that the crash comes when I put 9 or more files to read, while everything works when adding a maximum of 8 files !!
Is there any explanation ?
PS: the root version I use is 5.18/00b.
Thanks in advance!

Manuel

Hi Manuel,

Maybe you are running out of resources if you do not close each TFile. Could a TChain be used in your case to simply the code? Did you try to compile the code? Did you try to run it with valgrind?

Philippe.

Hi Philippe,

thank you for replying. I modified my code just writing f1->Close(), f2->Close(), and so on, after each file was opened and read. But when executing it, I got “segmentation violation”.
About using a TChain, it could be a way, but I don’t know how to produce a histogram out of it.
Any suggestion?

Thanks,

Manuel

Hi Manuel,

TChain pretty much the same as a TTree (i.e. you code as is would work), re-read the User’s Guide chapter on TTree for more details.

[quote] modified my code just writing f1->Close(), f2->Close(), and so on,[/quote]Did you also add ‘delete f1’, etc.?

[quote] I got “segmentation violation”.[/quote]What kind? Which stack trace? Did you make sure the histogram are not attached to the file you are closing (and hence also deleted) (See the User’s Guide chapter on Object Ownership).

Cheers,
Philippe.

Hi Philippe,

I read the TChain section of the User’s Guide. Now, if I’m not wrong, after SetBranchAddress’ing the variable I want to read, I can create only one histogram from all the files added in the chain. Instead I want to create a histogram for each file I read, i.e. 10 different histograms in my case, and then working with them. That’s the main reason why I didn’t want to use a chain.

Returning to my code, I don’t delete each file, so I don’t think that histograms are attached to the files.

[quote] Instead I want to create a histogram for each file I read, i.e. 10 different histograms in my case, and then working with them. That’s the main reason why I didn’t want to use a chain.[/quote]Fair enough.

[quote]I don’t delete each file, so I don’t think that histograms are attached to the files.[/quote]I do not see the correlation between the 2 statements. In the contrary, in the code sample you provided, the histogram ‘h1’, ‘h2’, etc… are attached to their respective input file and are indeed deleted as soon as you would call f1->Close() (which implicit request the deletion of all the attached objects).

Cheers,
Philippe.

Hi,

in fact I hadn’t well understood that before. However, even eliminating all the calls “f1->Close() ;f2->Close() ;” etc, or putting them at the end of the code, the problem persists, unless I read a maximum of 8 files.
The message I get is :
*** glibc detected *** /usr/bin/root.exe: free(): invalid next size (fast): 0x08649a10 ***
followed by a list of backtrace and memory map.
I can’t understand why this can happen.

Hi Philippe,

I have solved my problem at last. I think it had nothing to do with the closing of flies and the resulting destruction of all objects attached to each of them, but it was related to the dimension of the array where I saved the variable.
However, I have to thank you very much for your precise suggestions.
Now I have a more general question, which has now arisen to me. Just suppose you open a file and then create an object (such as an histogram or anything else). Is there to save this object for later uses before closing the file, in order to avoid its deletion?
I’ll be clear with a sample code:

for (i = 0 ; i <n ; i++)
{
TFile f = new TFile(myfile.root) ;
TTree t = (TTree)f->Get(“MyTree”);


h1 = new TH1D(“h1”,“h1”,100,0.0,1000); // declaration of histogram
t->Draw("x>>h1) ;


/
some kind of working with h1 */

f->Close() ;
}

After the loop and the closing of the file, is there a way to keep on working with h1 and avoid its deletion?

Thanks.

Manuel.

Use for example:gROOT->cd(); // This will associate h1 with gROOT rather than a TFile. h1 = new TH1D("h1","h1",100,0.0,1000); // declaration of histogram t->Draw("x>>h1) ;

Cheers,
Philippe.

Thanks for your suggestion.

Cheers,

Manuel