# How to shorten my program

hi guys,

I need some help again
I will be using the function below for several times like more than 50 times as I keep changing these 3 parameters:
fpf[i]->SetParameter(4,61.0);
fpf[i]->SetParameter(6,gRandom->Gaus(0.9346,0.0045));
fpf[i]->SetParameter(7,gRandom->Gaus(3.2880,0.2134));
For this case, my code would be getting longer and longer as I add more and more parameters.

Sample parameters:
fpf[i]->SetParameter(4,61.0); fpf[i]->SetParameter(6,gRandom->Gaus(0.9346,0.0045)); fpf[i]->SetParameter(7,gRandom->Gaus(3.2880,0.2134));

fpf[i]->SetParameter(4,55.0); fpf[i]->SetParameter(6,gRandom->Gaus(0.9122,0.0023)); fpf[i]->SetParameter(7,gRandom->Gaus(3.1280,0.0134));

fpf[i]->SetParameter(4,118.0); fpf[i]->SetParameter(6,gRandom->Gaus(0.8921,0.0003)); fpf[i]->SetParameter(7,gRandom->Gaus(2.8990,0.0014));

fpf[i]->SetParameter(4,71.0); fpf[i]->SetParameter(6,gRandom->Gaus(0.9021,0.0035)); fpf[i]->SetParameter(7,gRandom->Gaus(2.9590,0.0019));

Can you help me write a sample code for me to reduce my lengthy program?

`````` for (Int_t i=0; i<nof; i++)
{
fna = "f"; fna += i;
//choice: transfer momentum square; electron(finite)proton
fpf[i] = new TF1("fna","(((2.0*[0]*[0]*[1]*[1]*[2]*[2]*[3]*[3])/(x*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+2.0*[3]*[3]*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+(x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sqrt(x*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+4.0*[3]*[3]*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)))))*((2.0*[3]*[3])/(2.0*[3]*[3]+x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+sqrt(x*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+4.0*[3]*[3]*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0))))*((4.0*[3]*[3])/(4.0*[3]*[3]+x))*([6]*[6]*cos([4]*[5]/360.0)*cos([4]*[5]/360.0)+[7]*[7]*(2.0*[3]*[3]*x*cos([4]*[5]/360.0)*cos([4]*[5]/360.0)+4.0*[3]*[3]*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0)+x*x*sin([4]*[5]/360.0)*sin([4]*[5]/360.0))/(8.0*[3]*[3]*[3]*[3])))", 0.0025, 1.0);
fpf[i]->SetParameter(0,1.0);
fpf[i]->SetParameter(1,1.0);
fpf[i]->SetParameter(2,1.0/137.0);
fpf[i]->SetParameter(3,gRandom->Gaus(0.938272081,0.000000006));
fpf[i]->SetParameter(4,61.0);
fpf[i]->SetParameter(5,TMath::Pi());
fpf[i]->SetParameter(6,gRandom->Gaus(0.9346,0.0045));
fpf[i]->SetParameter(7,gRandom->Gaus(3.2880,0.2134));
}

//=== getting the average of the function===//

TGraph *av1 = new TGraph();
for (Int_t j=0; j < nop; j++)
{
xpt = del*j + 0.0025*del;
av1->SetPoint(j, xpt, 0.0);
}
for (Int_t i=0; i<nof; i++)
{
for (Int_t j=0; j < nop; j++)
{
av1->GetPoint(j, xpt, ypt);
av1->SetPoint(j, xpt, ypt + fpf[i]->Eval(xpt)/(Double_t)nof);
}
}
``````

best regards,
jaybee

Make a function factorising the common code and call that function instead of copy/pasting the same code several times.

gracias

