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

**URL:** <https://root-forum.cern.ch/t/can-someone-explain-what-is-happening-here-and-how-to-fix-it/53676>\
**Category:** Newbie\
**Created:** [February 18, 2023, 11:15pm UTC](https://root-forum.cern.ch/t/can-someone-explain-what-is-happening-here-and-how-to-fix-it/53676 "2023-02-18T23:15:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mbl1230](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@mbl1230](https://root-forum.cern.ch/u/mbl1230)\
**Post date:** [February 18, 2023, 11:15pm UTC](https://root-forum.cern.ch/t/can-someone-explain-what-is-happening-here-and-how-to-fix-it/53676/1 "2023-02-18T23:15:06Z")

</div>

* * *

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

```auto
#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

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 19, 2023, 12:12am UTC](https://root-forum.cern.ch/t/can-someone-explain-what-is-happening-here-and-how-to-fix-it/53676/2 "2023-02-19T00:12:12Z")

</div>

[Uncle Google → C++ variable scope](https://www.google.com/search?q=C%2B%2B+variable+scope)

---

<div class="post-metadata">

**Author:** ![mbl1230](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/ba8739/32.png) [@mbl1230](https://root-forum.cern.ch/u/mbl1230)\
**Post date:** [February 19, 2023, 4:41am UTC](https://root-forum.cern.ch/t/can-someone-explain-what-is-happening-here-and-how-to-fix-it/53676/3 "2023-02-19T04:41:37Z")

</div>

Many thanks Wile.
