# Plotting the differences between two graphs

**URL:** <https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009>\
**Category:** ROOT\
**Created:** [May 14, 2018, 3:11pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009 "2018-05-14T15:11:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [May 14, 2018, 3:11pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/1 "2018-05-14T15:11:39Z")

</div>

Please provide the following information:

* * *

ROOT Version (e.g. 6.12/02): 6.12/04  
Platform, compiler (e.g. CentOS 7.3, gcc6.2): CENTOS 7

* * *

Hello I would like to plot differences between two graphs that have drawn with TGraph.

Is there any way to do this? I tried by adding “Add(fluka\_lead,-1)” but it is not working

Just to be understood, I attach the code. Be warned it is bit messy because of different binning between two difference sets of data

```auto
Float_t E1box1, E2box1 , Ybox1, EYbox1;

 Double_t EnergyBox1[459];
 Double_t eEnergyBox1[459];
 Double_t FluenceBox1[459];
 Double_t eFluenceBox1[459];
 // per primary
 double Norm = 1.0;
 double sum=0;
 double esum=0;
  ifstream infile("fluka_lead.data");
  double emin=0;
  double emax=0;
for (int i=0; i<458; i++){
  infile >> E1box1 >> E2box1 >> Ybox1 >> EYbox1;
// printf("%g\n",E1box1/E2box1);
  if(!i) emin = E1box1;
  if(i==457) emax = E2box1;
  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);
  sum+=Ybox1*(E2box1-E1box1);
}
infile.close();
  printf("sum = %g\n",sum);

  TGraphErrors* fluka_lead = new TGraphErrors(458,EnergyBox1,FluenceBox1,eEnergyBox1,eFluenceBox1);
 fluka_lead->SetMarkerSize(0.8);
 fluka_lead->SetMarkerColor(kRed);
 fluka_lead->SetMarkerStyle(20);
 fluka_lead->SetLineColor(kRed);
 fluka_lead->Draw("PLZ");

// printf("%g, %g\n",EnergyBox1[0],EnergyBox1[356]);

 TGraph *geant_lead = new TGraph();
 TH1F *h1 = new TH1F("f","G4 results",458,log(emin),log(emax));
 ifstream file1("lead_geant4.txt");
int i=0;
double x1;
int nlines = 0;
  while(!file1.eof()) {
    nlines++;
    file1>>x1;
    h1->Fill(log(x1*1e-3));}
 double integral = nlines/20000.;

 sum=0;
 double deltalog = (log(emax)-log(emin))/458.;
 for(int i=0;i<458;++i) {
     double elow = exp(log(emin)+deltalog*i);
     double ehigh = exp(log(emin)+deltalog*(i+1));
     sum += h1->GetBinContent(i)*(log(ehigh)-log(elow));
    // printf("%g %g\n",x,h1->GetBinContent(i));
  }
 printf("sum=%g\n",sum);
for(int i=0;i<458;++i) {
     double x = exp(h1->GetBinCenter(i));
     double elow = exp(log(emin)+deltalog*i);
     double ehigh = exp(log(emin)+deltalog*(i+1));
     geant_lead->SetPoint(i,x,h1->GetBinContent(i)*integral/sum);
 }
 geant_lead->SetMarkerColor(kBlue);
 geant_lead->SetMarkerStyle(21);
 geant_lead->SetLineColor(kBlue);
 geant_lead->SetMarkerSize(0.8);
 geant_lead->Draw("PLZ");
 h1->SetMaximum(100);
 h1->SetMinimum(1e-9);
 TCanvas *c2 = new TCanvas();
 c2->SetLogy();
 h1->Draw();

  TCanvas *c3 = new TCanvas();
  geant_lead->Add(fluka_lead,-1)

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 14, 2018, 3:22pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/2 "2018-05-14T15:22:52Z")

</div>

Reading your code it is not completely clear where/how you are doing “the difference between two graphs”. Do you want to do the difference between `geant_lead` and `fluka_lead` ? I so I would guess that it would be enough to create a new TGraph called `diff` and to set its points as: `diff[i] = geant_lead[i]-fluka_lead[i]`.

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [May 14, 2018, 5:30pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/3 "2018-05-14T17:30:47Z")

</div>

Yes that is correct.

Thank you . I know the code is complex. It is because of the sorting out the different binning when i plot the data.

Thank you for your insight. I thought there is a way like using Add(fluka\_lead,-1)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 14, 2018, 7:19pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/4 "2018-05-14T19:19:28Z")

</div>

Note that the solution proposed by Olivier assumes that both graphs have exactly the same numbers of points and that their X coordinates are exactly the same (for all points).

If any of the above conditions is not met, try:

```auto
{
  // Note: this macro needs ROOT 6
  
  // define some variables used below
  Double_t x[100], y[100];
  Int_t n;
  
  // create the first graph
  n = 22;
  for (Int_t i = 0; i < n; i++) {
    x[i] = i * TMath::Pi() / (n - 1.);
    y[i] = TMath::Sin(x[i]);
  }
  TGraph *geant_lead = new TGraph(n, x, y);
  geant_lead->SetTitle("geant_lead;X;Y");
  
  // create the second graph
  n = 33;
  for (Int_t i = 0; i < n; i++) {
    x[i] = i * TMath::Pi() / (n - 1.);
    y[i] = TMath::Cos(x[i]);
  }
  TGraph *fluka_lead = new TGraph(n, x, y);
  fluka_lead->SetTitle("fluka_lead;X;Y");
  
  // create the difference graph
  std::vector<Double_t> v;
  v.insert( v.end(), geant_lead->GetX(), (geant_lead->GetX() + geant_lead->GetN()) );
  v.insert( v.end(), fluka_lead->GetX(), (fluka_lead->GetX() + fluka_lead->GetN()) );
  std::sort( v.begin(), v.end() );
  v.erase( std::unique( v.begin(), v.end() ), v.end() );
  n = v.size();
  TGraph *diff_lead = new TGraph(n);
  for (Int_t i = 0; i < n; i++)
    diff_lead->SetPoint( i, v[i], (geant_lead->Eval(v[i]) - fluka_lead->Eval(v[i])) );
  diff_lead->SetTitle("geant_lead - fluka_lead;X;#Delta Y");
  
  // draw everything
  TCanvas *c = new TCanvas("c", "c");
  c->Divide(1, 3);
  c->cd(1);
  geant_lead->Draw("AL*");
  c->cd(2);
  fluka_lead->Draw("AL*");
  c->cd(3);
  diff_lead->Draw("AL*");
  c->cd(0);
}

```

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [May 15, 2018, 8:13am UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/5 "2018-05-15T08:13:24Z")

</div>

Thank you so much. I will try the string that you used in last few lines introducing new TGraphs.

Several posts referred to be using Add ( x , -1); to subtract two graphs but it did not work for me.

Thank you

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 15, 2018, 8:35am UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/6 "2018-05-15T08:35:09Z")

</div>

> [@sclee0620](#):
>
> Several posts referred to be using Add ( x , -1); to subtract two graphs

The TGraph class does not have an “Add” method . Which “Add” are you talking about ?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 15, 2018, 9:42am UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/7 "2018-05-15T09:42:39Z")

</div>

I think people quite often mix [graphs](https://root.cern.ch/doc/master/classTGraph.html) with [histograms](https://root.cern.ch/doc/master/classTH1.html#a6da1513d03e7352438bd8356f5038e37).

---

<div class="post-metadata">

**Author:** ![sclee0620](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sclee0620](https://root-forum.cern.ch/u/sclee0620)\
**Post date:** [May 15, 2018, 1:25pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/8 "2018-05-15T13:25:42Z")

</div>

The Add in histogram I guess as Wile\_E\_Coyote suggested.

Thank you

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [May 15, 2018, 1:26pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/9 "2018-05-15T13:26:52Z")

</div>

> [@sclee0620](#):
>
> The Add in histogram I guess

Ok … but you are working with TGraph …So there is no Add()

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 29, 2018, 1:39pm UTC](https://root-forum.cern.ch/t/plotting-the-differences-between-two-graphs/29009/10 "2018-05-29T13:39:40Z")

</div>

Closed on May 29, 2018, 1:39pm UTC

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