``````root [17] .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 'void DataPointsCode()':
/home/jbmagallanes/./DataPointsCode.C:42: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:43: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:44: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:45: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:46: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:47: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:48: error: base operand of '->' has non-pointer type 'TF1'
/home/jbmagallanes/./DataPointsCode.C:49: error: base operand of '->' has non-pointer type 'TF1'
In file included from /usr/local/root/root-5.34.36/include/TF1.h:40,
from /home/jbmagallanes/./DataPointsCode.C:5,
from /home/jbmagallanes/DataPointsCode_C_ACLiC_dict.h:34,
from /home/jbmagallanes/DataPointsCode_C_ACLiC_dict.cxx:17:
/usr/local/root/root-5.34.36/include/Math/ParamFunctor.h: In static member function 'static double ROOT::Math::ParamFunctorHandler<ParentFunctor, Func>::FuncEvaluator<F*>::Eval(F*, double*, double*) [with F = Double_t(Double_t, Double_t*), ParentFunctor = ROOT::Math::ParamFunctor, Func = Double_t (*)(Double_t, Double_t*)]':
/usr/local/root/root-5.34.36/include/Math/ParamFunctor.h:88:   instantiated from 'double ROOT::Math::ParamFunctorHandler<ParentFunctor, Func>::operator()(double*, double*) [with ParentFunctor = ROOT::Math::ParamFunctor, Func = Double_t (*)(Double_t, Double_t*)]'
/home/jbmagallanes/DataPointsCode_C_ACLiC_dict.cxx:426:   instantiated from here
/usr/local/root/root-5.34.36/include/Math/ParamFunctor.h:109: error: cannot convert 'double*' to 'Double_t' in argument passing
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 ***
root [18]
``````

DataPointsCode.C (2.6 KB)

Many mistakes …
Here is a corrected version:

DataPointsCode.C (2.3 KB)

Muchas gracias… It has no error now. But, I got this print out with no y-values:

``````Info in <ACLiC>: unmodified script has already been compiled and loaded
Info in <ACLiC>: it will be regenerated and reloaded!
Info in <TUnixSystem::ACLiC>: creating shared library /home/jbmagallanes/./DataPointsCode_C.so
x[0]=9.975e-06, y[0]=-nan
x[1]=0.00399998, y[1]=-nan
x[2]=0.00798998, y[2]=-nan
x[3]=0.01198, y[3]=-nan
x[4]=0.01597, y[4]=-nan
x[5]=0.01996, y[5]=-nan
x[6]=0.02395, y[6]=-nan
x[7]=0.02794, y[7]=-nan
x[8]=0.03193, y[8]=-nan
x[9]=0.03592, y[9]=-nan
x[10]=0.03991, y[10]=-nan
x[11]=0.0439, y[11]=-nan
x[12]=0.04789, y[12]=-nan
x[13]=0.05188, y[13]=-nan
x[14]=0.05587, y[14]=-nan
x[15]=0.05986, y[15]=-nan
x[16]=0.06385, y[16]=-nan
x[17]=0.06784, y[17]=-nan
x[18]=0.07183, y[18]=-nan
x[19]=0.07582, y[19]=-nan
x[20]=0.07981, y[20]=-nan
x[21]=0.0838, y[21]=-nan
x[22]=0.08779, y[22]=-nan
x[23]=0.09178, y[23]=-nan
x[24]=0.09577, y[24]=-nan
x[25]=0.09976, y[25]=-nan
x[26]=0.10375, y[26]=-nan
x[27]=0.10774, y[27]=-nan
x[28]=0.11173, y[28]=-nan
x[29]=0.11572, y[29]=-nan
x[30]=0.11971, y[30]=-nan
x[31]=0.1237, y[31]=-nan
x[32]=0.12769, y[32]=-nan
x[33]=0.13168, y[33]=-nan
x[34]=0.13567, y[34]=-nan
x[35]=0.13966, y[35]=-nan
x[36]=0.14365, y[36]=-nan
x[37]=0.14764, y[37]=-nan
x[38]=0.15163, y[38]=-nan
x[39]=0.15562, y[39]=-nan
x[40]=0.15961, y[40]=-nan
x[41]=0.1636, y[41]=-nan
x[42]=0.16759, y[42]=-nan
x[43]=0.17158, y[43]=-nan
.
.
.
``````

Yes Y vales are nan… you should debug your code.

Yes, i will do best

`f001[i] = new TF1(fna, dcsep, 0.0, 0.0025, 8);`

yes, it worked but what could be the reason?

gracias

i’d like to ask about some lines. Honestly, I am just working on code templates so I do not have a deep understanding about programming. Here:

``````Double_t dcsep(Double_t *xx, Double_t *p)
{
Float_t x =xx[0];
Double_t fcn = (((2.0*p[0]*p[0]*p[1]*p[1]*p[2]*p[2]*p[3]*p[3])/(x*x*sin(p[4]*p[5]/360.0)...
return fcn;
}
``````

Why
“Double_t dcsep(Double_t *xx, Double_t *p) with Float_t x =xx[0];”