Creating TH2F by using two TH1Fs

Hi All,

I tried to create TH2F Histogram by using two TH1F histograms with this script…

TH2F *h1 = new("h1","h1",96,0,2700000,96,0,2700000);
TH1F *h2 = new("h2","h2",48,0,2700000);
TH1F *h3 = new("h3","h3",48,0,2700000);
h2->Fill(x);
h3->Fill(y);
int nbins = 48;
h1->Fill(h3->GetBinContent(nbins),h2->GetBinContent(nbins));
h1->Draw();

Why did not it fill h1 histogram by GetBinContent(nbins) ?

Thanks for your help,
Cheers,
Ersel

Hi Ersel,

so you are trying to put a “1” in the bin which is defined by the content in bin #48 in h1 and h2. Is that really what you want? Looking at your snippet, it’s unclear what is in bin #48 for h1 and h2. What is x and y?

I just put them there to show having been filled the histograms by the Fill(),that is, x and y are any values for filling the histograms. Though My script is really complex, I didn’t put here that…

Anyway, I want to create a TH2 histogram(96bin) with all the X-axis values(48bin) of the h2 histogram and all X-axis values(48bin) of the histogram h3, which they correspond to X and Y axis values in TH2, respectively.

Cheers,
Ersel

In this case, you have to have a loop somewhere. Here is a piece of pseudocode:

TH2F *h1 = new("h1","h1",96,0,2700000,96,0,2700000);
TH1F *h2 = new("h2","h2",48,0,2700000);
TH1F *h3 = new("h3","h3",48,0,2700000);

//fill your h2 here

//fill your h3 here

for (all the bins in h1, let i be the X-axis bin number, and j be the Y-axis bin number)
{
  h1->Fill(h2->GetBinContent(i), h3->GetBinContent(j), 1);
}

Ok done,Thanks.

But there is a problem related to the values of bin contents. Why the values in the h1 are in the range of “2” for X and “3” for Y-axis? .The values in h1 state as if compared(proportion)with each other !

Normally, Their values in h3 and h2 must be in the range of 10^6 !!!

Cheers,
Ersel

Sorry, I don’t understand what you’re trying to say. Could you maybe post some screenshots of your histograms here?

Yes, of course…

The file is in the attachment as pdf…
HistByBinContent.pdf (16.5 KB)

So AFAIU you have managed to fill your h1 96 times with (0; 0). Why zeroes? It may help to look at h2 and h3, too.

I don’t know why zeros.

When dragging by hold the axises,that is,the ranges of the axises are changed, the histogram h1 is as shown in the attachment (“Expandedhist.pdf”)!

And also my h2 and h3 histograms are in the attachment.Normally, the ranges of the bin contents are in the values of 10^6 as shown…

Cheers,
Ersel
h2 and h3.pdf (16.3 KB)
ExpandedHist.pdf (16.7 KB)

Could you help me?
Why the data gather in the origin of the histogram HVGAIN although,normally, the values of data are in the range of 10^6 for two histogtams GAIN_650 and GAIN_700.

The histograms HVGAIN and GAIN_650(h2),GAIN_700(h3) are in the attachment…


TH2F *HVGAIN = new TH2F("GAIN for HVs","GAIN for HVs",96,0,2700000,96,0,2700000);
TH2F *GAIN_650 = new TH2F("GAIN for 650V","GAIN for 650V",48,0,2700000,48,500,800);
TH2F *GAIN_700 = new TH2F("GAIN for 700V","GAIN for 700V",48,0,2700000,48,500,800);
 		  
 fstream inpu ("map.txt",ios::in)
 
 while(in>qie>>ieta>>depth){		  
 		  
 		    for(int hv=0; hv<2;hv++){
 		    for(int db=0;db<2;db++){
 		  
 		    if(hv==0){
                       GAIN_650->Fill(GAIN,650);
                       GAIN_650->SetMarkerStyle(8);
                       GAIN_650->SetMarkerColor(kRed);
                       GAIN_650->SetFillColor(2);}

                     if(hv==1){
                       GAIN_700->Fill(GAIN,700);
                       GAIN_700->SetMarkerStyle(8);
                       GAIN_700->SetMarkerColor(kBlue);
                       GAIN_700->SetFillColor(3);}



HVGAIN->Fill(GAIN_650->GetBinContent(qie),GAIN_700->GetBinContent(qie));
HVGAIN->SetFillColor(3);
HVGAIN->SetMarkerColor(kRed);
HVGAIN->GetXaxis()->SetTitle("GAINfor650V");
HVGAIN->GetYaxis()->SetTitle("GAINfor700V");
HVGAIN->SetMarkerStyle(8);
}
}
}
HVGAIN->Draw();

