# How to make and call (conditional statements) as an object?

**URL:** <https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914>\
**Category:** Newbie\
**Created:** [November 21, 2019, 7:52pm UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914 "2019-11-21T19:52:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbmagallanes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jbmagallanes/32/20291_2.png) [@jbmagallanes](https://root-forum.cern.ch/u/jbmagallanes)\
**Post date:** [November 21, 2019, 7:52pm UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/1 "2019-11-21T19:52:49Z")

</div>

this one, for example:

```auto
TRandom3 r;
Double_t rdm = r.Rndm();

if (rdm < 0.25)
{
 f001[i]->SetParameter(3,gRandom->Gaus(0.0017,0.0008));
}
 else if (rdm > 0.75)
 {
  f001[i]->SetParameter(3,gRandom->Gaus(0.0033,0.0008));
 }
 else
 {
  f001[i]->SetParameter(3,(gRandom->Gaus(0.0017,0.0008)+gRandom->Gaus(0.0033,0.0008))/2.0);
 }

```

In order to return a value that I can use to set as the value for Parameter 3.

regards,  
Jaybee

---

<div class="post-metadata">

**Author:** ![jbmagallanes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jbmagallanes/32/20291_2.png) [@jbmagallanes](https://root-forum.cern.ch/u/jbmagallanes)\
**Post date:** [November 21, 2019, 8:27pm UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/2 "2019-11-21T20:27:18Z")

</div>

would this be okay?

```auto
Double_t val01()
{
 TRandom3 r;
 Double_t rdm = r.Rndm();
 Double_t val01;

 if (rdm < 0.25)
 {
  val01 = gRandom->Gaus(0.0017,0.0008);
 }
  else if (rdm > 0.75)
  {
   val01 = gRandom->Gaus(0.0033,0.0008);
  }
  else
  {
   val01 = (gRandom->Gaus(0.0017,0.0008)+gRandom->Gaus(0.0033,0.0008))/2.0);
  }

 return val01;
}

```

then can I do this?

```auto
f001[i]->SetParameter(1,val01);

```

---

<div class="post-metadata">

**Author:** ![jbmagallanes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jbmagallanes/32/20291_2.png) [@jbmagallanes](https://root-forum.cern.ch/u/jbmagallanes)\
**Post date:** [November 22, 2019, 12:07am UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/3 "2019-11-22T00:07:03Z")

</div>

i updated it:

```auto
Double_t val01(Double_t *val)
{
 TRandom3 r;
 Double_t rdm = r.Rndm();
 Double_t val;

 if (rdm < 0.25)
 {
  val = gRandom->Gaus(0.0017,0.0008);
 }
  else if (rdm > 0.75)
  {
   val = gRandom->Gaus(0.0033,0.0008);
  }
   else
   {
    val = (gRandom->Gaus(0.0017,0.0008)+gRandom->Gaus(0.0033,0.0008))/2.0;
   }

 return val;
}

```

and this is the error message:

```auto
root [2] .x DataPointsCode.C++
Info in <TUnixSystem::ACLiC>: creating shared library /home/jbmagallanes/./DataPointsCode_C.so
In file included from /home/jbmagallanes/DataPointsCode_C_ACLiC_dict.h:34,
                 from /home/jbmagallanes/DataPointsCode_C_ACLiC_dict.cxx:17:
/home/jbmagallanes/./DataPointsCode.C: In function 'Double_t val01(Double_t*)':
/home/jbmagallanes/./DataPointsCode.C:36: error: declaration of 'Double_t val' shadows a parameter
g++: /home/jbmagallanes/DataPointsCode_C_ACLiC_dict.o: No such file or directory
Error in <ACLiC>: Compilation failed!
Error: Function DataPointsCode() is not defined in current scope :0:
***Interpreter error recovered***

```

---

<div class="post-metadata">

**Author:** ![jbmagallanes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jbmagallanes/32/20291_2.png) [@jbmagallanes](https://root-forum.cern.ch/u/jbmagallanes)\
**Post date:** [November 22, 2019, 12:41am UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/4 "2019-11-22T00:41:49Z")

</div>

can i do this?:

```auto
f001[i]->SetParameter(2,val01);

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 25, 2019, 9:17am UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/5 "2019-11-25T09:17:30Z")

</div>

Hi,

I don’t understand what you try to do. I can guess - tell me whether I guessed right or wrong.

Depending on `rdm` you want to return a different value, sampling from different random number distributions. You code looks good - the only issue is that your have a local variable `val` and you have a parameter of the same name. What’s your plan with the parameter `Double_t *val` - what do you need it for? Either way, this will fix the mechanical issue of the same-named variable:

```auto
TRandom3 r; // don't construct for each call!
Double_t val01(Double_t * /*val*/)
{
  Double_t rdm = r.Rndm(); // why do you use `r` here, not `gRandom`?
  auto lowGauss[] { return gRandom->Gaus(0.0017,0.0008); };
  auto highGauss[] { return gRandom->Gaus(0.0033,0.0008); };
  if (rdm < 0.25)
    return lowGauss();
  if (rdm > 0.75)
    return highGaus();
  return (lowGaus() + highGaus()) / 2.;
}

```

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![jbmagallanes](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jbmagallanes/32/20291_2.png) [@jbmagallanes](https://root-forum.cern.ch/u/jbmagallanes)\
**Post date:** [November 28, 2019, 8:43pm UTC](https://root-forum.cern.ch/t/how-to-make-and-call-conditional-statements-as-an-object/36914/6 "2019-11-28T20:43:58Z")

</div>

gracias Axel… i think you got my point 🙂 but I made my way like this:

```auto
if (rdm<0.40) { uqbm = gRandom->Gaus(0.0017,0.0008); dqbm = gRandom->Gaus(0.0041,0.0008); }
  else if (rdm>0.60) { uqbm = gRandom->Gaus(0.0033,0.0008); dqbm = gRandom->Gaus(0.0058,0.0008); }
  else { uqbm = (gRandom->Gaus(0.0017,0.0008)+gRandom->Gaus(0.0033,0.0008))/2.0; dqbm = (gRandom->Gaus(0.0041,0.0008)+gRandom->Gaus(0.0058,0.0008))/2.0; }

```
