Multiple TF1::Draw within compiled code with stack objects

Hi all,

I’m using Root within a compiled application. More precisely, I’m using it to plot in a png file several functions relevant to my application domain. Briefly, my code can be said to be (this code snippet would be called, eventually, several times in order to Print different plots.):

      /*
       * Suppose the following local vars:
       * functor, container, minX, maxX, id.
       */
      std::stringstream isCanvas;
      isCanvas << "c" << id;
      // Create a canvas for drawing.
      TCanvas c1(isCanvas.str().c_str() ,"t", 200, 10, 700, 500);
      std::stringstream isFun;
      isFun << "fun" << id;
      // Create and draw the main function.
      TF1  fun(isFun.str().c_str(), &functor, minX, maxX, 1, "");
      fun.SetLineColor(kRed);
      fun.Draw();
      unsigned int ppp = 0;
      // Loop over a container with more functions.
      for (Container::const_iterator p = container.begin(); p != container.end(); ++p) {
        Functor* functor2 = new Functor(*p);
        std::stringstream isFun2;
        isFun2 << "fun2_" << id << "_" << ppp++;
        TF1* fun2 = new TF1(isFun2.str().c_str(), functor2, minX, maxX, 1, "");
        fun2->SetLineColor(kBlack);
        fun2->Draw("same");
      }
      // Update the canvas with all the functions and then print it in a file.
      c1.Update();
      std::stringstream is;
      is << "canvas" << id << ".png";
      c1.Print(is.str().c_str());

Initially, I used inside the loop also stack allocated objects; but they didn’t show up in the final plot (generated by the last Print call), I even tried to put an Update after the Draw within the for loop. So I changed to heap allocated ones and then the drawings started to look fine.

What I’d like to ask is about the ownership of the functions created. Using via gROOT->GetListOfFunctions() I checked the existance of the functions (put that call in 3 places: at the beginning, at the end, and after the outtermost scope of my code). Initially there was none; but in subsequent calls the formerly created functions still appeared on the list.

What I had in mind initially was to code a self contained function (everything it creates gets destroyed after returning from it), without explicitly heap-allocated objects (avoiding potential leakages) and without much worrying about global issues (for instance, taking care of name clashing).

What’s the best style / recommended way to do that?

Thanks a lot for your help.

Rodolfo

If you have a function like

void foo() { TF1 f1("f1",....); f1.Draw(); } the function is drawn in the canvas, but immediately deleted when the object goes out of scope (when the function terminates).

Rene

Hi Rene. Thanks a lot for your answer.

I see now why initially it didn’t work with the objects allocated in the stack within the for-loop.

So, what I’ve done is to allocate (inside the for-loop) in the heap, and then storing the pointers in a container that’s stack-allocated, and whose scope is the same as the canvas. The destructor of those containers takes care of deleting the pointers.

Thanks again.