Adding ProjectionX of histograms

I am trying to add X projections of histograms (TH2F) , but currently I am failing at it. I got lots of files (xxxx.root, xxxx=9423…9754) and in each file 24 histograms called N_xx (xx=01…24). I want to add all the histograms of these files and save them in a file.

[code]{
Char_t filename[20];
Char_t detectorname[20];
TH2D *h24[25];
TH1F *h;

for(Int_t i=9423; i<9755; ++i) { // Loop all root files
Int_t createfilename = sprintf(filename,"%u.root",i); // Creating filename: xxxxx.root
if(FileExists(filename)!=0) { // Check if file exists (FileExists defined elsewhere)
TFile *f = new TFile(filename);

   for(Int_t n=1; n<25; ++n) {
	Int_t createdetname = sprintf(detectorname,"N_%02u",n); // Creating histogramname: N_xx
h24[n]              = (TH2D*)f->Get(detectorname); // Fill h24 with histograms
   TProfile *profile   = h24[n]->ProfileX();
   h->Add(profile);
   delete profile;
}
}

}
// Rest of the program
}[/code]

I get this error: Error: illegal pointer to class object h 0x0 397

TProfile != TProjection

[code]{
Char_t filename[20];
Char_t detectorname[20];
TH2D *h2;
TProfile *profile = 0;

for(Int_t i=9423; i<9755; ++i) { // Loop all root files
Int_t createfilename = sprintf(filename,"%u.root",i); // Creating filename: xxxxx.root
if(FileExists(filename)!=0) { // Check if file exists (FileExists defined elsewhere)
TFile *f = TFile::Open(filename);

  for(Int_t n=1; n<25; ++n) {
    Int_t createdetname = sprintf(detectorname,"N_%02u",n); // Creating histogramname: N_xx
    h2 = (TH2D*)f->Get(detectorname); // Get pointer to current histogram
    if (!profile) { // only once
      profile = h2->ProfileX();
      profile->SetDirectory(0);
    }
    else profile->Add(h2->ProfileX());
  }
  delete f;
}

}
// Rest of the program
}
[/code]I hope this help,
Jan

I think that helps a lot, thank you.

It works, but if I run the program multiple times i get the following error

Error: Symbol G__exception is not defined in current scope histo.C:28: Error: type G__exception not defined FILE:/../histo.C LINE:28

Line 28 is the line that says: h2 = (TH2D*)f->Get(detectorname);

I don’t understand the errormessage.

try add this line (before line 28 ):if (!f->FindObject(detectorname)) continue; or post your script histo.C (which can execute)

Jan

I see that that will fix this problem, but I find it weird that the program will run the first two times without errors.