Problem with TMultiGraph

Dear Rooters

In my program I want to draw density plots for leaves from multiple trees.
The corresponding code fragment is as follows:

void DrawDensity(const char *leafname)
{
   TCanvas     *fCanvas = new TCanvas("test", "test", 10, 10, 400, 400);
   TMultiGraph *mgraph  = new TMultiGraph();

   TH1F   *frame = 0;
   TGraph *graph = 0;

   for (Int_t i=0; i<numtrees; i++) {
      tree = (TTree*)(fTrees->At(i));
      leaf = tree->FindLeaf(leafname);
      brch = leaf->GetBranch();

      FillArrays(entries, brch, leaf, index, arr);

      Density(entries, arr, wght, npts, xden, yden, kernel);

      graph = new TGraph(npts, xden, yden);

      mgraph->Add(graph, opt);
   }//for_i

   frame = gPad->DrawFrame(fMinX - 0.2*fMinX, fMinY - 0.2*fMinY, fMaxX + 0.2*fMaxX, fMaxY + 0.2*fMaxY);

   frame->SetTitle(title);
   frame->SetXTitle(titleX);
   frame->SetYTitle(titleY);

//gPad->Clear();
   mgraph->Draw("A"); 
//gPad->Update();
//gPad->Modified();
}//DrawDensity

Using file “Plot.root” the resulting figure is shown in “C1_Density1.jpg”. The problems are the axes ranges.
Uncommenting “gPad->Clear()” etc results in correct axes ranges, but the title and the axes labels are
no longer visible, as shown in “C1_Density2.jpg”.

How can I get the correct axes ranges and axes labels?

I have also attached a macro “macroDrawDensity.C” which contains the relevant code from my program.
However, the output of the macro is strange:

root [0] .L macroDrawDensity.C
root [1] DrawDensity()        
tree= Tree1
arr: fMinX= 1   fMinY= -2.10327   fMaxX= 10000   fMaxY= 5.06962
den: fMinX= -2.53105   fMinY= -2.10327   fMaxX= 10000   fMaxY= 5.06962
tree= Tree2
arr: fMinX= 1   fMinY= -2.28327   fMaxX= 10000   fMaxY= 5.55057
den: fMinX= -2.70923   fMinY= -2.08726e+148   fMaxX= 10000   fMaxY= 8.53108e+163
tree= Tree3
arr: fMinX= 1   fMinY= -5.21461   fMaxX= 10000   fMaxY= 2.11885
den: fMinX= -5.64358   fMinY= -5.21461   fMaxX= 10000   fMaxY= 2.11885
pad: fMinX= -5.64358   fMinY= -5.21461   fMaxX= 10000   fMaxY= 2.11885

As shown, fMinY and fMaxY for Tree2 have unexpectedly large values, although I used the same file “Plot.root”
as in the attached Figures. However, when I draw Tree2 alone, using DrawDensity(“Tree2”), it is drawn correctly.

Can you tell me what might be wrong?

P.S.: I am using root version 5/17.02 on MacOS X.

Thank you in advance.
Best regards
Christian
macroDrawDensity.C (19.7 KB)




Christian,

For your first problem with TMultiGraph, do not specify option “A”.

For your second problem, my guess is that you should recompure the variable entries for the second Tree, otherwise you will get wrong values oif your two Trees do not have the same number of entries.

Rene

Dear Rene

My problem with the first problem is, that initially I did not specify option “A”.
The result is shown in the attached figure “C1_Density3.jpg”. As you see, the axes
ranges are completely wrong; only adding option “A” seemed to solve this problem.
I have no idea, why e.g. the x-axis displays as range the number of tree entries.

For the second problem, recomputing the variable entries did not help. It seems that
after loading the macro I can only draw one tree, e.g. DrawDensity(“Tree2”).
But even when only drawing one tree, the macro can demonstrate the problem, when not
using option “A”, i.e. the wrong axes ranges.

Do you have any ideas, what might be the reason?

Best regards
Christian


If you call gPad->DrawFrame you should not use theoption “A” when drawing the multigraph.
Your axis are wrong because you have an error in your program. To see what I mean, I suggest to

mgraph->Add(graph, opt); mgraph->Print("all"); //<====add this line and you will see that the Y values are nan

Rene

Dear Rene

Thank you, finally I found my error: I have to declare new variables minX, etc to find min/max values. I have attached the corrected macro.

Best regards
Christian

P.S.: The macro works only with one tree, i.e. DrawDensity(“Tree1”), and produces random values for the second tree.
macroDrawDensity.C (19.7 KB)

Christian,

You did not fix the problem in your latest version. Your y values are still undefined. Simply add the following statement at line 672

mgraph->Add(graph, opt); mgraph->Print("all"); //<====add this line
and you will see that the y values are undefined.

