# How to Pass a TTreeReaderArray\<float\> to a function

**URL:** <https://root-forum.cern.ch/t/how-to-pass-a-ttreereaderarray-float-to-a-function/58084>\
**Category:** Newbie\
**Created:** [February 7, 2024, 3:53am UTC](https://root-forum.cern.ch/t/how-to-pass-a-ttreereaderarray-float-to-a-function/58084 "2024-02-07T03:53:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GorgeGrouse](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/gorgegrouse/32/35631_2.png) [@GorgeGrouse](https://root-forum.cern.ch/u/GorgeGrouse)\
**Post date:** [February 7, 2024, 3:53am UTC](https://root-forum.cern.ch/t/how-to-pass-a-ttreereaderarray-float-to-a-function/58084/1 "2024-02-07T03:53:48Z")

</div>

I want to pass a TTreeReaderArray\<float\> to a function, and I haven’t been able to find the relevant documentation.

Here’s my function:

```auto
TVector3 findJetMatchReconToGen(TTreeReaderArray<float> genMomArrayX, TTreeReaderArray<float> genMomArrayY, TTreeReaderArray<float> genMomArrayZ, TTreeReaderArray<int> typeArray, TVector3 reconMom) {
  for(unsigned int q=0; q<genMomArrayX.GetSize(); q++){
	    if(typeArray[q] == 0){ //not a particle
	      TVector3 genMom(genMomArrayX[q],genMomArrayY[q],genMomArrayZ[q]);
	      double minDR = 999.;
	      double jetGenMatchIndex = 0;
	      for(unsigned int r=0; r<genMomArrayX.GetSize(); r++) {
		double deltaEta = TMath::Abs(reconMom.PseudoRapidity()-genMom.PseudoRapidity());
		double deltaPhi = TMath::Abs(reconMom.Phi()-genMom.Phi());
		double deltaR = TMath::Sqrt(deltaEta*deltaEta + deltaPhi*deltaPhi);
		if (deltaR < minDR) {
		  minDR = deltaR;
		  jetGenMatchIndex = r;
		}
	      }
         TVector3 matchingGenJet(genMomArrayX[jetGenMatchIndex], genMomArrayY[jetGenMatchIndex], genMomArrayZ[jetGenMatchIndex]); 
	      return matchingGenJet;
	    }
  }       
}

```

I originally got this compiler error:

```auto
 error: could not convert ‘& jetGenMomX’ from ‘TTreeReaderArray<float>*’ to ‘TTreeReaderArray<float>’

```

but when I define the function to have pointer arguments and change the functions to pointer syntax, I get a very long series of error messages that are mostly some form of

```auto
error: no match for ‘operator==’ (operand types are ‘TTreeReaderArray<int>’ and ‘int’)
  562 | if(typeArray[q] == 0){ //not a particle
      | ~~~~~~~~~~~~ ^~ ~
      | | |
      | | int
      | TTreeReaderArray<int>

```

which is confusing, because I use the same syntax in my main() function without issue.  
I also tried to pass the arrays by reference, but the class already does that automatically so it didn’t change anything.  
Here’s the parts relevant to the TChain and TTreeReader declaration of the arrays I want to pass to the function.

```auto
TChain *mychain = new TChain("events");
mychain->Add(infile);
TFile *ofile = TFile::Open(output,"recreate");
TTreeReader tree_reader(mychain);

```

```auto
TTreeReaderArray<float> jetGenMomX = {tree_reader, "GeneratedJets.momentum.x"};
TTreeReaderArray<float> jetGenMomY = {tree_reader, "GeneratedJets.momentum.y"};
TTreeReaderArray<float> jetGenMomZ = {tree_reader, "GeneratedJets.momentum.z"};

```

_ROOT Version:_ Not Provided  
_Platform:_ Emacs  
_Compiler:_ ePIC-analysis

* * *

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [February 7, 2024, 4:53am UTC](https://root-forum.cern.ch/t/how-to-pass-a-ttreereaderarray-float-to-a-function/58084/2 "2024-02-07T04:53:57Z")

</div>

Hi,

I think a very good practice would be to pass these objects by reference also to avoid copies:

```auto
TVector3 findJetMatchReconToGen(TTreeReaderArray<float>& genMomArrayX, TTreeReaderArray<float>& genMomArrayY, TTreeReaderArray<float>& genMomArrayZ, TTreeReaderArray<int>& typeArray, TVector3& reconMom)

```

Best,  
Danilo
