How to merge two TH2D histograms (two contour plots)?

Dear rooters,

I have the following problem:

I have two TH2D histograms -each corresponding to a different phenomenological model- which, independently drawn with the CONT4 option, result in two separate contour plots, each one showing regions of visibility of a certain signal. Both contour plots have the same x and y axes. Each contour plot has only two contour levels, so that each of them has only two regions: one in which the model is visible and one in which it isn’t.

I need to merge both contour plots into a single one, showing four different regions: one in which the signal predicted by model A alone is visible, one in which the signal predicted by model B alone is visible, one in which neither predicted signal is visible, and one in which both predicted signals is visible.

I have attempted to superimpose the two original contour plots, but I just end up having the top one covering the bottom one completely.

Do you have any ideas on how to merge the two plots?

Thankfully,

Mauricio

Have you tried to draw the contour plots using lines instead of filled contours ?
See all the contour options here:

root.cern.ch/root/html/THistPainter.html#HP16

I do need to fill the regions because, once I manage to merge the two contour plots, I will plot other curves (just lines). In this final plot, then, the colour of the background will indicate whether or not the curves shown in the foreground have any meaning within the context of one or the other model (A or B).

May be you can group the two histograms into one ? … Would be easier to help you if you send a small running example showing what you have right now.

Thanks for the quick reply. Here is a running example. You will need two external txt files which I am attaching.

