I have a problem in a large piece of code, but I could track it down to the following example:
[code]#include
#include <TTree.h>
using namespace std ;
struct resultsStruct
{
double aDouble ;
int anInt ;
double anotherDouble ;
} ;
void testStruct()
{
struct resultsStruct results ;
results.aDouble = 1.2 ;
results.anInt = 3 ;
results.anotherDouble = 4.5 ;
TTree * treeResults = NULL ;
treeResults = new TTree(“treeResults”, “treeResults” ) ;
treeResults->Branch(“branchResults”, &results, “aDouble/D:anInt/I:anotherDouble/D”) ;
treeResults->Fill() ;
TFile * file = new TFile(“test.root”, “RECREATE”) ;
treeResults->Write() ;
file->Close() ;
}[/code]
If I read back the file and do a scan (or fill a histogram) I get :
[quote]$ root -l test.root
root [0]
Attaching file test.root as _file0…
root [1] treeResults.Scan()
- Row * aDouble * anInt * anotherDo *
-
0 * 1.2 * 3 * 0 *
[/quote]
The last value (the value of anotherDouble) is now zero! Actually, in my larger code the last value of the tree is a random number. If I invert the order of the variables in the struct declaration (and in the branch as well), placing the interger value as the last one, everything looks ok. This is wierd, but so simple that I’m obviously missing something…
I attach a working example just for convenience:
[code]#include
#include <TTree.h>
using namespace std ;
struct resultsStruct
{
double aDouble ;
double anotherDouble ;
int anInt ;
} ;
void testStruct()
{
struct resultsStruct results ;
results.aDouble = 1.2 ;
results.anotherDouble = 4.5 ;
results.anInt = 3 ;
TTree * treeResults = NULL ;
treeResults = new TTree(“treeResults”, “treeResults” ) ;
treeResults->Branch(“branchResults”, &results, “aDouble/D:anotherDouble/D:anInt/I”) ;
treeResults->Fill() ;
TFile * file = new TFile(“test.root”, “RECREATE”) ;
treeResults->Write() ;
file->Close() ;
}
[/code]
This time the result appears to be correct!
[quote]$ root -l test.root
root [0]
Attaching file test.root as _file0…
root [1] treeResults.Scan()
- Row * aDouble * anotherDo * anInt *
-
0 * 1.2 * 4.5 * 3 *
[/quote]
Amazed: thanks for any reply.
Dario