Looping over several MakeClass objects

Hi,

I think this is a very basic question, but I cannot seem to find the solution.

I have a root macro than simply does something like this:

void mylooper(){
string rootfiles;
rootfiles=*.root;

gROOT->LoadMacro("MyNTupleLooper.C++g");
MyNTupleLooper *a = new MyNTupleLooper();
a->AddFiles(rootfiles.c_str());
a->Loop();
}

Were MyNTupleLooper.C is a lopper class made by MakeClass and where I have modified MakeClass to handle a chain of files (The AddFiles function)

this works perfectly.

Now, I have wanted for some time to be able to loop over this sequence for different datasets, thus each time I would like to create a new MyNTupleLooper object, and new output, just as it would be if I just ran the macro twice for example.

But I have no idea how to delete the previous object here, a, and begin again. I always get the problem of histograms being replaced, even if I try to do something like this:

void mylooper(){
for(int it=0; it<path.size(); it++){

    gROOT->Reset();

    MyNTupleLooper *a = new MyNTupleLooper();
    a->AddFiles(path.at(it).c_str());
    a->Loop();

  
}
}

where path now is a vector object with the paths to my dataset-rootfiles (ntuples). I was hoping that to create the MyNTupleLooper object for each iteration again with new would be the solution, but obviously it is not.

Any suggestions are welcome

Thanks!

Maiken

Hi,

You need to make to delete the histogram and/or TFile in MyNTupleLooper::~MyNTupleLooper and call ‘delete a;’ in your loop.

Also to not call gROOT->Reset() from with a C++ function (it wipes the functions itself from being known by CINT …)

Cheers,
Philippe.

Thanks a lot for your reply.

I however still get the messages:

Warning in TROOT::Append: Replacing existing TH1: hMu_indet_neg_pt (Potential memory leak).
Warning in TROOT::Append: Replacing existing TH1: hMu_indet_neg_low_pt (Potential memory leak).
etc
etc…

which for me indicates that the object really is not deleted? I delete all the histograms in my looper class - what else can I do I wonder?

Maiken

Hi,

[quote]what else can I do I wonder[/quote]First you need to make sure you know where the histogram are coming from, they could come from either a ROOT file (via a TFile object), a TTree::Draw command, call to new TH1F or TH1D (etc.) or from some sub-routine you are calling. Then you need to make sure you call the delete operator on each of those.

Cheers,
Philippe.

Thank you very much, that worked nicely.

Maiken