Hello,
I have the following class (I simplify)
[code]class Vertex : public TObject {
public:
std::vector *_linkToAssociatedTracks;
Vertex() { _linkToAssociatedTracks = 0; }
~Vertex() { if (_linkToAssociatedTracks) delete _linkToAssociatedTracks; }
void AddTheLink (int theLink);
ClassDef(Vertex, 1);
}
void Vertex::AddTheLink (int theLink) {
if (!_linkToAssociatedTracks) _linkToAssociatedTracks = new std::vector;
if (_linkToAssociatedTracks) _linkToAssociatedTracks->push_back(theLink);
}[/code]
and I want to put it inside a TTree via a TClonesArray, so I can store multiple vertices within the same event (i.e. filling the tree event-wise).
I read somewhere that I should somehow define
Vertex::Clear() {
if (_linkToAssociatedTracks) delete _linkToAssociatedTracks;
}
and call
[code] // do the following once
TTree *tree;
TClonesArray *vertices;
vertices = new TClonesArray(“Vertex”, 20);
ntuple->Branch(“Vertex”, &vertices);
// do the following for each event
vertices->Clear(“C”);
for (int i = 0; i < number_of_vertices; i++) {
new((*vertices)[i]) Vertex();
Vertex *thisVertex = (Vertex *)((*vertices)[i]);
for (int j = 0; j < number_of_tracks[i]; j++) {
thisVertex->AddTheLink(…)
}
[/code]
Is this the correct approach? Actually the code crashes, so I’m not so confident about that.
Thank you in advance for your help!