# Taking the absolute value of all the entries in a TBranch in RooFit

**URL:** <https://root-forum.cern.ch/t/taking-the-absolute-value-of-all-the-entries-in-a-tbranch-in-roofit/37253>\
**Category:** Newbie\
**Tags:** roofit\
**Created:** [December 18, 2019, 4:51pm UTC](https://root-forum.cern.ch/t/taking-the-absolute-value-of-all-the-entries-in-a-tbranch-in-roofit/37253 "2019-12-18T16:51:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Federico\_G](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecd19e/32.png) [@Federico\_G](https://root-forum.cern.ch/u/Federico_G)\
**Post date:** [December 18, 2019, 4:51pm UTC](https://root-forum.cern.ch/t/taking-the-absolute-value-of-all-the-entries-in-a-tbranch-in-roofit/37253/1 "2019-12-18T16:51:55Z")

</div>

* * *

Hi I have a million entries in a TBranch and the value for each entry ranges from -2.5 to 2.5. I need to cut another set of data using these entries. Is there any way to turn take the absolute value of all the entries in the TBranch? This is the code I’ve been trying to have work:

```auto
 TTree * tree = (TTree*)file->Get("T_mv");
  //find the branch with the data we need in the tree and set the limits
  RooRealVar jpsi("Jpsi_mass", "Mass of Jpsi", 2900, 3300);
  RooRealVar eta1("Muon1_eta","Muon 1's eta", -2.5, 2.5);
  RooRealVar eta2("Muona2_eta","Muon 2's eta", -2.5, 2.5);
  RooDataSet data("data", "Jpsi data set", tree,RooArgSet(jpsi, abs(eta1), abs(eta2)));

```

I’ve tried using RooAbsReal to eta1, RooAbsRealLValue to eta1. I just don’t quite know what to do here. I’ve made a separate file where I plot the values for eta1 and keep trying to find a way to turn all my negative values positive but for the time being I’ve had no success. Any help would be really appreciated. Thanks!  
_ROOT Version:_ 6.14  
_RooFit Version:_ 3.60  
_Platform:_ Windows  
_Compiler:_ mobatek

* * *

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [December 19, 2019, 4:39pm UTC](https://root-forum.cern.ch/t/taking-the-absolute-value-of-all-the-entries-in-a-tbranch-in-roofit/37253/2 "2019-12-19T16:39:35Z")

</div>

Hi,

You would need to create a new data set and loop on the data and fill with the right values.  
For example:

```auto
RooDataSet data1("data1", "Jpsi data set (original)", tree,RooArgSet(jpsi, eta1, eta2));
// make a new data set but empty (not attached to the Three)
 RooDataSet data2("data2", "Jpsi data set with abs eta1 and eta2", RooArgSet(jpsi, eta1, eta2));
int nEvent = data1.numEntries(); 
for(int i=nStart; i < nevent ; ++i) {
    auto vars = data1.get(i);
    auto v1 = (RooRealVar*) vars->find("eta1");
    auto v2 = (RooRealVar*) vars->find("eta2");
    v1->setVal( std::abs( v1->getVal() ) );
    v2->setVal( std::abs( v2->getVal() ) );
    data2.add(*vars);
}
// now data2 will be filled with the abs value of eta1 and eta2 
data2.Print(); 

```

Lorenzo

---

<div class="post-metadata">

**Author:** ![Federico\_G](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/ecd19e/32.png) [@Federico\_G](https://root-forum.cern.ch/u/Federico_G)\
**Post date:** [December 20, 2019, 8:07am UTC](https://root-forum.cern.ch/t/taking-the-absolute-value-of-all-the-entries-in-a-tbranch-in-roofit/37253/3 "2019-12-20T08:07:05Z")

</div>

Hey Lorenzo,  
Thank you so much, it’s not the perfect solution but it’s definitely set me on the corrct bpath. I was considering doing a loop but wasn’t quite sure as to how it would work. I should be able to solve my problem now, thanks!
