Error: illegal type cast (2) using ROOT macro

Dear ROOT forum,

I am running a piece of Root macro to help me set limits using HypoTestInverter Class. My code is structured like so:

struct results{
float lLimit_1s_fc;
float lLimit_2s_fc;
float uLimit_1s_fc;
float uLimit_2s_fc;
float median_fc;
float observed95ulimit_fc;
float observed95_ulimit_error;
} current_bin;

void upper_tut4(int bin_number, int mult_slice){

struct results current_bin=upperlimit_tut1(obs,bk, syserror, bin_number, mult_slice);


struct results upperlimit_tut1(float nObs, float bVal, float bk_sys,int input_multi, int input_bin){

[code to get the limits based on inputs to function…]

struct results bin={band1sigDown,band2sigDown,band1sigUp,band2sigUp,bandMedian,upperlimit95,upperlimit95_error};

return bin;

However I find that when the code get to the line that returns the bin I get the error message:

Error: illegal type cast (2) upper_tut4.C:668:
*** Interpreter error recovered *

I can’t figure out what the problem is. The strangest thing is that this error only occurs sometimes. To be specific I am running HypoTestInverter to give me confidence intervals and it only occurs if I rebuild the toy intervals using something like:

  SamplingDistribution * limDist = calc.GetUpperLimitDistribution(true,mNToyToRebuild);

Therefore it is not easy for me to duplicate the error as rebuilding toys is a very time consuming process. If I rebuild only 1 or 2 toys (setting mNToyToRebuild=2) this error does not occur it only occurs if I set NToyToRebuild to 20 or greater).

Does anyone have ideas as to what’s going on?

Thank You,



I would recommend that as a first debugging step, you try compiling your script with ACLiC. I.e. replace .L script.C with .L script.C+ (which may require that you update your script to correct any C++ syntax error that CINT might have been lenient on). If you still see problem, I then recommend that you run your example using valgrind ( by running valgrind root.exe … )


Same type of Pb
with the following function

66 TString TakeLastElement(TString tmpfile, char separator){
68 cout << “tmpfile dans TakeLastElement de ReadSimu :”<< tmpfile << std::endl;
69 TString laststring ;
70 char namelastelement[100];
71 TObjArray
Strings = tmpfile.Tokenize(separator);
72 int i =0;
73 if(Strings->GetEntriesFast())
74 {
75 //if some strings exist
76 TIter iString(Strings);//iteration of the strings
77 TObjString* os = 0;
78 while ((os=(TObjString*)iString()))
79 {
80 cout << "i : "<< i <<std::endl;
81 laststring = os->GetString().Data();
82 cout << "laststring : " << laststring << std::endl;
83 i++;
84 }
86 }
87 delete Strings;
88 return laststring;
89 }

tmpfile dans TakeLastElement de ReadSimu :/CADUX-DESTO/SDOS/BINTU201/ILAIM/DataTestTV/Analogique_Numerique_202111/15MeV/BU_NUM/gamma/Data_CH2@DT5730_1662_20211117-207Bi_BU_soir_2.csv
i : 0
laststring : CADUX-DESTO
i : 1
laststring : SDOS
i : 2
laststring : BINTU201
i : 3
laststring : ILAIM
i : 4
laststring : DataTestTV
i : 5
laststring : Analogique_Numerique_202111
i : 6
laststring : 15MeV
i : 7
laststring : BU_NUM
i : 8
laststring : gamma
i : 9
laststring : Data_CH2@DT5730_1662_20211117-207Bi_BU_soir_2.csv
Error: illegal type cast (2) …/…/MODULESCOMMUNS9.8.5/ReadSimuAnalysisFile.C:87:

note that this error seems to be randomly triggered
I tried to lanch my program with valgrind

Pb Illegal Cast .txt (19.2 KB)
See this file for detail of execution with valgrind
Best Regards