Clearing Canvas

Hi, quick question: I am trying to draw a multigraph on a canvas in a loop, which means it should always delete the previous plot before drawing the new ones on the canvas. My code looks something like this:

....
TMultiGraph* multiGraph = new TMultiGraph();

while (true) {

         for (Int_t i = 0; iSample < branchNames.size(); ++i) {
               // get the branch of my tree
              //  get the leaf (each entry is an array of floats)

              // create new graph
}

         for (Int_t j = 0; j < arrayLength; ++j){
             // set points of graph with values of the entry's array
}
            
            // add graph to multigraph
}
               
               multiGraph->Draw("AL");
               canvas->Update()

                ....
                        

So basically each new iteration of the while loop, the canvas should be cleared from the previous plots/graphs. I have tried everything, i.e. putting

multigraph->Clear();

canvas->Clear();

canvas->Modified();

In different combinations in different places of the code but nothing seems to work. It keeps overlaying the plots. The only thing that works is creating a new instance of multigraph each new iteration of the loop. But that does not seem like an efficient elegant way.

It is strange the plots are overlayed because the option “A”, when you draw the multipgraph, asks to redefine the axis and produce a new plot. Do you have a simple macro reproducing this effect ?

multigraph->Clear(); does nothing … you need: delete multigraph; multigraph = new TMultiGraph();

Ok I guess I’ll go with this solution. Will the former graphs added in the loop also be deleted from memory when deleting the multigraph?

The TMultiGraph owns the objects in the list.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.