Anybody understands this?

This is driving me crazy. I tried to reproduce it in a simplescript. You will need the file to run this, i copied it at /afs/cern.ch/user/m/marios/public.
The second script which is the one i want to use with more than 2 iterations obviously, throughs a segmentation error when i run it with .x from rint. In the first script I am reproducing what the loop was supposed to do. The problem on the second iteration is when h->Fit is invoked.
I am working on lxplus , My ROOTSYS is
/afs/cern.ch/sw/lcg/app/releases/ROOT/5.22.00b/slc4_ia32_gcc34/root/

THIS WORKS

[code]{
TFile nf(“newFile”);
TTree * t = (TTree*) nf.Get(“newTree”);
TFile tmp(“tmpFile”,“RECREATE”);
TString stripCut;
TF1 *f1 = new TF1(“f1”,"[0]*x+[1]",0,200);

//for(int strip=1;strip<3;strip++){

int strip=1;
stripCut=Form(“stripH==%d && stripL==%d”,strip,strip);
t->Draw(“ymaxL:ymaxH>>h(200,0,200,60,0,60)”,stripCut && “ymaxH/20 + 3 > ymaxL && ymaxH/20 -3 <ymaxL”);
h->Fit(“f1”,"","",10,200);
h->Write();
strip++;

stripCut=Form("stripH==%d && stripL==%d",strip,strip);
t->Draw("ymaxL:ymaxH>>h(200,0,200,60,0,60)",stripCut && "ymaxH/20 + 3 > ymaxL && ymaxH/20 -3 <ymaxL");
h->Fit("f1","","",10,200);
h->Write();

tmp.Close();
}[/code]

THIS WILL NOT WORK

[code]{
TFile nf(“newFile”);
TTree * t = (TTree*) nf.Get(“newTree”);
TFile tmp(“tmpFile”,“RECREATE”);
TString stripCut;
TF1 *f1 = new TF1(“f1”,"[0]*x+[1]",0,200);

//for(int strip=1;strip<3;strip++){

int strip=1;
while(strip<3){

stripCut=Form("stripH==%d && stripL==%d",strip,strip);
t->Draw("ymaxL:ymaxH>>h(200,0,200,60,0,60)",stripCut && "ymaxH/20 + 3 > ymaxL && ymaxH/20 -3 <ymaxL");
h->Fit("f1","","",10,200);
h->Write();
strip++;
}

tmp.Close();
}[/code]

see the changes marked with “<=====” in your code below

Rene

[code]{
TFile nf(“newFile.root”);
TTree * t = (TTree*) nf.Get(“newTree”);
TFile tmp(“tmpFile”,“RECREATE”);
TString stripCut;
TF1 *f1 = new TF1(“f1”,"[0]*x+[1]",0,200);

//for(int strip=1;strip<3;strip++){

int strip=1;
while(strip<3){

stripCut=Form(“stripH==%d && stripL==%d”,strip,strip);
t->Draw(“ymaxL:ymaxH>>h(200,0,200,60,0,60)”,stripCut && “ymaxH/20 + 3 > ymaxL && ymaxH/20 -3 <ymaxL”);
TH1 h = (TH1)tmp.Get(“h”); //<=========
h->Fit(“f1”,"","",10,200);
h->Write();
strip++;
}

//tmp.Close(); //<=========
}
[/code]

Many thanks Rene. Could you explain why the first case was working and the second was not?

In your first implementation, the pointer h was automatically created by CINT by taking the object named “h” in the current directory. However, this object was deleted in the second iteration by tree.Draw and h was still pointing to the deleted object.

Rene