Can someone explain what is happening here and how to fix it?


I would like to run a script to read a number of different files, the file determined by an argument. For some reason, selecting the file to read when within an if-then-else statement does not work. A simple example of a code snippet that doesn’t work is

#include "TH2.h"
#include "TBrowser.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TTree.h"
#include "TNtuple.h"
#include "TFile.h"
auto in_file = new TFile();
void simpletest(int narg){
  if(narg==1){
      TFile *in_file = new TFile("./level6/level6results200.root");
      }
      else if(narg==2){
      TFile *in_file = new TFile("./level6/level6results070.root");
      }
    cout << narg << endl;
    TTree *mydata = (TTree*)in_file->Get("Results");
}

Trying to run that code results in the following error:
root [0]
Processing simpletest.C(1)…
1
Error in TFile::Get: Unexpected type of TDirectoryFile::fKeys!

But if the if-then-else code is commented out and only a single file is read, then the code works as desired.

Can anyone explain what is happening and suggest a way to get the selection process working?

ROOT Version: 6.26.10
Platform: Ubuntu 22
Compiler: Not Provided


Uncle Google → C++ variable scope

Many thanks Wile.