Check if element is in array of TGraph X values

Hello!

I know this might not be linked necessarily to a root problem, but I have issues implementing this.
I am trying to check if there is an element in the array of X values from a TGraph. I do this with begin/end functions.



#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end

#include <iostream>
using namespace std;

int t()
{
    TFile *IPRfile = new TFile(64080.IPR.root);
    TGraphErrors* g = (TGraphErrors*)IPRfile->Get("IPRchargeTelType0");

     const int n_iprg = g->GetN();
     Double_t * arrx =  g->GetX();

     //here I tried retrieving the content of the array X into a type double
     Double_t arr[n_iprg];
     for (i=0; i<n_iprg; i++){
      arr[i] = arrx[i];
     }

    bool exists = std::find(std::begin(arr), std::end(arr), target) != std::end(arr);
    if (exists) {
        std::cout << "Element found";
    } else {
        std::cout << "Element not found";
    }

    return 0;
}

But this gives me an error (error: no matching function for call to ‘end’), which is related to the type of variable that is being passed to the function. My question is: how can I get the array of X values from a TGraph in type double, so that I can pass it to this function?

Passing a simple double like double arr2[4] = {6, 7, 8.7, 4.5}; works fine.

64080.IPR.root (112.6 KB)

Hi,
I changed your code a bit.
Since you are already looping over the X vector you check inside the loop for your target.
I added also a tolerance value eps , so you do not need to insert the exact value for your target.
Obviously setting eps =0, your target should be in the x vector

int t()
{
    TFile *IPRfile = new TFile("64080.IPR.root");
    TGraphErrors* g = (TGraphErrors*)IPRfile->Get("IPRchargeTelType0");

     const int n_iprg = g->GetN();
     
    bool exists=false;
    
     //here I tried retrieving the content of the array X into a type double
    double target=18.9486;
    double eps=1e-2;
    int i=0;
     do {
      if(abs(target-g->GetPointX(i))<eps)
          exists=true;
         i++;
     }while(!exists && i<n_iprg);

    
    if (exists) {
        std::cout << "Element found";
    } else {
        std::cout << "Element not found";
    }

    return i++;
}


int t()
{
    TFile *IPRfile = new TFile("64080.IPR.root");
    TGraphErrors* g = (TGraphErrors*)IPRfile->Get("IPRchargeTelType0");

    const int n_iprg = g->GetN();
    double * arrx =  g->GetX();

//    double target = 6.0; // should not be found
    double target = 31.05; // should be found
    bool exists = std::find(arrx, arrx+n_iprg, target) != arrx+n_iprg;
    if (exists)
        std::cout << "Element found" << std::endl;
    else
        std::cout << "Element not found" << std::endl;

    return 0;
}

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