{
gROOT->Reset();
ifstream flist;

Double_t alpha, zmax, SNR, zmaxMAX, zmaxMIN;

zmaxMIN = 0.001;
zmaxMAX = 0.03;

// ROOT file where we will save the combined-histograms plot
TFile f("MODELCOMP_Enumin_10^5.root","RECREATE");

////// Visibility area (signal-to-noise ratio) for BB model //////
//TFile f("MODELCOMP_Enumin_10^5_SNRBB.root","RECREATE");
TH2D *h2B = new TH2D("h2B"," ", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
flist.open("MODELCOMP_Enumin_10^5_SNRBB.txt");
while(!flist.eof()){ // Fill the histogram
  flist >> alpha >> zmax >> SNR;
  h2B->Fill(alpha, zmax, SNR); 
}
flist.close();

// Visibility area (signal-to-noise ratio) for KT model
//TFile f("MODELCOMP_Enumin_10^5_SNRKT.root","RECREATE");
TH2D *h2C = new TH2D("h2C"," ", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
flist.open("MODELCOMP_Enumin_10^5_SNRKT.txt");
while(!flist.eof()){ // Fill the histogram
  flist >> alpha >> zmax >> SNR;
  h2C->Fill(alpha, zmax, SNR); 
}
flist.close();

// Define contour levels
// Visibility condition: SNR>=3
h2B->SetContour(2);
h2B->SetContourLevel(0,3);
h2B->SetContourLevel(1,10000.0); //10000 is a high, non-existing, level
h2C->SetContour(2);
h2C->SetContourLevel(0,0); //0 is a low, non-exiting, level
h2C->SetContourLevel(1,3);

// Show colour legend and palette
h2B->GetZaxis()->SetLabelSize();
gStyle->SetPalette(16);

// Now draw the two histograms in a single, superimposed, plot
h2B->Draw("CONT4SAME");
h2C->Draw("CONT4SAME");

f->Write();
}

MODELCOMP_Enumin_10^5_SNRKT.txt (37.4 KB)
MODELCOMP_Enumin_10^5_SNRBB.txt (37.4 KB)

couet,

Could a possible solution be to use the LIST draw option for the TH2s to extract the contour lines as TGraph objects and then use these to build the merged contour plots?

If so, how could I fill the area between the curves when plotting the TGraph objects?

I had not time yet to run you example. I have something else to finish before. Sorry for that. Without running you example and without having seen in details what you want, my first guess would be that should group the 2 histogram into one. But may be that not the answer to your problem. I need more time.

Something is wrong with our macro, the 2nd histo is empty. I Have tried:

        // Now draw the two histograms in a single, superimposed, plot
//      h2B->Draw("CONT3");
        h2C->Draw("box");

it gives an empty plot

    h2B->Draw("box");

gives something

That’s strange: when I run the macro, both histos are filled.

To check, I’ve copied-and-pasted the macro I posted (instead of using the one I had already on my computer), downloaded the txt files I had attached and re-run the macro, changing only

// Now draw the two histograms in a single, superimposed, plot //h2B->Draw("CONT4SAME"); //h2C->Draw("CONT4SAME"); h2B->Draw("box");

and it does work.

I am attaching the results of running h2B->Draw("box"); and

h2C->Draw("box");.



I tried again and for me H2C is empty. I will try to figure out…

Thank you, couet. The support for ROOT is just awesome.

Ok I finally made it work. Now the macro is:

{
   ifstream f1,f2;

   Double_t alpha, zmax, SNR, zmaxMAX, zmaxMIN;

   zmaxMIN = 0.001;
   zmaxMAX = 0.03;

   TH2D *h2B = new TH2D("h2B"," ", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
   f1.open("MODELCOMP_Enumin_10^5_SNRBB.txt");
   while(!f1.eof()){ // Fill the histogram
      f1 >> alpha >> zmax >> SNR;
      h2B->Fill(alpha, zmax, SNR);
   }
   f1.close();

   TH2D *h2C = new TH2D("h2C"," ", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
   f2.open("MODELCOMP_Enumin_10^5_SNRKT.txt");
   while(!f2.eof()){ // Fill the histogram
      f2 >> alpha >> zmax >> SNR;
      h2C->Fill(alpha, zmax, SNR);
   }
   f2.close();

   // Define contour levels
   // Visibility condition: SNR>=3
   h2B->SetContour(2);   
   h2B->SetContourLevel(0,3);   
   h2B->SetContourLevel(1,10000.0); //10000 is a high, non-existing, level   
   h2C->SetContour(2);   
   h2C->SetContourLevel(0,0); //0 is a low, non-exiting, level   
   h2C->SetContourLevel(1,3);   

   // Show colour legend and palette
   h2B->GetZaxis()->SetLabelSize();
   gStyle->SetPalette(16);

   // Now draw the two histograms in a single, superimposed, plot
   h2B->SetLineColor(2);
   h2B->Draw("cont3");
   h2C->SetLineColor(3);
   h2C->Draw("cont3 same");
}

I get the attached picture. That’s it the simplest way to show the two histograms as contours on the same plot. On this plot what area would you like to fill with color ? As you see the two plots are crossing so that’s not obvious define what area should be colored"


Great!

Well, I am attempting to fill different areas with different colors (they will be shades of gray in the final version of the plot) [see attached image]: the area from the left up to the first red curve should have one color; the area from here to the first green curve, another; and the area from the first green curve to the second green curve, a different one.

Is this possible?


may be something like that:

{
   ifstream f1,f2;

   Double_t alpha, zmax, SNR, zmaxMAX, zmaxMIN;

   zmaxMIN = 0.001;
   zmaxMAX = 0.03;

   TH2D *h2B = new TH2D("h2B","h2B", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
   f1.open("MODELCOMP_Enumin_10^5_SNRBB.txt");
   while(!f1.eof()){ // Fill the histogram
      f1 >> alpha >> zmax >> SNR;
      h2B->Fill(alpha, zmax, SNR);
   }
   f1.close();

   TH2D *h2C = new TH2D("h2C","h2C", 21, 2, 3, 25, log10(zmaxMIN), log10(zmaxMAX));
   f2.open("MODELCOMP_Enumin_10^5_SNRKT.txt");
   while(!f2.eof()){ // Fill the histogram
      f2 >> alpha >> zmax >> SNR;
      if (SNR>=3) h2C->Fill(alpha, zmax, SNR);
   }
   f2.close();

   // Define contour levels
   // Visibility condition: SNR>=3
   h2B->SetContour(2);   
   h2B->SetContourLevel(0,3);   
   h2B->SetContourLevel(1,10000.0); //10000 is a high, non-existing, level   
   h2C->SetContour(2);   
   h2C->SetContourLevel(0,0); //0 is a low, non-exiting, level   
   h2C->SetContourLevel(1,3);   

   // Show colour legend and palette
   h2B->GetZaxis()->SetLabelSize();
   gStyle->SetPalette(16);

   // Now draw the two histograms in a single, superimposed, plot
   h2B->SetLineColor(2);
   h2B->Draw("col");    
   h2C->SetLineColor(3);
   h2C->Draw("colsame");    
}