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

this one, for example:

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

would this be okay?

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?

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

i updated it:

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:

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 ***

can i do this?:

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

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:

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.

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

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; }