Problem to read a TTree

Dear all,

I am having some trouble to read the contents of a TTree. I have a simple TTree with several branches (arrays) as follows,

TTree* tree = new TTree(“ntp1”, “Hits”);
tree->SetAutoSave(100000000);

tree->Branch(“nHits”,&nHits,“nHits/I”);
tree->Branch(“HitTrackID”, HitTrackID, “HitTrackID[nHits]/I”);
tree->Branch(“HitTrackPDGCode”, HitTrackPDGCode, “HitTrackPDGCode[nHits]/I”);
tree->Branch(“HitTrackVertexPosX”, HitTrackVertexPosX, “HitTrackVertexPosX[nHits]/D”);
tree->Branch(“HitTrackVertexPosY”, HitTrackVertexPosY, “HitTrackVertexPosY[nHits]/D”);
tree->Branch(“HitTrackVertexPosZ”, HitTrackVertexPosZ, “HitTrackVertexPosZ[nHits]/D”);
tree->Branch(“HitMimosaSensorID”, HitMimosaSensorID, “HitMimosaSensorID[nHits]/I”);
tree->Branch(“HitEdep”, HitEdep, “HitEdep[nHits]/D”);
tree->Branch(“HitPosX”, HitPosX, “HitPosX[nHits]/D”);
tree->Branch(“HitPosY”, HitPosY, “HitPosY[nHits]/D”);
tree->Branch(“HitPosZ”, HitPosZ, “HitPosZ[nHits]/D”);
tree->Branch(“HitStepLengthX”, HitStepLengthX, “HitStepLengthX[nHits]/D”);
tree->Branch(“HitStepLengthY”, HitStepLengthY, “HitStepLengthY[nHits]/D”);
tree->Branch(“HitStepLengthZ”, HitStepLengthZ, “HitStepLengthZ[nHits]/D”);
tree->Branch(“HitFirstStepPosX”, HitFirstStepPosX, “HitFirstStepPosX[nHits]/D”);
tree->Branch(“HitFirstStepPosY”, HitFirstStepPosY, “HitFirstStepPosY[nHits]/D”);
tree->Branch(“HitFirstStepPosZ”, HitFirstStepPosZ, “HitFirstStepPosZ[nHits]/D”);
tree->Branch(“HitLastStepPosX”, HitLastStepPosX, “HitLastStepPosX[nHits]/D”);
tree->Branch(“HitLastStepPosY”, HitLastStepPosY, “HitLastStepPosY[nHits]/D”);
tree->Branch(“HitLastStepPosZ”, HitLastStepPosZ, “HitLastStepPosZ[nHits]/D”);
tree->Branch(“HitFirstStep4VectorPx”, HitFirstStep4VectorPx, “HitFirstStep4VectorPx[nHits]/D”);
tree->Branch(“HitFirstStep4VectorPy”, HitFirstStep4VectorPy, “HitFirstStep4VectorPy[nHits]/D”);
tree->Branch(“HitFirstStep4VectorPz”, HitFirstStep4VectorPz, “HitFirstStep4VectorPz[nHits]/D”);
tree->Branch(“HitFirstStep4VectorE”, HitFirstStep4VectorE, “HitFirstStep4VectorE[nHits]/D”);
tree->Branch(“HitLastStep4VectorPx”, HitLastStep4VectorPx, “HitLastStep4VectorPx[nHits]/D”);
tree->Branch(“HitLastStep4VectorPy”, HitLastStep4VectorPy, “HitLastStep4VectorPy[nHits]/D”);
tree->Branch(“HitLastStep4VectorPz”, HitLastStep4VectorPz, “HitLastStep4VectorPz[nHits]/D”);
tree->Branch(“HitLastStep4VectorE”, HitLastStep4VectorE, “HitLastStep4VectorE[nHits]/D”);

I manage to write this TTree into a file (filename). I browsed the contents of the TTree and they seems to be OK.

I then load the tree in another script as follows,
TTree* tree = NULL;
TFile fdata(filename);
tree = (TTree*)fdata.Get(“ntp1”);
tree->SetDirectory(0);
fdata.Close();