Cheers,
Ersel
h2 and h3.pdf (16.3 KB)
HistByBinContent.pdf (16.5 KB)

To run your program we need your file map.txt
Also make sure you send a running macro. I doubt the last one you posted will run.

Ok. i posted here a part of my macro…
I’ll solved this problem with another method!

If I have any trouble with this problem, I’ll write here at that time…

For now, thanks for your help

Cheers,
Ersel

Ok, I came back here for this issue which is related to creating TH2F by using two TH1F
My script is as in the following ,and runs…

1-The problem is that although the histograms GAIN_650 and GAIN_700 are filled and drew in root file, the content getting with GAIN_650->GetBinContent(qie) consists of zeros ! These histogram here are two TH2F histograms ,and the aim here is filling another TH2F histogram by using them with GetBinContent().

2-The same problem as above is that the content getting with GAIN650->GetBinContent(qie) consists of zeros !
These histogram here are two TH1F histograms ,and the aim here is creating TH2F histogram by using them with GetBinContent()

PS: “map.txt” and “Run_2983.root” are in the attachment being necessary for running the script ‘HFLED.C’
,and HFLED.C runs as root -l ‘HFLED.C(2983)’

Please, could you help me for handling this problem?

#include <iostream>
#include <fstream>

using namespace std;

char rootfile[200];
int qie,depth,ieta;
double GAINfor650,GAINfor700;
double GAIN;
//int runno;                                                                                                                                                                      \
                                                                                                                                                                                   

