NaN unless I print it out?

Hi Rooters,

I have here a script that counts some doubles.

double count_passed_events_v2(TString file_prefix, int num_jobs){

  TChain *my_chain = new TChain("passTree");

  for(int i=1; i<=num_jobs; i++){

    TString file_name ("");
    file_name.Form(file_prefix + "/run%d/IceFinal_%d_passTree0.root", i ,i);

    my_chain->Add(file_name); 
  }
  
  TTreeReader reader(my_chain);

  TTreeReaderValue<double_t> w1(reader, "event.nicemc::EventSummary.neutrino.path.weight");
  TTreeReaderValue<double_t> w2(reader, "event.nicemc::EventSummary.loop.positionWeight");
  TTreeReaderValue<double_t> w3(reader, "event.nicemc::EventSummary.loop.directionWeight");

  double weighted_passed_event_count;
  while (reader.Next()) {
    weighted_passed_event_count += *w1/(*w2 * *w3); 
  }

  
  delete my_chain;
  return weighted_passed_event_count;
}

As it is, it does not work. I’ve found that it only works when I also tell the script to print out something, anything. For example, if I add
std::cout << "done with " << file_prefix << std::endl;
then the macro runs just fine.

Could someone please tell me why this is the case?
I am fairly new to ROOT and C++ in general, and I don’t really know what to even search for when I google this problem.

ROOT Version: 6.30/02
Platform: Fedora
Compiler: Build for linuxx8664gcc

Hi,

you probably have an undefined behaviour somewhere. Meaning that anything may happen, even your script appearing to be working with cout added.

Hi yus,

Thanks for the help!
Could you give me some examples of undefined behavior? I am not familiar with the concept.

This article explains the concept of UB:
https://en.cppreference.com/w/cpp/language/ub

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.