When I do tree->Print(); I correctly get,


*Tree :ntp1 : Hits *
*Entries : 1000000 : Total = 1300728787 bytes File Size = 946489938 *

  •    :          : Tree compression factor =   1.37                       *
    

*Br 0 :nHits : nHits/I *
*Entries : 1000000 : Total Size= 4004986 bytes File Size = 499229 *
*Baskets : 51 : Basket Size= 275456 bytes Compression= 8.02 *

*Br 1 :HitTrackID : HitTrackID[nHits]/I *
*Entries : 1000000 : Total Size= 27311579 bytes File Size = 2432059 *
*Baskets : 321 : Basket Size= 1881088 bytes Compression= 11.23 *

*Br 2 :HitTrackPDGCode : HitTrackPDGCode[nHits]/I *
*Entries : 1000000 : Total Size= 27313204 bytes File Size = 2311552 *
*Baskets : 321 : Basket Size= 1881088 bytes Compression= 11.81 *

*Br 3 :HitTrackVertexPosX : HitTrackVertexPosX[nHits]/D *
*Entries : 1000000 : Total Size= 50617321 bytes File Size = 11954086 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 4.23 *

*Br 4 :HitTrackVertexPosY : HitTrackVertexPosY[nHits]/D *
*Entries : 1000000 : Total Size= 50617321 bytes File Size = 11952989 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 4.23 *

*Br 5 :HitTrackVertexPosZ : HitTrackVertexPosZ[nHits]/D *
*Entries : 1000000 : Total Size= 50617321 bytes File Size = 3363416 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 15.05 *

*Br 6 :HitMimosaSensorID : HitMimosaSensorID[nHits]/I *
*Entries : 1000000 : Total Size= 27313854 bytes File Size = 3006581 *
*Baskets : 321 : Basket Size= 1881600 bytes Compression= 9.08 *

*Br 7 :HitEdep : HitEdep[nHits]/D *
*Entries : 1000000 : Total Size= 50611249 bytes File Size = 45228716 *
*Baskets : 548 : Basket Size= 3485184 bytes Compression= 1.12 *

*Br 8 :HitPosX : HitPosX[nHits]/D *
*Entries : 1000000 : Total Size= 50611249 bytes File Size = 46561494 *
*Baskets : 548 : Basket Size= 3485184 bytes Compression= 1.09 *

*Br 9 :HitPosY : HitPosY[nHits]/D *
*Entries : 1000000 : Total Size= 50611249 bytes File Size = 46540076 *
*Baskets : 548 : Basket Size= 3485184 bytes Compression= 1.09 *

*Br 10 :HitPosZ : HitPosZ[nHits]/D *
*Entries : 1000000 : Total Size= 50611249 bytes File Size = 45388200 *
*Baskets : 548 : Basket Size= 3485184 bytes Compression= 1.11 *

*Br 11 :HitStepLengthX : HitStepLengthX[nHits]/D *
*Entries : 1000000 : Total Size= 50615113 bytes File Size = 36831532 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.37 *

*Br 12 :HitStepLengthY : HitStepLengthY[nHits]/D *
*Entries : 1000000 : Total Size= 50615113 bytes File Size = 36781548 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.38 *

*Br 13 :HitStepLengthZ : HitStepLengthZ[nHits]/D *
*Entries : 1000000 : Total Size= 50615113 bytes File Size = 32878626 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.54 *

*Br 14 :HitFirstStepPosX : HitFirstStepPosX[nHits]/D *
*Entries : 1000000 : Total Size= 50616217 bytes File Size = 46333639 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.09 *

*Br 15 :HitFirstStepPosY : HitFirstStepPosY[nHits]/D *
*Entries : 1000000 : Total Size= 50616217 bytes File Size = 46315240 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.09 *

*Br 16 :HitFirstStepPosZ : HitFirstStepPosZ[nHits]/D *
*Entries : 1000000 : Total Size= 50616217 bytes File Size = 44806905 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.13 *

*Br 17 :HitLastStepPosX : HitLastStepPosX[nHits]/D *
*Entries : 1000000 : Total Size= 50615665 bytes File Size = 46604864 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.09 *

