Problems with TFile

Dear Rene and other root developers,
I have this problem:
reading.C is a simple macro that wants to combine together automatically some histograms, saved in different .root files.

The list of root file to read is also added: histofile.txt

Into another file is written what are the histograms to combine.
(because the limit of 3 files to upload, i didn’t upload it, but in this case
is a simple on line file: hgaus (the example histogram tha i have created))

The macro reads the input files, avoids what starts with // and tries to print the contnent of the files loaded.
At this point root ends with this message:

So, what is my fault?

If a create one by one TFile It works, but if I do this, I lose the possibility
of a dynamic macro.

I hope to have been clear in my exposistion

Best Regards

Paolo

p.s. my root version is 5.14/00e, CINT 5.16.16 on an SLC 4 machine
p.p.s. you can clone histo1.root to obtain histo2.root histo3.root, i was blocked to upload them because 3 file limit
histofile.txt (41 Bytes)
reading.C (2.56 KB)
histo1.root (5.48 KB)

Hi,

The problem is that you define: //Local variables TFile *f_histos2; f_histos2 = new TFile[nomi_histofile.size()];as a single object, but use it as an array: f_histos2[file_loop].Print(); f_histos2[file_loop].GetListOfKeys()->Print();

Cheers,
Philippe

Thank you for your help, but I don’t know how to use an array of
TFiles in this istuation.
An suggestion to correct it?

Hi,

You could reorganize your code so that you only need one file open at a time or you could use a TObjArray or a TList or your favorite STL containers.

Philippe

Dear Phileippe, I have done it !
But now I have antoher problem!
I don’t understand why, if I create a “good” TFile array,
when I open a file it gives me no stats and when i do

 f_histos2[file_loop].GetListOfKeys()->Print();

It hangs.

Here the full code:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include "TNtuple.h"
#include "TFile.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TTree.h"
#include "TChain.h"
#include "TCanvas.h"
#include "TGraph.h"
#include "TGraphErrors.h"
#include "TF1.h"
#include "TStyle.h"

void Comb_HISTO(char *histofile,char* histotodraw){

  char char_buffer[150];
 
  vector <string> nomi_file;
  vector <string> nomi_histo;

  FILE *histo_file;
  histo_file = fopen(histofile,"r");
  FILE *histo_to_draw;
  histo_to_draw = fopen(histotodraw,"r");


  //REad list of root files
  while ( fscanf(histo_file,"%s\n",char_buffer) > -1){
    string dummystring;
    dummystring = char_buffer;
    if(dummystring.substr(0,2) == "//" ) continue;
    cout << "File input: " << dummystring << endl;
    nomi_file.push_back(char_buffer);
  }
  
  if (nomi_file.size() == 0) {
    cout << "!! ERROR NO USABLE FILES" << endl;
    return;
  }


  //read list of histo to combine together
  while ( fscanf(histo_to_draw,"%s\n",char_buffer) > -1){
    string dummystring;
    dummystring = char_buffer;
    if(dummystring.substr(0,2) == "//" ) continue;
    cout << "Histo to draw: " << dummystring << endl;
    nomi_histo.push_back(char_buffer);
  }
  
  if (nomi_histo.size() == 0) {
    cout << "!! ERROR NO USABLE HISTO" << endl;
    return;
  }

  
  //Local variables
  vector <string>::iterator histo_iter;


  //TFile *f_histos;
  size_t file_array_size(nomi_file.size());
  TFile *f_histos2 = new TFile[file_array_size];

  TH1F *histos;
  histos = new TH1F[nomi_histo.size()];

  vector <string>::iterator file_iter;
  int file_loop=0;
  
  for(file_iter=nomi_file.begin();file_iter!=nomi_file.end();file_iter++){
    cout << *file_iter << endl;
    char dummy_name[((*file_iter).size())+1];
    sprintf(dummy_name,"%s",(*file_iter).c_str());
    cout << dummy_name << endl;
    f_histos2[file_loop].Open( (*file_iter).c_str(),"READ");
    f_histos2[file_loop].Print();
    f_histos2[file_loop].GetListOfKeys()->Print();
    cout << "      ####   " << endl << endl;;
    file_loop++;
  }

}

It seems that it doesn’t like an array of Tfiles

Thanks for your help and patience!

Instead of TFile *f_histos2 = new TFile[file_array_size]; .... f_histos2[file_loop].Open( (*file_iter).c_str(),"READ");
UseTFile **f_histos2 = new TFile*[file_array_size]; .... f_histos2[file_loop] = TFile::Open( (*file_iter).c_str(),"READ");

Philippe.