# Using a predefined TH2D in RDataframe to compute new quantiy

**URL:** <https://root-forum.cern.ch/t/using-a-predefined-th2d-in-rdataframe-to-compute-new-quantiy/50881>\
**Category:** Newbie\
**Tags:** rdataframe\
**Created:** [July 20, 2022, 4:07pm UTC](https://root-forum.cern.ch/t/using-a-predefined-th2d-in-rdataframe-to-compute-new-quantiy/50881 "2022-07-20T16:07:21Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![RENATO\_QUAGLIANI](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/renato_quagliani/32/19496_2.png) [@RENATO\_QUAGLIANI](https://root-forum.cern.ch/u/RENATO_QUAGLIANI)\
**Post date:** [July 20, 2022, 9:27pm UTC](https://root-forum.cern.ch/t/using-a-predefined-th2d-in-rdataframe-to-compute-new-quantiy/50881/2 "2022-07-20T21:27:35Z")

</div>

> [@DataFrame define column with "input" histogram](https://root-forum.cern.ch/t/dataframe-define-column-with-input-histogram/34143):
>
> Dear experts I gave a first trial to “fill” a weight bracnh reading a histogram and a couple of variables on my ntuple… The code i came up and make it run/work is something like this : TH1D \* \_histo = new TH1D( "dummy","dummy", 100,0.,100); auto GetHistogramVal = [&\_histo](double \_variable){ double \_val = std::numeric\_limits\<float\>::min(); int \_bin = \_histo-\>FindBin(\_variable); if (\_histo-\>IsBinUnderflow(\_bin) || \_histo-\>IsBinOverflow(\_bin)) { if (\_variable \<…

Maybe this can be helpful  
There are shorter and clever tricks than the one posted here, for example you can simply make a struct implementing a member being your 2D histo to read and one operator() implementation, or even better maybe and simple to use lambda capturing the histogram as argument

```auto
 auto reweight=[& inputHist]( double X, double y){
....
};
node = node.Define( "w", reweight,{ "col1","col2"})

```

( Personally I prefer to declare a functor class to do this since then I can compile and make dictionary for it and use in pyROOT for other cases I need it)

. The only thing to be careful is that you use only const methods on the TH2D object in the operator() implementation.  
(E.g FindFixBin, GetBinContent, check out the doxygen if the methods you use are marked `const` for your Root version.

---

_[View the full topic](https://root-forum.cern.ch/t/using-a-predefined-th2d-in-rdataframe-to-compute-new-quantiy/50881)._
