# Passing arguments to function called by .Define in RDataFrame

**URL:** <https://root-forum.cern.ch/t/passing-arguments-to-function-called-by-define-in-rdataframe/42208>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [November 10, 2020, 5:42pm UTC](https://root-forum.cern.ch/t/passing-arguments-to-function-called-by-define-in-rdataframe/42208 "2020-11-10T17:42:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bzalewsk](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bzalewsk/32/19716_2.png) [@bzalewsk](https://root-forum.cern.ch/u/bzalewsk)\
**Post date:** [November 10, 2020, 5:42pm UTC](https://root-forum.cern.ch/t/passing-arguments-to-function-called-by-define-in-rdataframe/42208/1 "2020-11-10T17:42:28Z")

</div>

Dear all

I have to calibrate data in my columns with a linear function. I would like to that with the use of a reusable function. In order to do that, I am looping .Define, as explained in [this post](https://root-forum.cern.ch/t/running-rdataframes-define-in-for-loop/32484/3). The problem I have now is how to pass information about which column is being called to my calibrating function, in order to get proper calibration parameters.

Calibrating Function below

```auto
ROOT::VecOps::RVec<float> calibrateMe(ROOT::VecOps::RVec<unsigned short> &inputArray)
{
	auto mySize = array.size();
	ROOT::VecOps::RVec<float> outputArray(mySize);
	for (size_t iii = 0; iii < mySize; iii++)
	{
		outputArray.at(iii) = B[iii] + A[iii] * inputArray[iii];
	}
	return outputArray;
}

```

Looped Define function

```auto
ROOT::RDF::RNode ApplyDefines(					
					ROOT::RDF::RNode df, 
					const std::vector<std::string> &colNames,
               		unsigned int iii = 0)
{
	if (iii == colNames.size())
	{
		return df;
	}

	std::string newColumn = colNames[iii];
	inputColumn.insert(0, cal_);
	return ApplyDefines(df.Define(newColumn.c_str(), calibrateMe, {colNames[iii].c_str()}), colNames, iii + 1);
}

```

I figured out that I could modify the first entry in the input tree to contain some ID, but that doesn’t seem to be an elegant solution. Does anybody have any idea?

Cheers,  
Bogumił

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ 6.22/00  
_Platform:_ Ubuntu 18.04  
_Compiler:_ gcc version 7.5.0

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [November 10, 2020, 5:57pm UTC](https://root-forum.cern.ch/t/passing-arguments-to-function-called-by-define-in-rdataframe/42208/2 "2020-11-10T17:57:45Z")

</div>

Hi @bzalewsk,  
and welcome to the ROOT forum!

If I understand the task correctly, the simplest solution is probably to make `calibrateMe` an object, with the current implementation of `calibrateMe` that can be just copy-pasted in the object’s `operator()`:

```auto
struct Calibrator {
   Calibrator(std::string columnToCalibrate) { ... };

   ROOT::VecOps::RVec<float>
   operator()(ROOT::VecOps::RVec<unsigned short> &inputArray) {
      	auto mySize = array.size();
        ROOT::VecOps::RVec<float> outputArray(mySize);
        for (size_t iii = 0; iii < mySize; iii++)
           outputArray.at(iii) = B[iii] + A[iii] * inputArray[iii];
        return outputArray;
   }
};

```

and then you create `Calibrator`s as needed in `ApplyDefines`:

```cpp
return ApplyDefines(df.Define(newColumn.c_str(), Calibrator(colNames[iii]), {colNames[iii]}), colNames, iii + 1);

```

(also note that you should not need the `c_str()`.

Hope this helps!  
Enrico

---

<div class="post-metadata">

**Author:** ![bzalewsk](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bzalewsk/32/19716_2.png) [@bzalewsk](https://root-forum.cern.ch/u/bzalewsk)\
**Post date:** [November 10, 2020, 7:01pm UTC](https://root-forum.cern.ch/t/passing-arguments-to-function-called-by-define-in-rdataframe/42208/3 "2020-11-10T19:01:38Z")

</div>

It’s working like a charm! Thank you very much for your help.

Bogumił Zalewski
