Hello,
I understand that in user’s guide, there is an example about how to create a branch by ‘struct’. This is the case only for 1 object per entry. If I have more than 1 object data to store, how can I create the branch and fill the data? I know I can write a class for my object and then use TClonesArray to fill them. Here I would avoid to write a new class. I am wondering if there are some methods like:[code]
testStruc() {
struct myobject{
double pt, px, py, pz;
};
TFile *f = new TFile(“test.root”, “RECREATE”);
TTree *t = new TTree(“test”, “test”);
TClonesArray * array = new TClonesArray(“myobject”);
t->Branch(“Jet”, &array);
for ( int i =0; i<1000; i++ ) {
array->Clear();
for (int ia=0; ia<10; ia++) {
myobject jet;
jet->pt = i0.01;
cout << jet->pt << endl;
new (array[ia]) myobject(*jet);
}
}
}[/code]
Of course, the code above is definitely not working.
Thanks,
Zhiyi.
Hi,
In order to have a TClonesArray of myobject, you class myobject needs to:
[ul]* Inherit from TObject
- have ClassDef(myobject,1); at the end of its declaration
- have a compiled dictionary generated for it (via rootcint and/or ACLiC)[/ul]
Cheers,
PHilippe
Thanks for your information.
I know how to create a branch by co-operation of TClonesArray and TMyClass as mentioned in your reply. But I don’t know how to do that by ‘struct’ and TClonesArray since I don’t want to write a new class.
Cheers,
Zhiyi.
[quote]But I don’t know how to do that by ‘struct’ and TClonesArray[/quote]As I mentioned, this is not possible. You couldcreate a struct with C style array … but this is a bad awkward!
Why? A priori this is very simple. Use the (minimal) modification of your script below, you can simply use it by compiling via ACLiC
[code] #include “TFile.h”
#include “TTree.h”
#include “TClonesArray.h”
struct myobject : public TObject {
double pt, px, py, pz;
ClassDef(myobject,1);
};
testStruc() {
TFile *f = new TFile(“test.root”, “RECREATE”);
TTree *t = new TTree(“test”, “test”);
TClonesArray * array = new TClonesArray(“myobject”);
t->Branch(“Jet”, &array);
for ( int i =0; i<1000; i++ ) {
array->Clear();
for (int ia=0; ia<10; ia++) {
myobject jet;
jet->pt = i0.01;
cout << jet->pt << endl;
new (array[ia]) myobject(*jet);
}
}
}[/code]
Cheers,
Philippe.