Rene

Dear Rene

Yes, I know that this problem is not solved, and I have no idea, what might be the reason.

When I start root every time new before calling DrawDensity(“Tree1”), DrawDensity(“Tree2”),
then the x,y-coordinates are printed correctly, and the density plot is correct. However,
when I call DrawDensity(“Tree1”), DrawDensity(“Tree2”) in the same root session, I get the
following output:

m3412p005:/Volumes/CoreData/ROOT/rootcode/xps-x.x.x rabbitus$ root
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.17/04   16 October 2007   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.17/04 (trunk@20359, Oct 19 2007, 14:42:00 on macosx)

CINT/ROOT C/C++ Interpreter version 5.16.26, Oct 11, 2007
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L macroDrawDensity.C
root [1] DrawDensity("Tree1") 
numtrees= 1
entries=  10000
tree= Tree1
arr: fMinX= 1   fMinY= -2.10327   fMaxX= 10000   fMaxY= 5.06962
den: minX= -2.53105   minY= -2.96401e-17   maxX= 5.4974   maxY= 0.402156
pad: minX= -2.53105   minY= -2.96401e-17   maxX= 5.4974   maxY= 0.402156
root [2] .q
m3412p005:/Volumes/CoreData/ROOT/rootcode/xps-x.x.x rabbitus$ root
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.17/04   16 October 2007   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.17/04 (trunk@20359, Oct 19 2007, 14:42:00 on macosx)

CINT/ROOT C/C++ Interpreter version 5.16.26, Oct 11, 2007
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L macroDrawDensity.C
root [1] DrawDensity("Tree2") 
numtrees= 1
entries=  10000
tree= Tree2
arr: fMinX= 1   fMinY= -2.28327   fMaxX= 10000   fMaxY= 5.55057
den: minX= -2.70923   minY= -5.56575e-17   maxX= 5.97653   maxY= 0.395958
pad: minX= -2.70923   minY= -5.56575e-17   maxX= 5.97653   maxY= 0.395958
root [2] .q
m3412p005:/Volumes/CoreData/ROOT/rootcode/xps-x.x.x rabbitus$ root
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.17/04   16 October 2007   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.17/04 (trunk@20359, Oct 19 2007, 14:42:00 on macosx)

CINT/ROOT C/C++ Interpreter version 5.16.26, Oct 11, 2007
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L macroDrawDensity.C
root [1] DrawDensity("Tree3") 
numtrees= 1
entries=  10000
tree= Tree3
arr: fMinX= 1   fMinY= -5.21461   fMaxX= 10000   fMaxY= 2.11885
den: minX= -5.64358   minY= -3.57451e-17   maxX= 2.54782   maxY= 0.389175
pad: minX= -5.64358   minY= -3.57451e-17   maxX= 2.54782   maxY= 0.389175
root [2] .q
m3412p005:/Volumes/CoreData/ROOT/rootcode/xps-x.x.x rabbitus$ root
  *******************************************
  *                                         *
  *        W E L C O M E  to  R O O T       *
  *                                         *
  *   Version   5.17/04   16 October 2007   *
  *                                         *
  *  You are welcome to visit our Web site  *
  *          http://root.cern.ch            *
  *                                         *
  *******************************************

ROOT 5.17/04 (trunk@20359, Oct 19 2007, 14:42:00 on macosx)

CINT/ROOT C/C++ Interpreter version 5.16.26, Oct 11, 2007
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0] .L macroDrawDensity.C
root [1] DrawDensity("Tree1") 
numtrees= 1
entries=  10000
tree= Tree1
arr: fMinX= 1   fMinY= -2.10327   fMaxX= 10000   fMaxY= 5.06962
den: minX= -2.53105   minY= -2.96401e-17   maxX= 5.4974   maxY= 0.402156
pad: minX= -2.53105   minY= -2.96401e-17   maxX= 5.4974   maxY= 0.402156
root [2] DrawDensity("Tree2")
numtrees= 1
entries=  10000
tree= Tree2
arr: fMinX= 1   fMinY= -2.28327   fMaxX= 10000   fMaxY= 5.55057
den: minX= -2.70923   minY= 1.79769e+308   maxX= 5.97653   maxY= -1.79769e+308
Warning in <TCanvas::ResizePad>: Inf/NaN propagated to the pad. Check drawn objects.
Warning in <TCanvas::ResizePad>: test height changed from 0 to 10

I have no idea what the reason for this behavior is.

At least I could solve the problem, for which I have created this macro, and my program
does not have this problem (this would be devastating).

Best regards
Christian

Dear Rene

You were right, I had to initialize a couple of arrays to zero, now everything works, see enclosed macro.

Best regards and thank you
Christian
macroDrawDensity.C (19.9 KB)