# Y correction in a correlation plot

**URL:** <https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205>\
**Category:** Newbie\
**Created:** [January 21, 2023, 8:15pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205 "2023-01-21T20:15:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 21, 2023, 8:15pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/1 "2023-01-21T20:15:22Z")

</div>

Hi all, I have plotted a Y vs X and then fitted the plot with a straight line. Now, ideally, there shouldn’t be any correlation b/w Y and X but I can see a slope (let’s say m1) in the fitted line so wanted to make a small correction in the Y ONLY. (something like Y\_new = Y \*/+ some factor)

that means, **after correcting the Y** , the newly fitted line in the **Y\_new vs X** will have a slope zero. Now I understand that I have to rotate the fitted line by some angle about a point (x0,𝑦0) in the +/- direction to get the Y\_new. (seems like choosing the point can also be tricky but for the moment let’s take any point on either end of the boundary) But not sure how can I achieve this mathematically and in the code as well. Sorry if it seems trivial, I am kind of confused about it. A part of the plotting routine and fitting of the plot is given below

```auto
 ch1->Draw("(Y1-Y2): 1/TMath::Sqrt(X)>>hist1","","colz");
	hist1->Fit("fit1","","",0.033,0.039); 
	
	cout << "slope1"<<"\t"<< fit1->GetParameter(1)<< endl;
	double slope1 = fit1->GetParameter(1);

```

and the fitted line looks like the image attached:

 ![FitROOT](https://root-forum.cern.ch/uploads/default/original/3X/9/1/914afcfe3535d4490758fdd6758bae43f0604cfa.png)

Do let me know if you have any suggestions! Thanks!!

---

<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:** [January 21, 2023, 8:39pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/2 "2023-01-21T20:39:21Z")

</div>

Try:

`ch1->Draw(TString::Format("(Y1 - Y2) - (%g / TMath::Sqrt(X)) : 1. / TMath::Sqrt(X)", slope1), "", "colz");`

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 23, 2023, 11:06pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/3 "2023-01-23T23:06:49Z")

</div>

thanks for your note. unfortunately, it didn’t work. Also, I have tried similar corrections before; mostly meaningful hits and trials method; adding or multiplying constants. Needless to say, didn’t work either. However, I was looking for a more generalized way to rotate the fitted line regardless of the initial positive or negative slope. kind of Euler rotations that will correct the Y. I hope it makes some sort of sense.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [January 24, 2023, 5:04am UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/4 "2023-01-24T05:04:34Z")

</div>

If you want a horizontal line from that fit, the first problem is where do you want that horizontal line; if the fit is `y = a*x + b`, do you want `y=b`? or at what point of the original line? Or just fit a horizontal line to the data in the first place.

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 24, 2023, 11:54pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/5 "2023-01-24T23:54:08Z")

</div>

_` where do you want that horizontal line;`_

the point on either end would do and rotate the line accordingly. Like if there is a negative slope to the correlation and we choose left most point OR right most point of the original fit then the rotation would be counter-clockwise to make it Y\_new = b.  
Similarly, if the original fit has a positive slope, the rotation would be clockwise w.r.t both endpoints of the fit to make it Y\_new = b. Makes sense?

---

<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:** [January 25, 2023, 8:05am UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/6 "2023-01-25T08:05:37Z")

</div>

> [@Y correction in a correlation plot](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/2):
>
> Try: ch1-\>Draw(TString::Format("(Y1 - Y2) - (%g / TMath::Sqrt(X)) : 1. / TMath::Sqrt(X)", slope1), "", "colz");

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 25, 2023, 1:09pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/7 "2023-01-25T13:09:36Z")

</div>

@Wile_E_Coyote I already said above, I tried this.

> [@Y correction in a correlation plot](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/3):
>
> thanks for your note. unfortunately, it didn’t work. Also, I have tried similar corrections before; mostly meaningful hits and trials method; adding or multiplying constants. Needless to say, didn’t work either. However, I was looking for a more generalized way to rotate the fitted line regardless of the initial positive or negative slope. kind of Euler rotations that will correct the Y. I hope it makes some sort of sense.

It’s doing a bit of overcorrection; initially, I started with a negative slope after this correction now the correlation is positively sloped.

---

<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:** [January 25, 2023, 1:15pm UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/8 "2023-01-25T13:15:55Z")

</div>

My assumption was that the “`slope1`” came from the original fit (in the first post). If this value is not “right”, you need to play with it.

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [January 26, 2023, 5:07am UTC](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/9 "2023-01-26T05:07:21Z")

</div>

Your assumption is right. ‘Slope1’ is from the original fit and I was trying to correct the new\_Y based on Y\_old - slope1/sqrt(x); Isn’t that what this suggestion of yours means?

> [@Y correction in a correlation plot](https://root-forum.cern.ch/t/y-correction-in-a-correlation-plot/53205/2):
>
> Try: ch1-\>Draw(TString::Format("(Y1 - Y2) - (%g / TMath::Sqrt(X)) : 1. / TMath::Sqrt(X)", slope1), "", "colz");

or am I missing something here?
