# Data Structure Question - Storing Data per Event

**URL:** <https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376>\
**Category:** ROOT\
**Created:** [October 24, 2014, 7:06am UTC](https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376 "2014-10-24T07:06:02Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 24, 2014, 1:33pm UTC](https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376/2 "2014-10-24T13:33:05Z")

</div>

See:  
[ROOT User’s Guide -\> Trees -\> Example 2: A Tree with a C Structure -\> Writing the Tree -\> Adding a Branch with a Variable Length Array](http://root.cern.ch/root/html534/guides/users-guide/Trees.html#adding-a-branch-with-a-variable-length-array)  
Try: [code]#include “TFile.h”  
#include “TTree.h”  
#include “TRandom.h”

const unsigned int kMax\_Ntp = 8;

struct Data {  
Int\_t event;  
Float\_t triggerTime;  
UInt\_t Ntp; // note: Ntp MUST be \<= kMax\_Ntp  
Float\_t pulseAmp[kMax\_Ntp];  
Float\_t pulseTime[kMax\_Ntp];  
} fDataStruct;

void trial(void) {  
TFile \*f = TFile::Open(“fDataFile.root”, “RECREATE”);  
TTree \*t = new TTree(“fDataTree”, “some tp data”);

// [http://root.cern.ch/root/html/TTree.html](http://root.cern.ch/root/html/TTree.html)  
t-\>Branch(“event”, &fDataStruct.event, “event/I”);  
t-\>Branch(“triggerTime”, &fDataStruct.triggerTime, “triggerTime/F”);  
t-\>Branch(“Ntp”, &fDataStruct.Ntp, “Ntp/i”);  
t-\>Branch(“pulseAmp”, fDataStruct.pulseAmp, “pulseAmp[Ntp]/F”);  
t-\>Branch(“pulseTime”, fDataStruct.pulseTime, “pulseTime[Ntp]/F”);

gRandom-\>SetSeed(0);  
for (int i = 0; i \< 1000; i++) {  
fDataStruct.event = i;  
fDataStruct.triggerTime = 100.0 \* i;  
fDataStruct.Ntp = UInt\_t((1.0 + kMax\_Ntp) \* gRandom-\>Rndm());  
// just a precaution (Ntp MUST be \<= kMax\_Ntp)  
if (fDataStruct.Ntp \> kMax\_Ntp) fDataStruct.Ntp = kMax\_Ntp;  
for (int j = 0; j \< ((int)(fDataStruct.Ntp)); j++) {  
fDataStruct.pulseAmp[j] = gRandom-\>PoissonD(10);  
fDataStruct.pulseTime[j] = gRandom-\>Gaus(10);  
}  
t-\>Fill();  
}

t-\>Write();  
delete f; // automatically deletes “t”, too  
}[/code]

---

_[View the full topic](https://root-forum.cern.ch/t/data-structure-question-storing-data-per-event/18376)._
