TChain problem

Hi Everyone,

I am trying to read root file paths from a text file and add them to a TChain. I have the following code


TString treename = "truth";
TChain *nt = new TChain(treename);


std::string filename = "input.txt";
std::ifstream inputfilenamestream( filename.c_str() );


if (!inputfilenamestream.is_open()) {
     std::cout<<"Sorry, inputfilenamestream could not be opened!" << std::endl;
      exit(1);
}else{

      std::string rootfile;
      while (getline(inputfilenamestream,rootfile)){
        TString rootfile2(rootfile);
        nt->Add(rootfile2);
      }
   }

But this crashes when I try to do GetEntry(i) in my event loop. The problem is not my event loop because I replaced the above code with the following

    TString infilename;
    infilename = argv[1];
    nt->Add(infilename);
    infilename = argv[2];
    nt->Add(infilename);

and supply the same paths that are in the .txt file as command line arguaments and my code runs perfectly. I am not sure what I am doing differently between the two as in both I am suppling a TString to TChain::Add.

Can anyone spot my mistake?

Thanks,
Paul

p.s. I am using root version 5.34.18-x86_64-slc6-gcc4.7 from ATLASLocalRootBase

Hi,

it is hard to understand what it’s happening without a reproducer.
Did you check that the paths you read in are correct (eg with a simple printout)?

Danilo

Hi,

I tried to make a small piece of code that can reproduce the problem. The following crashes if I SetBranchAddress for two branches but if I set either one it runs fine.

I checked that the file paths are correct.


#include "TChain.h"
#include "TTree.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

nt main(int argc, char* argv[]){

    TString treename = "truth";
    TChain *nt = new TChain(treename);

    std::string filename = "input.txt";
    std::ifstream inputfilenamestream( filename.c_str() );


    if (!inputfilenamestream.is_open()) {
      std::cout<<"Sorry, inputfilenamestream could not be opened!" << std::endl;
      //exit(1);
    }else{

      std::string rootfile;
      while (getline(inputfilenamestream,rootfile)){
        TString rootfile2(rootfile);
        nt->Add(rootfile2);
      }
   }
TBranch *b_top_hfor_type=0;   //!
    TBranch *b_top_hfor_Quarks_px=0; //!
    int top_hfor_type=0;
    std::vector<float> *top_hfor_Quarks_px;


    nt->SetBranchAddress("top_hfor_type", &top_hfor_type, &b_top_hfor_type);
    nt->SetBranchAddress("top_hfor_Quarks_px", &top_hfor_Quarks_px, &b_top_hfor_Quarks_px);

   for(int i=0;i < (int)nt->GetEntries() ; i++){  //loop not needed?

       Long64_t ientry = nt->LoadTree(i);
       if (ientry<0)break;
       std::cout << "Will I crash?" << ientry << std::endl;
       int nbytes = nt->GetEntry(i);
       std::cout << "I did not crash. "<< std::endl;
       if(nbytes==0){ std::cout << "failed to get entry" << std::endl; continue;}
   }

    return 0;
}

Hi,

still I am afraid this is not enough as it is not a complete reproducer.
Did you try to see if the paths to the files are correct when you read them from the txt file?

D

Hi,

Yep. I printed the paths read in to the screen and then checked that they were correct. What else do you need to be able to reproduce the problem?

Thanks,
P

Hi,

the txt file would be a good start. Mind, at this point I start doubting that this is a root issue :frowning:

Cheers,
Danilo

I attached the text file.

Thanks,
P
input.txt (512 Bytes)

Hi,

I could have a look to a dry run of your program (dry because I cannot access the actual root files :frowning: ).
The names look sane and I tend to exclude a root bug here.
Said that, to recap: you can run if you add one by one the files to the chain, say hard coding the names in the program or passing those via command line but not when reading them through the txt file and the filenames look identical.
If yes, I think this looks like an issue solvable with some printouts and a cup of coffee…

Cheers,
Danilo

Either:
std::vector *top_hfor_Quarks_px = 0;
or:
std::vector *top_hfor_Quarks_px = new std::vector;