# Crossing point of two signals(function)

**URL:** <https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805>\
**Category:** Newbie\
**Created:** [April 29, 2019, 9:05am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805 "2019-04-29T09:05:33Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 9:05am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/1 "2019-04-29T09:05:33Z")

</div>

Hi boys,

I’m really glad to start ROOT learning. Would anyone be kind enough to help a beginner ?

Well, i have two plot that i could draw them successfully. Next i draw them at ones Canvas using “same”. Okay, Now i’m looking for the cross point. I mean somewhere that the first signal **cross** the second one. The crossing coordinate is my interest.

I’m beginner and i can share my code if need.

Any help will appreciated. 🌹🌹🌹

---

<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:** [April 29, 2019, 9:10am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/2 "2019-04-29T09:10:02Z")

</div>

What kind of object are you using to draw these two plots ? TH1/D/F ? TGraph ?

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 9:17am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/3 "2019-04-29T09:17:50Z")

</div>

@couet // Thanks a lot for answering. Actually its two digitized signals that i saved the digitized data in a txt file. the txt file consisting a numbers that correspond to ADC heights. look at this :

 ![1](https://root-forum.cern.ch/uploads/default/original/3X/9/e/9eed18c1e79899bf53eef3da256580e73205d0fb.png)  
I saved this signal data to a txt file.  
As i said, i have 2 signal and now i plot them at one canvas

I’m looking for the crossing point.  
Hope to could convey my mean

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 9:21am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/4 "2019-04-29T09:21:14Z")

</div>

@couet // Because I’m new user , i can not share more image.😣  
The txt file is something like this :  
 ![Untitled](https://root-forum.cern.ch/uploads/default/original/3X/2/d/2dbf5feb21be170243f57ff5f3fc9490d68b8d69.png)

I could draw two plot at one canvas and i share it in the next post.

---

<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:** [April 29, 2019, 9:23am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/5 "2019-04-29T09:23:43Z")

</div>

> [@AA360c](#):
>
> i have 2 signal and now i plot them at one canvas

Can you send the ROOT commands you used to produce this plot ? I guess you wrote a macro…

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 9:27am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/6 "2019-04-29T09:27:24Z")

</div>

@couet // This is something that i draw :

 ![Untitled2](https://root-forum.cern.ch/uploads/default/original/3X/4/4/4439ad3542fa9c675310c4ffd800de0f1530ed62.png)

I’m looking for the coordinate of red circular.

This is my root code : ( ofc just root part )

```auto
TApplication app("app", &argc, argv);

TCanvas *Canv1 = new TCanvas("Canv1", "Energy Spectrum");
TH1D *Charge = new TH1D("Charge", "Charge Histogram", 500, 0.0, 10000000.0);
TCanvas *Canv2 = new TCanvas("Canv2", "Timing");
TH1I *RisingTime = new TH1I("RisingTime", "RisingTime Histogram", 1024, -1024, 1024);
TCanvas *Canv3 = new TCanvas("Canv3", "Signlas");
TCanvas *Canv4 = new TCanvas("Canv4", "CrossPoint");

 Charge->Fill(Q);
 RisingTime->Fill(0.9*RiseTime * 2);

TGraph *Plot1 = new TGraph("Output1.txt");
TGraph *Plot2 = new TGraph("Output2.txt");
TGraph *Plot3 = new TGraph("Output3.txt");

Canv1->cd();
Charge->Draw();
		
Canv2->cd();
RisingTime->Draw();
		
Canv3->Divide(1, 2);

Canv3->cd(1);
Plot1->Draw();

Canv3->cd(2);
Plot2->Draw();
		
Canv4->cd();
Plot2->Draw();
Plot1->Draw("Same");

Canv1->Update();
Canv2->Update();
Canv3->Update();
Canv4->Update();

```

In addition i dont know what do u mean by saying **“macro”**.

---

<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:** [April 29, 2019, 10:19am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/7 "2019-04-29T10:19:19Z")

</div>

may be you can try this:  
[https://root.cern.ch/doc/master/namespaceROOT\_1\_1VecOps.html#a2e903e9bed20c05d7a70afb393355643](https://root.cern.ch/doc/master/namespaceROOT_1_1VecOps.html#a2e903e9bed20c05d7a70afb393355643)

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 10:47am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/8 "2019-04-29T10:47:38Z")

</div>

@couet // Thanks, But could not grasp and applied it in my c++ code . 😥

should i use plot1\_intersect\_plot2 ?

---

<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:** [April 29, 2019, 11:00am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/9 "2019-04-29T11:00:50Z")

</div>

can you send me the two file Output1.txt Output2.txt ?

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 11:11am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/11 "2019-04-29T11:11:03Z")

</div>

@couet // Ofcourse, really thanks. I use ROOT through Visual studio.

Forgive me to taking your time, as i said first, i’m beginner.

[Output1.txt](https://root-forum.cern.ch/uploads/default/original/3X/3/0/30f3c84d21975997ed42140ff12916312ec1b288.txt) (999.3 KB)  
[Output2.txt](https://root-forum.cern.ch/uploads/default/original/3X/4/a/4adc81e208e7192b9fb0a04ac48f0b0f03e848f4.txt) (1.1 MB)

---

<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:** [April 29, 2019, 11:49am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/12 "2019-04-29T11:49:59Z")

</div>

some idea:

```auto
void intersect() {
   auto Plot1 = new TGraph("Output1.txt");
   auto Plot2 = new TGraph("Output2.txt");

   Plot2->Draw("AL");
   Plot1->Draw("L");

   double min = 33000;
   double max = 35000;
   double d = 1.;
   double eps = 1.;

   for (double s=min; s<max; s=s+d) {
      if (TMath::Abs(Plot2->Eval(s)-Plot1->Eval(s)) < eps) {
         printf("Plot1 and Plot2 might intersect at (%g, %g)\n",s,Plot2->Eval(s));
      }
   }
}

```

the execution of this macro gives:

```auto
$ root intersect.C 
   ------------------------------------------------------------
  | Welcome to ROOT 6.17/01 https://root.cern |
  | (c) 1995-2019, The ROOT Team |
  | Built for macosx64 on Apr 29 2019, 11:02:08 |
  | From heads/master@v6-16-00-rc1-1795-ga98d98cf5b |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------
root [0] 
Processing intersect.C...
Info in <TCanvas::MakeDefCanvas>: created default TCanvas with name c1
Plot1 and Plot2 might intersect at (34284, 15)
Plot1 and Plot2 might intersect at (34285, 16)
root [1] 

```

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 12:13pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/13 "2019-04-29T12:13:03Z")

</div>

@couet // i encounter with this eror : 🤐

 ![Untitle222d](https://root-forum.cern.ch/uploads/default/original/3X/6/0/60e9bbba0fbd224725b5932da1a5f7fdd682df25.png)

---

<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:** [April 29, 2019, 12:16pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/14 "2019-04-29T12:16:29Z")

</div>

did you execute the macro exactly as I did ? … it is not clear on your screen dump

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 12:21pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/15 "2019-04-29T12:21:07Z")

</div>

I copy all of thing u wrote here and i saved as “ **intersect.C** ” file in root/bin.  
Then i drag it and open with root shortcut on my desktop .

Did i made a mistake ?

Seems program fall in and endless for loop, I dont know…

It says illegal number of point, Why ? how could u run it with no error ?

---

<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:** [April 29, 2019, 12:27pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/16 "2019-04-29T12:27:01Z")

</div>

the two files Output1.txt and Output2.txt must both be in the folder where intersect.C is

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 12:30pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/17 "2019-04-29T12:30:55Z")

</div>

Indeed, I know. But it’s not the problem .😥

---

<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:** [April 29, 2019, 12:37pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/18 "2019-04-29T12:37:50Z")

</div>

It seems your graphs have an invalid number of points… so the graph are not created properly… It might be that the txt files are not found… Can you send the beginning of the log ? … Just after .x intersect.C ?

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 29, 2019, 12:47pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/19 "2019-04-29T12:47:28Z")

