Setting bin size from previous graph to new histogram

Hello I have ploted with TGraph for the first graph and with the binning of x axis (e.g EnergyBox1) in this case,

I would like to fill the histogram with the same x axis binning but I have failed.

Could anyone help me with this?
here is the following code:

 
  Float_t  E1box1, E2box1 , Ybox1, EYbox1;

 Float_t EnergyBox1[358];
 Float_t eEnergyBox1[358];
 Float_t FluenceBox1[358];
 Float_t eFluenceBox1[358];
 // per primary 
 double Norm = 1.0;
 double sum=0;
 double esum=0;
  ifstream infile("lead_fluka.data");
for (int i=0; i<357; i++){
  infile >>  E1box1 >> E2box1 >> Ybox1 >> EYbox1;
  EnergyBox1[i] = (E2box1+E1box1)/2;
  eEnergyBox1[i] = 0;
  FluenceBox1[i] = Norm*Ybox1*(E2box1-E1box1)/(log(E2box1)-log(E1box1));
  eFluenceBox1[i] = Norm*0.01*EYbox1*Ybox1*(E2box1-E1box1);
endl;
  sum+=FluenceBox1[i];
}
infile.close();
  
  TGraphErrors* fluka_lead = new TGraphErrors(357,EnergyBox1,FluenceBox1,eEnergyBox1,eFluenceBox1);
 fluka_lead->SetMarkerSize(0.8);
 fluka_lead->SetMarkerColor(2);
 fluka_lead->SetMarkerStyle(20);
 fluka_lead->Draw("PLZ");
  
 
 int points_1 = 587289;
ifstream file1("lead_raw.txt");
TFile *f = new TFile("basic.root","RECREATE");
TH1F *h1 = new TH1F("lead","" ,358,1e-14,100);
Float_t x1,x2,x3;
  for (Int_t i=0; i<points_1; i++){
    file1>>x1;
    x1=log(x1);
    h1->SetBinContent(EnergyBox1,x1);}
  Float_t scale_1 = 2E-4;
  h1->Scale(scale_1);
  h1->SetTitle("The n/p produced from various spallation targets with 1GeV proton beam (QGSP_BERT_HP)");
 h1->Draw("hist same");

Basically I put SetBinContent to fill the x1 which is straight from the data point (the data is single column for histogram). The energybox has been already calculated from previous graph.

The error message says like this

/result_root.C:64:9: error: no matching member function for call to 'SetBinContent'
    h1->SetBinContent(EnergyBox1,x1);}
    ~~~~^~~~~~~~~~~~~
/root_build/include/TH1.h:339:21: note: candidate function not viable: no known conversion from 'Float_t [358]' to 'Int_t' (aka 'int') for 1st argument
   virtual void     SetBinContent(Int_t bin, Double_t content);
                    ^
/root_build/include/TH1.h:340:21: note: candidate function not viable: requires 3 arguments, but 2 were provided
   virtual void     SetBinContent(Int_t bin, Int_t, Double_t content) { SetBinContent(bin, content); }
                    ^
/root_build/include/TH1.h:341:21: note: candidate function not viable: requires 4 arguments, but 2 were provided
   virtual void     SetBinContent(Int_t bin, Int_t, Int_t, Double_t content) { SetBinContent(bin, content); }

The first parameter of SetBinContent is an int … not an array. That what the error message tells you.

yes I understand. Is there any way to fill it like previous graph does?

Thank you

FillN allows to fill an histogram from an array.

Thank you so much :slight_smile:

unfortunately I still have similar error :frowning:

can you post what you get ?

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