Memory resistent problem with TTree

Hi rooters !
I have a problem concerning a memory-resistent tree.

Here is my code

TH1F *henergy_sum(string process, double xmin, double xmax, int nbins){

  string file_path = "$WORKDIR/Simus_Lyon/Sensitivity/"+process+string("/with_B")+string("/root_file_")+process+string("_");
  string file_extension = ".root";
  string file_path_new;
  string file_adress;
  string tree_adress;
  TList *list = new TList;

  string file_path_Canvas1 = "plots/energy_sum_"+process+".eps";
  const char* c_file_path_Canvas1 = file_path_Canvas1.c_str();

  ///Loop on all .root output files
  for (int i = 0; i < 10; ++i) {
    stringstream ss;
    ss << i;
    string str = ss.str();
    file_path_new = file_path+str+file_extension;
    file_adress = string("f")+str;
    tree_adress = string("tree")+str;
    const char* c_file_path_new = file_path_new.c_str();
    TFile *file_events = TFile::Open(c_file_path_new);
    if (file_events->IsOpen()) {
      cout << "file opened :)" << endl;
    }
    TTree *tree_events = (TTree*)file_events->Get("calorimeter_hit");
    list->Add(tree_events);

    // tree_events->SetDirectory(0);
  }



  ///Merge all trees in "newtree"
  TTree *newtree = TTree::MergeTrees(list);
  int event_counter;
  Double_t time_difference_E;
  Double_t energy_sum;
  Double_t time_Emin;
  Double_t time_Emax;
  Double_t probability;
  Double_t length_Emin;
  Double_t length_Emax;
  Double_t minimal_energy;
  Double_t maximal_energy;
  Double_t sigma_time_Emin;
  Double_t sigma_time_Emax;

  newtree->SetBranchAddress("event_counter",&event_counter);
  newtree->SetBranchAddress("time_difference_E",&time_difference_E);
  newtree->SetBranchAddress("time_Emin",&time_Emin);
  newtree->SetBranchAddress("time_Emax",&time_Emax);
  newtree->SetBranchAddress("probability",&probability);
  newtree->SetBranchAddress("energy_sum",&energy_sum);
  newtree->SetBranchAddress("length_Emin",&length_Emin);
  newtree->SetBranchAddress("length_Emax",&length_Emax);
  newtree->SetBranchAddress("minimal_energy",&minimal_energy);
  newtree->SetBranchAddress("maximal_energy",&maximal_energy);
  newtree->SetBranchAddress("sigma_time_Emin",&sigma_time_Emin);
  newtree->SetBranchAddress("sigma_time_Emax",&sigma_time_Emax);


  ///Create histograms to be filled with reconstructed data outputs
  Long64_t nentries = newtree->GetEntries();

  int nbr_entries = 0;

  TH1F *henergy_sum = new TH1F("energy_sum","Energy sum histogram",nbins,xmin,xmax);

  for (Long64_t i=0;i<newtree->GetEntries();i++) {
    newtree->GetEntry(i);
    henergy_sum->Fill(energy_sum);
    nbr_entries++;
  }

  return henergy_sum;
}

void efficiency(){

  TH1F *energy_spectrum_0nubb = henergy_sum("0nubb",0,4,100);

  // ///Drawing
  TCanvas *c1 = new TCanvas("canvas","canvas");
  gStyle->SetOptStat(kFALSE);
  gPad->SetLogy();
  energy_spectrum_0nubb->Draw();
  
}

If the line tree_events->SetDirectory(0); is commented, I have this error when compiling :

Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.length_Emin, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.length_Emax, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.energy_sum, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.minimal_energy, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.maximal_energy, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.sigma_time_Emin, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.sigma_time_Emax, nbytes=-1, entry=2859396
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.time_Emin, nbytes=-1, entry=2860113
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:
    TTree *T = new TTree(...)
    TFile *f = new TFile(...)
 you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)
Error in <TTree::Fill>: Failed filling branch:calorimeter_hit.time_Emax, nbytes=-1, entry=2860113
 This error is symptomatic of a Tree created as a memory-resident Tree
 Instead of doing:
    TTree *T = new TTree(...)
    TFile *f = new TFile(...)
 you should do:
    TFile *f = new TFile(...)
    TTree *T = new TTree(...)

And if I uncomment this line, this error no longer appears but I have no date stored in my histogram energy_spectrum_0nubb.

Can someone help me ?
Thank you,
Cloé


ROOT Version: 6.08/06
Platform: Ubuntu 16.04
Compiler: gcc 5.4.0


Hi

You may need:

TFile outputFile = TFile::Open("outputfile.root","RECREATE");
TTree *newtree = TTree::MergeTrees(list);

Cheers,
Philippe.

Thank you very much, it worked :slight_smile:
Could you explain me why ? For the next time
Cloé

Creating an output TFile allow the TTree to be written to disk as it accumulates data (and the written data is dropped from memory) Without the TFile, the TTree is memory resident and the data accumulate in RAM (take more and more in memory) until you run out of memory …

Cheers,
Philippe.

Thank you very much,
Cloé

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