</div>

@couet // Omg, unbelievable…  
You know, when i run ROOT and then command **.x fileopen.c** and in the next when root object browser opened and i choose intersect.c it work correctly !!!

in contrast, when i just drag intersect and put on root icon, its going to error !!! cant understand whats wrong. both way are the same.

 ![aaa](https://root-forum.cern.ch/uploads/default/original/3X/3/a/3a94544ccd8acd6c5263e41617e4cc3b24a55e2e.jpeg)

Could the reason be this that i using old version of ROOT ?

```auto

  *******************************************
  * *
  * W E L C O M E to R O O T *
  * *
  * Version 5.34/36 5 April 2016 *
  * *
  * You are welcome to visit our Web site *
  * http://root.cern.ch *
  * *
  *******************************************
ROOT 5.34/36 (v5-34-36@v5-34-36, Apr 05 2016, 10:25:45 on win32)
CINT/ROOT C/C++ Interpreter version 5.18.00, July 2, 2010
Type ? for help. Commands must be C++ statements.
Enclose multiple statements between { }.
root [0]

```

---

<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:** [April 29, 2019, 1:07pm UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/20 "2019-04-29T13:07:43Z")

</div>

Look at your browser… the macro intersect.C and the two txt file are in the same directory. When you run “.x intersect.C” from the root prompt you must first cd to that directory before starting root.

---

<div class="post-metadata">

**Author:** ![AA360c](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aa360c/32/18471_2.png) [@AA360c](https://root-forum.cern.ch/u/AA360c)\
**Post date:** [April 30, 2019, 10:02am UTC](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805/21 "2019-04-30T10:02:08Z")

</div>

@couet // Thanks a lot for your guidance. I do anything u said but, i encounter with the same error as i showed before. At last i could not grasp whats wrong, however, The only way to run intersect.c here, is that open it with ROOT " **filebrowser.C**".

Thank you anyway.

[Next page](https://root-forum.cern.ch/t/crossing-point-of-two-signals-function/33805.md?page=2)