void HFLED(int runno){

  gStyle->SetOptStat("nemrRM"); // "nemr" is the default                                                                                                                          \
                                                                                                                                                                                   
  sprintf(rootfile,"Run_%d.root",runno);
  TFile *f = new TFile(rootfile);

  stringstream outfile;
  outfile<<runno<<"_Ntuple.root";
  TFile *f1 = new TFile(outfile.str().c_str(),"RECREATE");
  outfile.str("");

  int HV[] = {650,700};

  TCanvas *c3 = new TCanvas("GAIN for HV","GAIN for HV",600,400);
  TCanvas *c4 = new TCanvas("GAINs","GAINs",600,400);

  TH2F *GAINHV;
  TH2F *GAIN_650;
  TH2F *GAIN_700;

  TH1F *GAIN650;
  TH1F *GAIN700;
  TH2F *GAINCorr;

  GAIN_650 = new TH2F("GAIN for 650V","GAIN for 650V",48,500,800,48,0,2700000);
  GAIN_700 = new TH2F("GAIN for 700V","GAIN for 700V",48,500,800,48,0,2700000);
  GAINHV = new TH2F("GAIN for HV","GAIN for HV",96,500,800,96,0,2700000);

  GAIN650 = new TH1F("GAIN650","GAIN650",48,100000,2700000);
  GAIN700 = new TH1F("GAIN700","GAIN700",48,100000,2700000);
  GAINCorr = new TH2F("GAINforHVs","GAINforHVs",96,100000,2700000,96,100000,2700000);

  ifstream input("map.txt");
  if (input.fail()){  cout<<"map.txt is not available"<<endl; }
  int count = 0;
  while( input>>qie>>ieta>>depth ){

    for(int hv=0;hv<2;hv++){
      for(int db=0;db<2;db++) {

        TString histnameLED,histnamePED;
        stringstream ledname,pedname;
        histnameLED.Form("/Analyzer/QIEsSumLED%d_QiECh_%d_DBOX_%d_eta_%d_D_%d",HV[hv],qie,db,ieta,depth);
        histnamePED.Form("/Analyzer/QIEsSumPED%d_QiECh_%d_DBOX_%d_eta_%d_D_%d",HV[hv],qie,db,ieta,depth);
        pedname<<histnamePED;
        ledname<<histnameLED;

        //      cout<<histnameLED<<endl;                                                                                                                                     
        TH1F *HistLED = (TH1F*)f->Get(ledname.str().c_str());
        TH1F *HistPED = (TH1F*)f->Get(pedname.str().c_str());
        ledname.str("");
        pedname.str("");
        //      cout<<ledname.str().c_str()<<endl;                                                                                                                                                                                                                                                                                                                   

        if(HistLED && HistPED) {
          double LEDMEAN;
          double PEDMEAN;
          double LEDRMS;
          double PEDRMS;

          LEDMEAN = HistLED->GetMean();
          PEDMEAN = HistPED->GetMean();
          LEDRMS = HistLED->GetRMS();
          PEDRMS = HistPED->GetRMS();


          double TRUEMEAN = LEDMEAN - PEDMEAN;
          double TRUERMS = sqrt(pow(LEDRMS,2)+pow(PEDRMS,2));
          double NPE = 1.15*pow(TRUEMEAN,2)/pow(TRUERMS,2); // 1.15+-0.05 = ENF(Excess Noise Factor)                                                                              
          double GAIN = TRUEMEAN*2.6*10000/(NPE*1.6);

          if (GAIN>0) {

            GAINHV->Fill(HV[hv],GAIN);
            GAINHV->SetMarkerStyle(8);
            GAINHV->SetMarkerColor(kRed);
            GAINHV->SetFillColor(2);

            if(hv==0){
              GAIN_650->Fill(650,GAIN);
              double content_650 = GAIN_650->GetBinContent(qie);
              cout<<"650_GetBinContent__"<<content_650<<endl;
              GAIN_650->SetMarkerStyle(8);
              GAIN_650->SetMarkerColor(kRed);
              GAIN_650->SetFillColor(2);}

            if(hv==1){
              GAIN_700->Fill(700,GAIN);
              GAIN_700->SetMarkerStyle(8);
              GAIN_700->SetMarkerColor(kBlue);
              GAIN_700->SetFillColor(3);}


            if(hv==0){
              cout<<"650__"<<GAIN<<endl;
              GAIN650->Fill(GAIN);
              double content650 = GAIN650->GetBinContent(qie);
              cout<<"650GetBinContent__"<<content650<<endl;
              GAIN650->SetMarkerColor(kRed);
              GAIN650->SetFillColor(1);
              GAIN650->SetMarkerStyle(8);}

            if(hv==1){
              cout<<"700__"<<GAIN<<endl;
              GAIN700->Fill(GAIN);
              GAIN700->SetMarkerColor(kBlue);
              GAIN700->SetFillColor(2);
              GAIN700->SetMarkerStyle(8);}
             if(hv==1){
              cout<<"700__"<<GAIN<<endl;
              GAIN700->Fill(GAIN);
              GAIN700->SetMarkerColor(kBlue);
              GAIN700->SetFillColor(2);
              GAIN700->SetMarkerStyle(8);}


          }
        }
      }
    }
    GAINCorr->Fill(GAIN650->GetBinContent(qie),GAIN700->GetBinContent(qie));
    GAINCorr->SetMarkerColor(kGreen);
    GAINCorr->SetMarkerStyle(8);
    GAINCorr->SetFillColor(3);
    GAINCorr->GetXaxis()->SetTitle("GAIN650");
    GAINCorr->GetYaxis()->SetTitle("GAIN700");


  }
  c4->cd();
  GAINCorr->Draw("AXIS");
  GAIN650->Draw("same");
  GAIN700->Draw("same");

  c3->cd();
  GAINHV->Draw("AXIS");
  GAIN_650->Draw("same");
  GAIN_700->Draw("same");

  c4->Write();
  c3->Write();

}

Thanks for your help,
Cheers,
Ersel
Run_2983.root (731 KB)
map.txt (241 Bytes)

If I draw GAIN_650 alone I see that it has entries only at X = 650 (try you will see)

You do:

  c3->cd();
  GAINHV->Draw("AXIS");
  GAIN_650->Draw("same"); 
  GAIN_700->Draw("same");

But GAINHV x axis starts at 100 000 … so well above 650 that’s why you see nothing … you should have a close look at you code … it seems there is confusion between GAIN650 and GAIN_650