mzriu
November 23, 2021, 1:20pm
1
ROOT Version : 6.24/06
Hello guys,
I am having a problem which is pretty weird. Here is my code :
Int_t ind_run = 0;
if(ind_run==0){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-12micrA-sansRED_0001_single_out.root";
} else if(ind_run == 1){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN1_0001_single_out.root";
} else if(ind_run == 2){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN2_0001_single_out.root";
} else if(ind_run == 3){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN139_0001_single_out.root";
} else if(ind_run == 4){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN140_0001_single_out.root";
} else if(ind_run == 5){
TString path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-beamchopper-RUN144_0001_single_out.root";
}
TFile *f = new TFile(path);
My problem is whatever value I set for my ind_run variable, I get the error message "use of undeclared identifier ‘path’ ". It seems like my code won’t go inside the if or else if statements and so my TString path is not declared. But why it won’t do that is a mystery to me…
Hope you guys have a solution !
Best,
Marius
yus
November 23, 2021, 1:27pm
2
You declare your TString path
inside the if
or else if
statements. It only survives until the next closing curly bracket. You should change it to:
TString path = "";
Int_t ind_run = 0;
if(ind_run==0)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-12micrA-sansRED_0001_single_out.root";
else if(ind_run == 1)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN1_0001_single_out.root";
else if(ind_run == 2)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-10uA-sansRED-RUN2_0001_single_out.root";
else if(ind_run == 3)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN139_0001_single_out.root";
else if(ind_run == 4)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-Nobeamchopper-RUN140_0001_single_out.root";
else if(ind_run == 5)
path = "/home/marius/Documents/Thèse/E819S/Copie/Histogrammes/single/8He-beamchopper-RUN144_0001_single_out.root";
TFile *f = new TFile(path);
1 Like
mzriu
November 23, 2021, 1:31pm
3
Hi thanks for your response !
It works nicely now, but I don’t really understand why the variable only survives inside the if statement. Do you have an explanation for that ?
Best,
Marius
yus
November 23, 2021, 1:43pm
4
Check this out: Scope of Variables in C++ - GeeksforGeeks
Variables defined within a function or block are said to be local to those functions.
…
Anything between ‘{‘ and ‘}’ is said to inside a block.
Local variables do not exist outside the block in which they are declared, i.e. they can not be accessed or used outside that block.
1 Like