# How to shorten my program

**URL:** <https://root-forum.cern.ch/t/how-to-shorten-my-program/36882>\
**Category:** Newbie\
**Created:** [November 19, 2019, 2:11am UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882 "2019-11-19T02:11:29Z")\
**Posts on this page:** 14\
**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 19, 2019, 2:11am UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/1 "2019-11-19T02:11:30Z")

</div>

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?

```auto
 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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 19, 2019, 8:20am UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/2 "2019-11-19T08:20:30Z")

</div>

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

---

<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 19, 2019, 6:03pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/3 "2019-11-19T18:03:43Z")

</div>

gracias 🙂

---

<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 20, 2019, 10:44pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/4 "2019-11-20T22:44:25Z")

</div>

there are errors. please help 🙂 .

```auto
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](https://root-forum.cern.ch/uploads/short-url/emE0UwKtVpCiuPD8tZwNPnGxuBD.C) (2.6 KB)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 21, 2019, 7:58am UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/5 "2019-11-21T07:58:59Z")

</div>

Many mistakes …  
Here is a corrected version:

[DataPointsCode.C](https://root-forum.cern.ch/uploads/short-url/2qyPDlauxcgZxqLtETrzIFtdewC.C) (2.3 KB)

---

<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, 3:16pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/6 "2019-11-21T15:16:42Z")

</div>

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

```auto
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
.
.
.

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 21, 2019, 3:54pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/7 "2019-11-21T15:54:19Z")

</div>

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

---

<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, 4:16pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/8 "2019-11-21T16:16:36Z")

</div>

Yes, i will do best 🙂

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 21, 2019, 4:24pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/9 "2019-11-21T16:24:04Z")

</div>

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

---

<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, 5:29pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/10 "2019-11-21T17:29:02Z")

</div>

yes, it worked but what could be the reason?

---

<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, 5:31pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/11 "2019-11-21T17:31:01Z")

</div>

gracias 🙂  
…

---

<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, 5:45pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/12 "2019-11-21T17:45:28Z")

</div>

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:

```auto
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];”  
instead of only  
“Double\_t dcsep(Double\_t x, Double\_t \*p)”?

Also, in  
f001[i] = new TF1(“fna”, dcsep, 0.0, 0.0, 8.0);,  
what is the purpose of the first 0.0?  
And, why does the y-values do not appear below 8.0?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 21, 2019, 5:49pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/13 "2019-11-21T17:49:31Z")

</div>

[TF1](https://root.cern.ch/doc/master/classTF1.html)  
[TF2](https://root.cern.ch/doc/master/classTF2.html)  
[TF3](https://root.cern.ch/doc/master/classTF3.html#a58abe78c20f700d98172ea48f513def9)

---

<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:04pm UTC](https://root-forum.cern.ch/t/how-to-shorten-my-program/36882/14 "2019-11-21T19:04:22Z")

</div>

okay guys 🙂 I get them already, for now…  
muchas gracias for your help 🙂
