Best way to save "long double" variables into a TTree

I have a lot of “long double” data variables which I want to save into a TTree, but the “long double” datatype is not supported in ROOT. Currently I am doing something like the following:

long double var;
double var_copy;
tree->Branch("varname", &var_copy);

// value of 'var' is then generated by another part of the code

var_copy = var;
tree->Fill();

but I would prefer not to have to save extra “double versions” of all my data. Replacing

tree->Branch("varname", &var_copy);

with

tree->Branch("varname", reinterpret_cast<double*>(&var));

doesn’t work, as the resulting TTree just contains zero-valued entries. Is there an alternative method?

(N.B. CPU and especially memory is very constrained, so performance is important.)

Thanks for any advice!


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


@pcanal Indeed, when one tries to create a branch for a “long double” variable, one gets: “Error in <TTree::Branch>: The pointer specified for varname is not of a class or type known to ROOT”.

@ed20 You can try the following “brutal fix” (note that the tree will depend on the exact machine’s layout of the “long double” so, you may get problems when reading it back on another computer architecture / operating system / compiler):

{
  // create and fill the tree
  TTree *tree = new TTree("tree", "tree");
  long double var;
  tree->Branch("varname", &var, // reinterpret_cast<UChar_t*>(&var)
               TString::Format("var[%ld]/b", sizeof(long double)));
  for (Long64_t i = 0; i < 10; i++) { var = -i * i; tree->Fill(); }
  tree->Print();
  tree->ResetBranchAddresses(); // disconnect from local variables
  // "read" the tree
  Long64_t n = tree->GetEntries();
  long double v;
  tree->SetBranchAddress("varname", &v); // reinterpret_cast<UChar_t*>(&v)
  for (Long64_t i = 0; i < n; i++) {
    tree->GetEntry(i);
    std::cout << i << " ... " << v << std::endl;
  }
  tree->ResetBranchAddresses(); // disconnect from local variables
  // cleanup
  delete tree;
}

thanks, this could potentially work. However, the code will be shared between a few people on different operating systems and also run on external computing resources, and I do not yet know the details of the architectures of all of the machines. Is there a way to adapt this so that the “double version” of the variable is saved to Root instead? Long double precision is needed when generating the values, but for saving it would be OK (actually preferable in terms of storage) to only save the equivalent of (double)var.

It seems to me that the best idea would be to use the “double var_copy;” approach (as you do in your first post above).

Just to confirm that indeed we currently do not support storing long double both because the last time I got a chance to really look into it it was not really portable and because it (likely) a waste of storage for most use case.