Hi Fons,
Here is the structure of the code I have written:
class MyClass {
public:
Tfile* file;
private:
TTree* tree;
int SimpleInteger;
int* ArrayInteger;
TLorentzVector* ALorentzVector;
TClonesArray* LorentzVectorArray;
void Initialize() {
file = TFile::Open(“FileName.root”,“CREATE”);
tree = new TTree(“treename”,“Tree Title”);
tree->SetDirectory(file);
CreateBranches();
}
void CreateBranches() {
SimpleInteger = 10; //Will give the size of the arrays
tree->Branch(“SimpleInteger”, &SimpleInteger, “SimpleInteger/I”,32000);
ArrayInteger = new int[SimpleInteger];
tree->Branch(“ArrayInteger”, ArrayInteger, “ArrayInteger[SimpleInteger]/I”, 32000);
ALorentzVector = new TLorentzVector(-1000,-1000,-1000,-1000);
tree->Branch(“ALorentzVector”, “TLorentzVector”, &ALorentzVector, 32000, 99);
LorentzVectorArray = new TClonesArray(“TLorentzVector”, SimpleInteger);
tree->Branch(“LorentzVectorArray”, “TClonesArray”, &LorentzVectorArray, 32000, 99);
}
void AddEvent(Event EventObject) {
// The Class Event has many member functions which yield integers, doubles, TVector3
// and TLorentzVector’s. So the branch variables are equated to a new number/object
// each time. Simple integer is not changed.
ALorentzVector = InitialParticleMomentum; //Filled once per event
//loop over trajectories in the event, many per event
for(int i=0; …) {
ArrayInteger[i] = trajectoryID;
new((*LorentzVectorArray)[i]) TLorentzVector(trajectoryMomentum);
}
tree->Fill(); //Fill the tree every event
}
void finalise() {
file->cd();
tree->Write();
}
};
The initialize is invoked once and the AddEvent is invoked every event and the finalise is invoked at the end of looping over events.
I have tried using valgrind to find the memory leak and it seems to be in the AddEvent function some where. I have many TClonesArray’s and other objects that I fill each event. The program steadily increase the memory it uses by about 2Mb every ~5mins
Thanks for your help,
Ben