*Br 18 :HitLastStepPosY : HitLastStepPosY[nHits]/D *
*Entries : 1000000 : Total Size= 50615665 bytes File Size = 46584281 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.09 *

*Br 19 :HitLastStepPosZ : HitLastStepPosZ[nHits]/D *
*Entries : 1000000 : Total Size= 50615665 bytes File Size = 45442440 *
*Baskets : 548 : Basket Size= 3486208 bytes Compression= 1.11 *

*Br 20 :HitFirstStep4VectorPx : HitFirstStep4VectorPx[nHits]/D *
*Entries : 1000000 : Total Size= 50619093 bytes File Size = 46043152 *
*Baskets : 549 : Basket Size= 3487232 bytes Compression= 1.10 *

*Br 21 :HitFirstStep4VectorPy : HitFirstStep4VectorPy[nHits]/D *
*Entries : 1000000 : Total Size= 50619093 bytes File Size = 46043503 *
*Baskets : 549 : Basket Size= 3487232 bytes Compression= 1.10 *

*Br 22 :HitFirstStep4VectorPz : HitFirstStep4VectorPz[nHits]/D *
*Entries : 1000000 : Total Size= 50619093 bytes File Size = 40042639 *
*Baskets : 549 : Basket Size= 3487232 bytes Compression= 1.26 *

*Br 23 :HitFirstStep4VectorE : HitFirstStep4VectorE[nHits]/D *
*Entries : 1000000 : Total Size= 50618425 bytes File Size = 40056339 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 1.26 *

*Br 24 :HitLastStep4VectorPx : HitLastStep4VectorPx[nHits]/D *
*Entries : 1000000 : Total Size= 50618425 bytes File Size = 46096977 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 1.10 *

*Br 25 :HitLastStep4VectorPy : HitLastStep4VectorPy[nHits]/D *
*Entries : 1000000 : Total Size= 50618425 bytes File Size = 46096055 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 1.10 *

*Br 26 :HitLastStep4VectorPz : HitLastStep4VectorPz[nHits]/D *
*Entries : 1000000 : Total Size= 50618425 bytes File Size = 40146090 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 1.26 *

*Br 27 :HitLastStep4VectorE : HitLastStep4VectorE[nHits]/D *
*Entries : 1000000 : Total Size= 50617873 bytes File Size = 40147710 *
*Baskets : 548 : Basket Size= 3486720 bytes Compression= 1.26 *

I then do,

tree->SetMakeClass(1);
Long64_t nentries = tree->GetEntries();

I then try to read two of the branches as follows,
const int kMaxHits(100);
Int_t nHits;
Int_t HitTrackID[kMaxHits]; //[nHits]

TBranch *b_nHits = tree->GetBranch(“nHits”);
b_nHits->SetAddress(&nHits);
TBranch *b_HitTrackID = tree->GetBranch(“HitTrackID”);
b_HitTrackID->SetAddress(HitTrackID);

for(Long64_t ievt=0; ievt<10; ievt++) {
b_nHits->GetEntry(ievt);
b_HitTrackID->GetEntry(ievt);
cout << "ievt = " << ievt+1 << " nHits = " << nHits << " HitTrkID[0,1] = " << HitTrackID[0] << " " << HitTrackID[1] << endl;
}

The output is,
ievt = 1 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 2 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 3 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 4 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 5 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 6 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 7 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 8 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 9 nHits = 0 HitTrkID[0,1] = 249246688 32708
ievt = 10 nHits = 0 HitTrkID[0,1] = 249246688 32708

As you can see the values of nHits and HitTrackID are always the same for all the events. It seems that the GetEntry Functions don’t load correctly the values of the branch variables and so the variables always have the initial value (which is mainly random as I don’t give then an initial value).

Does anyone knows what I am doing wrong?

Many thanks,

Alejandro

Hi,

I think you shouldn’t close the file. Afaik, the Tree loads data only on demand.

Cheers,
Jochen

Can you attach (or make it accessible in any other way) your ROOT file?

Many thanks! I moved the fdata.Close(); command after the loop and it works now.

Alejandro