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
@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”.
{
// 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.
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.