Drawing multiple histograms with different domains and bin widths on the same canvas


ROOT Version: 6.14
Platform: CentOS
Compiler: Not Provided


Hello,

I have several TH1F objects, all with the same number of bins, though with different bin widths, bin content, and natural domains. I initialize an array of them (note the TH1F with (…,66,0.,0.), which gives 66 bins and then allows the histogram to determine its own domain), then loop through them and fill them from a tree branch containing a variable DP, then reweight each bin by a particular normalization factor cobbled together from histograms. After that, outside these loops, I loop over all of the histograms in the array and draw each of them to a single canvas.

...
  TH1F* hist_YIELDS_dp[vector_length];
…
   for(int i=0; i<vector_length; i++)                                                                                
    {                                                                                                               
      hist_YIELDS_dp[i] = new TH1F("run_hist","run_hist_title",66,0.,0.);                                      
      hist_YIELDS_dp[i]->Sumw2();                                                                                   
      hist_YIELDS_dp[i]->SetAxisRange(0.,0.5,"Y");                                                                  
      hist_YIELDS_dp[i]->SetLineColor(i+1);                                                                         
      hist_YIELDS_dp[i]->SetTitle(hist_names_dp[i]);                                                                
      hist_YIELDS_dp[i]->SetName(hist_names_dp[i]);
…
   for(int j=0; j<NumberEntries; j++)
    {
...
      hist_YIELDS_dp[i]->Fill(DP,ps_fact[i]/(charge[i]*e_track_effs[i]*comp_lt[i]));
…
     }
   }
…
  TCanvas *c2 = new TCanvas;
...                                                                                                                    
  for (Int_t i = 0; i<vector_length; i++)                                                                           
    {
      c2->cd();                                                                                                     
      hist_YIELDS_dp[i]->Draw("hist && same");
...                                                                     
    }                                                                                                               
  c2->BuildLegend(0.9, 0.8, 0.55, 0.55);                                                                            
  c2->Print("all_runs_carbon_21_degrees_dp.png");
...

So, all of this works, but the issue is that the domain of the canvas (created from the Draw command) is determined from the histogram drawn first, not the whole slew of them that I need to show up across it. These histograms, for a variety of reason, have different bin widths and content, although the number of bins is identical (66 total). Is there a way to draw all of them on the same canvas without defining a domain for each of them? This will be a part of an automated procedure to produce a plethora of histograms, and it isn’t feasible that I go through and check each one.

Try the THStack with its “nostack” drawing option.

1 Like

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