StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
mainFitBeamLine3D.cxx
1 #include <math.h>
2 #include <assert.h>
3 #include <TMinuit.h>
4 #include <TFile.h>
5 #include <TH1.h>
6 #include <TObjArray.h>
7 #include <TRandom3.h>
8 
9 #include "UtilBeamLine3D.h"
10 
11 UtilBeamLine3D util; // global instance
12 int twoLineDca3D(double& lambda,double& kappa,TVector3& V, TVector3& U, TVector3& R,TVector3& P);
13 
14 
15 //pick MINUIT algorithim to use:
16 const char* comAlgo = "MIGRAD";
17 //const char* comAlgo = "SIMPLEX"; // does NOT compute errors , but is fast
18 //const char* com = "MIGRAD";
19 //const char* com = "MINOS";
20 
21 
22 //===========================
23 int main(int argc, char *argv[]) {
24  printf("MAIN: %s(%s) nPar=%d\n",argv[0],comAlgo, argc-1);
25 
26  if(argc<2) { printf("Abort, expected: %s coreName [x=doChi2plot] \n",argv[0]); return 1;}
27 
28  TRandom3* rnd = new TRandom3(0); // use random start each time
29 
30 
31  //define variables needed to manipulate parameters after fit
32  Double_t amin,edm,errdef;
33  Int_t nvpar,nparx,icstat;
34  double pval[mxPar],perr[mxPar],plo[mxPar],phi[mxPar];
35  TString paraName[mxPar];
36  int istat;
37  const char *core = argv[1]; assert(strlen(core)>5);
38 
39  TObjArray HList;
40  util.initHisto(&HList);
41 
42  pval[0] = (rnd->Rndm()-0.5)*2.;
43  pval[1] = (rnd->Rndm()-0.5)*2.;
44  pval[2] = (rnd->Rndm()-0.5)/25.;
45  pval[3] = (rnd->Rndm()-0.5)/25.;
46 
47  printf("rand # starting point = %f , %f , %f , %f \n",pval[0],pval[1],pval[2],pval[3]);
48  printf("main fit 3D beamline to %s start...\n",core);
49  // select input file:
50  TString inpFile=Form("inp/globTr_%s.txt",core);
51  util.readTracks(inpFile);
52 
53  util.print();
54  int nOkTr=util.qaTracks();
55 #if 1
56  assert(nOkTr>2000); // temporary
57 
58  TMinuit *gMinuit = new TMinuit(4);
59  //..... pass name of the minimized function ......
60  gMinuit->SetFCN(beamLineLike3D);
61  double arglist[10];
62  int error_flag = 0;
63 
64  arglist[0] = 1;
65  gMinuit->mnexcm("SET ERR",arglist,1,error_flag);
66  //Interprets a command and takes appropriate action
67  //void mnexcm(const char* comand, Double_t* plist, Int_t llist, Int_t& ierflg)
68 
69  //define range, step size and name of all parameters
70  gMinuit->mnparm(0, "X0 (cm)",pval[0],0.01,-2,2,error_flag);
71  gMinuit->mnparm(1, "Y0 (cm)",pval[1],0.01,-2,2,error_flag);
72  gMinuit->mnparm(2, "Ux",pval[3],0.001,-0.1,0.1,error_flag);
73  gMinuit->mnparm(3, "Uy",pval[3],0.001,-0.1,0.1,error_flag);
74 
75 
76  arglist[0] = 500.;
77  arglist[1] = 1.;
78 
79 
80  /* *******
81  minimize
82  ******* */
83 
84  gMinuit->mnexcm(comAlgo,arglist,2,error_flag);
85 
86  gMinuit->mnstat(amin,edm,errdef,nvpar,nparx,icstat);
87  gMinuit->mnprin(3,amin);
88 
89  //Prints the values of the parameters at the time of the call*-
90  // 3 values, errors, step sizes, first derivs.
91 
92  util.fcnCount=-1; // disable ceratin monitorings for final plots
93 
94  for(int ip=0;ip<mxPar;ip++) {
95  gMinuit->mnpout(ip,paraName[ip],pval[ip],perr[ip],plo[ip],phi[ip],istat);
96  TString tt=paraName[ip]; float val=pval[ip], err=perr[ip];
97  if(ip>1) { tt="30x "+tt; val*=30.; err*=30.;}
98  util.hA[19]->Fill(tt,val);
99  util.hA[19]->SetBinError(ip+1,err);
100  }
101  util.hA[19]->GetXaxis()->SetTitle(core);
102 
103  printf("\n#beamLine for %s Xcm,Ycm,Ux,Uy = %.3f %.3f %.5f %.5f nOkTr= %d\n",core,pval[0],pval[1],pval[2],pval[3], nOkTr);
104  printf("#beamLineWerr for %s Xcm,Ycm,Ux,Uy = %.3f %.2e %.3f %.2e %.5f %.2e %.5f %.2e nOkTr= %d\n",core,pval[0],perr[0],pval[1],perr[1],pval[2],perr[2],pval[3],perr[3], nOkTr );
105  // gMinuit->Delete();
106 
107  printf("\n ***done!***, scan Chi2 around solution\n");
108  util.evalSolution(pval);
109 
110  if(argc>2) {
111  util.scanChi2(pval,0); //0=x-y
112  util.scanChi2(pval,1); //1=x-nx
113  util.scanChi2(pval,2); //2= y-ny
114  } else
115  printf("skip generation of 2D chi2 plots\n");
116 #endif
117 
118  // Save histograms
119  HList.ls();
120  // return 0;
121  TString outName="out/3D_beam_"; outName+=core;outName+=".hist.root";
122  TFile f( outName,"recreate");
123  assert(f.IsOpen());
124  printf("%d histos are written to '%s' ...\n",HList.GetEntries(),outName.Data());
125  HList.Write();
126  f.Close();
127  // gMinuit->Delete();
128 
129 }
130 
131