ROOT logo
//*-- Author : Anar Rustamov

#include "halignmentgshower.h"
#include "halignmentggeom.h"
#include "halignmentgrotations.h"
#include "hgeomtransform.h"
#include "TH2F.h"
#include "TH1F.h"
#include "TMinuit.h"
#include "iostream"
#include "TMath.h"
#include "TFile.h"
#include "TCanvas.h"
#include "TNtuple.h"

using namespace std;
ClassImp(HAlignmentGShower)
  HAlignmentGShower::HAlignmentGShower()
{
  fAlignGeom  = new HAlignmentGGeom();
  fAlignRot   = new HAlignmentGRotations();
  fMinuit     = NULL;////new TMinuit(6);
  alignMode   = 0;
}

void HAlignmentGShower::SetNtuple(TNtuple *_nt)
{
 nt = _nt;
 nt -> SetBranchAddress("x1s",&x1); // yuxari dubna 
 nt -> SetBranchAddress("y1s",&y1); // yuxari dubna
 nt -> SetBranchAddress("z1s",&z1);
 nt -> SetBranchAddress("x2s",&x2); // yuxari gsi 
 nt -> SetBranchAddress("y2s",&y2); // yuxari gsi
 nt -> SetBranchAddress("z2s",&z2);
 nt -> SetBranchAddress("xsh",&xsh); // ashagi gsi 
 nt -> SetBranchAddress("ysh",&ysh); // ashagi gsi
 nt -> SetBranchAddress("zsh",&zsh);
 nt -> SetBranchAddress("xshl",&xshl); // ashagi gsi
 nt -> SetBranchAddress("yshl",&yshl);
 nt -> SetBranchAddress("zshl",&zshl);




 nt -> SetBranchAddress("s",&sec);
}

HAlignmentGShower::~HAlignmentGShower()
{
  if(fAlignGeom)
    {
      delete fAlignGeom;
      fAlignGeom=NULL;
    }
  if(fAlignRot)
    {
      delete fAlignRot;
      fAlignRot=NULL;
    }
  
  if(fMinuit)
    {
      delete fMinuit;
      fMinuit=NULL;
    }
}



void fcnShower(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
{
  static int count = 1;
  count++;
  HAlignmentGShower       *myObject   = (HAlignmentGShower*)(gMinuit -> GetObjectFit());
  HAlignmentGRotations    *fAlignRot  = myObject -> GetAlignRot();
  HGeomTransform trans =  fAlignRot -> MakeTransMatrix(par[0], par[1], par[2], par[3], par[4], par[5]);
 // HGeomTransform transOldT = myObject -> getTransOld();
 // transOldT.transTo(trans);

  
  f =  myObject -> GetMinFunction(trans);
  if(count%100 == 0) 
  
  cout<<"iter:  "<<count<<"  function  "<<f<<" "<<par[0]<<"  "<<par[1]<<"  "<<par[2]<<"  "<<par[3]<<"  "
                      <<par[4]<<"  "<<par[5]<<" alignmode" <<myObject -> getAlignMode() << endl;
}


void HAlignmentGShower::AlignShower(Int_t sec)
{

  alignSec = sec;

  Int_t ierflg = 0;
  if(!gMinuit) fMinuit=new TMinuit(6);
  gMinuit->SetFCN(fcnShower);
  gMinuit->SetObjectFit(this);
  Double_t arglist[6];
  Double_t vstart[6]={0.};
  Double_t low[6],up[6];
  Double_t step[13];
  Double_t out[6];
  Double_t errOut[6];
  
  
  HGeomTransform transOldB     =  transOld;
  transOldB.transTo(*fAlignRot -> GetTransMdc(alignSec));
  
  fAlignRot -> GetEulerAngles (transOldB, vstart[0], vstart[1], vstart[2]);
  fAlignRot -> GetTransVector (transOldB, vstart[3], vstart[4], vstart[5]);
//  vstart[0] = acos(-1.)/180.;
//  vstart[1]=0.; 
//  vstart[2] = -1.*vstart[0];
 
  for(Int_t i=0; i<6; i++)
    {
       step[i] = 0.001;
       low[i]  = vstart[i] - .09;
       up[i]   = vstart[i] + .09;
    }
  low[3]  = vstart[3] - 55.;
  low[4]  = vstart[4] - 55.;
  low[5]  = vstart[5] - 55.;
  up[3]   = vstart[3] + 55.;
  up[4]   = vstart[4] + 55.;
  up[5]   = vstart[5] + 55.;
  step[3] = 1.;
  step[4] = 5.;
  step[5] = 5.;

  
  gMinuit->mnparm(0,"phi", vstart[0],step[0],low[0],up[0],ierflg);
  gMinuit->mnparm(1,"psi",vstart[1], step[1],low[1],up[1],ierflg);
  gMinuit->mnparm(2,"theta", vstart[2], step[2],low[2],up[2],ierflg);
  gMinuit->mnparm(3,"x", vstart[3], step[3], low[3],up[3],ierflg);
  gMinuit->mnparm(4,"y",vstart[4], step[4],low[4],up[4],ierflg);
  gMinuit->mnparm(5,"z", vstart[5], step[5], low[5],up[5],ierflg);
 
/*
  gMinuit->mnparm(0,"phi", vstart[0],step[0],0,0,ierflg);
  gMinuit->mnparm(1,"psi",vstart[1], step[1],0,0,ierflg);
  gMinuit->mnparm(2,"theta", vstart[2], step[2],0,0,ierflg);
  gMinuit->mnparm(3,"x", vstart[3], step[3], 0,0,ierflg);
  gMinuit->mnparm(4,"y",vstart[4], step[4],0,0,ierflg);
  gMinuit->mnparm(5,"z", vstart[5], step[5], 0,0,ierflg);
*/

 
  gMinuit->SetPrintLevel(-1);
  gMinuit->SetErrorDef(1);
  arglist[0]=2;
  gMinuit->mnexcm("SET STR",arglist,1,ierflg);
  arglist[1] = 0.01;
  arglist[0] = 20000;
  gMinuit->mnexcm("SIMPLEX",arglist,1,ierflg);
  
  for (Int_t ii = 0; ii < 6; ii++)
    {
      gMinuit->GetParameter(ii,out[ii],errOut[ii]);
    }
  //transNew = transOld;
  transNew = fAlignRot->MakeTransMatrix(out[0], out[1], out[2], out[3], out[4], out[5]);
  transNew.transFrom(*fAlignRot->GetTransMdc(alignSec));
 // transNew.transTo(transS);
}



Float_t HAlignmentGShower::GetMinFunction(HGeomTransform &trans)
{
  Float_t dist = 0;
  Float_t dist1;
  Int_t nentries = nt -> GetEntries();
  for(Int_t i = 0; i < nentries; i++)
    {
      nt->GetEntry(i);
      
      if(sec != alignSec) continue;
      
      point1.setXYZ(x1, y1, z1); //lab
      point2.setXYZ(x2, y2, z2);      
      
      if(alignMode == 0)
      {
        pointSh.setXYZ(xshl,yshl,zshl); 
        pointSh = trans.transFrom(pointSh);//lab
        dist1 = fAlignGeom -> CalcDistanceToLine(pointSh,point1,point2);
        if(dist1 > 150) continue;
      }     
      else
      { 
	pointSh.setXYZ(xsh,ysh,zsh);//lab
	point1  =   trans.transTo(point1); 
	point2  =   trans.transTo(point2);
	pointSh =   trans.transTo(pointSh);
	point1.setZ(point1.getZ() - zshl);
	point2.setZ(point2.getZ() - zshl);
	diffZ = point2.getZ() - point1.getZ();
	diffX = point2.getX() - point1.getX();
	diffY = point2.getY() - point1.getY();
	xCr   = (point1.getX()*diffZ - point1.getZ()*diffX)/diffZ;
	yCr   = (point1.getY()*diffZ - point1.getZ()*diffY)/diffZ;
	dist1 = sqrt((xCr - pointSh.getX())*(xCr - pointSh.getX()) + 
		     (yCr - pointSh.getY())*(yCr - pointSh.getY()));
	if(dist1 > 180) continue; 
      }       
      
      dist += dist1;
    }
  return dist;
}

  void HAlignmentGShower::CheckAlignment(HGeomTransform transNew, HGeomTransform transOld, TFile *f)
  {
   
     transNew.transTo(*fAlignRot->GetTransMdc(alignSec));
     transOld.transTo(*fAlignRot->GetTransMdc(alignSec));
    
  
    Char_t nnn[256];
    
    sprintf(nnn,"%s%i%s","align",alignSec,".root");
    TString nn = nnn;
    
    
    
    if(!f) f = new TFile(nn.Data(),"recreate");
  
    TNtuple *chnt = new TNtuple("chnt","chnt","xold:yold:xnew:ynew:xcrold:ycrold:xcrnew:ycrnew:sec");  
    
    Int_t nentries = nt -> GetEntries();
    for(Int_t i = 0; i < nentries; i++)
    {
      nt->GetEntry(i);
      
      if(sec != alignSec) continue;
      
      point1.setXYZ(x1, y1, z1); 
      point2.setXYZ(x2, y2, z2);
      
      ////////////////////
      pointSh.setXYZ(xshl,yshl,zshl); 
      //pointSh = trans.transFrom(pointSh);//lab
      //pointSh.setXYZ(xsh,ysh,zsh);
      Float_t coordOld[4];
      Float_t coordNew[4];
      
      getPoints(point1, point2, pointSh, transOld, coordOld);
      getPoints(point1, point2, pointSh, transNew, coordNew);
      chnt -> Fill(coordOld[0], coordOld[1], coordNew[0], coordNew[1], coordOld[2], coordOld[3], 
                  coordNew[2], coordNew[3],sec);
     }
     f->cd();
     chnt -> Write();
   }
   
  void HAlignmentGShower::getPoints(HGeomVector &p1, HGeomVector &p2, HGeomVector &psh, 
                                    HGeomTransform &trans, Float_t *coord)
  {
    
      HGeomVector pointSh;
      pointSh = trans.transFrom(psh);
      HGeomVector testCr = fAlignGeom -> CalcIntersection(p1,p2,trans);
       
//      point1  =   trans.transTo(p1);
//      point2  =   trans.transTo(p2);
//      pointSh =   trans.transTo(psh);

//      diffZ = point2.getZ() - point1.getZ();
//      diffX = point2.getX() - point1.getX();
//      diffY = point2.getY() - point1.getY();
//      xCr   = (point1.getX()*diffZ - point1.getZ()*diffX)/diffZ;
//      yCr   = (point1.getY()*diffZ - point1.getZ()*diffY)/diffZ;
      
      coord[0] = pointSh.getX();
      coord[1] = pointSh.getY();
      coord[2] = testCr.getX();
      coord[3] = testCr.getY();

        
  
  }
        
 halignmentgshower.cc:1
 halignmentgshower.cc:2
 halignmentgshower.cc:3
 halignmentgshower.cc:4
 halignmentgshower.cc:5
 halignmentgshower.cc:6
 halignmentgshower.cc:7
 halignmentgshower.cc:8
 halignmentgshower.cc:9
 halignmentgshower.cc:10
 halignmentgshower.cc:11
 halignmentgshower.cc:12
 halignmentgshower.cc:13
 halignmentgshower.cc:14
 halignmentgshower.cc:15
 halignmentgshower.cc:16
 halignmentgshower.cc:17
 halignmentgshower.cc:18
 halignmentgshower.cc:19
 halignmentgshower.cc:20
 halignmentgshower.cc:21
 halignmentgshower.cc:22
 halignmentgshower.cc:23
 halignmentgshower.cc:24
 halignmentgshower.cc:25
 halignmentgshower.cc:26
 halignmentgshower.cc:27
 halignmentgshower.cc:28
 halignmentgshower.cc:29
 halignmentgshower.cc:30
 halignmentgshower.cc:31
 halignmentgshower.cc:32
 halignmentgshower.cc:33
 halignmentgshower.cc:34
 halignmentgshower.cc:35
 halignmentgshower.cc:36
 halignmentgshower.cc:37
 halignmentgshower.cc:38
 halignmentgshower.cc:39
 halignmentgshower.cc:40
 halignmentgshower.cc:41
 halignmentgshower.cc:42
 halignmentgshower.cc:43
 halignmentgshower.cc:44
 halignmentgshower.cc:45
 halignmentgshower.cc:46
 halignmentgshower.cc:47
 halignmentgshower.cc:48
 halignmentgshower.cc:49
 halignmentgshower.cc:50
 halignmentgshower.cc:51
 halignmentgshower.cc:52
 halignmentgshower.cc:53
 halignmentgshower.cc:54
 halignmentgshower.cc:55
 halignmentgshower.cc:56
 halignmentgshower.cc:57
 halignmentgshower.cc:58
 halignmentgshower.cc:59
 halignmentgshower.cc:60
 halignmentgshower.cc:61
 halignmentgshower.cc:62
 halignmentgshower.cc:63
 halignmentgshower.cc:64
 halignmentgshower.cc:65
 halignmentgshower.cc:66
 halignmentgshower.cc:67
 halignmentgshower.cc:68
 halignmentgshower.cc:69
 halignmentgshower.cc:70
 halignmentgshower.cc:71
 halignmentgshower.cc:72
 halignmentgshower.cc:73
 halignmentgshower.cc:74
 halignmentgshower.cc:75
 halignmentgshower.cc:76
 halignmentgshower.cc:77
 halignmentgshower.cc:78
 halignmentgshower.cc:79
 halignmentgshower.cc:80
 halignmentgshower.cc:81
 halignmentgshower.cc:82
 halignmentgshower.cc:83
 halignmentgshower.cc:84
 halignmentgshower.cc:85
 halignmentgshower.cc:86
 halignmentgshower.cc:87
 halignmentgshower.cc:88
 halignmentgshower.cc:89
 halignmentgshower.cc:90
 halignmentgshower.cc:91
 halignmentgshower.cc:92
 halignmentgshower.cc:93
 halignmentgshower.cc:94
 halignmentgshower.cc:95
 halignmentgshower.cc:96
 halignmentgshower.cc:97
 halignmentgshower.cc:98
 halignmentgshower.cc:99
 halignmentgshower.cc:100
 halignmentgshower.cc:101
 halignmentgshower.cc:102
 halignmentgshower.cc:103
 halignmentgshower.cc:104
 halignmentgshower.cc:105
 halignmentgshower.cc:106
 halignmentgshower.cc:107
 halignmentgshower.cc:108
 halignmentgshower.cc:109
 halignmentgshower.cc:110
 halignmentgshower.cc:111
 halignmentgshower.cc:112
 halignmentgshower.cc:113
 halignmentgshower.cc:114
 halignmentgshower.cc:115
 halignmentgshower.cc:116
 halignmentgshower.cc:117
 halignmentgshower.cc:118
 halignmentgshower.cc:119
 halignmentgshower.cc:120
 halignmentgshower.cc:121
 halignmentgshower.cc:122
 halignmentgshower.cc:123
 halignmentgshower.cc:124
 halignmentgshower.cc:125
 halignmentgshower.cc:126
 halignmentgshower.cc:127
 halignmentgshower.cc:128
 halignmentgshower.cc:129
 halignmentgshower.cc:130
 halignmentgshower.cc:131
 halignmentgshower.cc:132
 halignmentgshower.cc:133
 halignmentgshower.cc:134
 halignmentgshower.cc:135
 halignmentgshower.cc:136
 halignmentgshower.cc:137
 halignmentgshower.cc:138
 halignmentgshower.cc:139
 halignmentgshower.cc:140
 halignmentgshower.cc:141
 halignmentgshower.cc:142
 halignmentgshower.cc:143
 halignmentgshower.cc:144
 halignmentgshower.cc:145
 halignmentgshower.cc:146
 halignmentgshower.cc:147
 halignmentgshower.cc:148
 halignmentgshower.cc:149
 halignmentgshower.cc:150
 halignmentgshower.cc:151
 halignmentgshower.cc:152
 halignmentgshower.cc:153
 halignmentgshower.cc:154
 halignmentgshower.cc:155
 halignmentgshower.cc:156
 halignmentgshower.cc:157
 halignmentgshower.cc:158
 halignmentgshower.cc:159
 halignmentgshower.cc:160
 halignmentgshower.cc:161
 halignmentgshower.cc:162
 halignmentgshower.cc:163
 halignmentgshower.cc:164
 halignmentgshower.cc:165
 halignmentgshower.cc:166
 halignmentgshower.cc:167
 halignmentgshower.cc:168
 halignmentgshower.cc:169
 halignmentgshower.cc:170
 halignmentgshower.cc:171
 halignmentgshower.cc:172
 halignmentgshower.cc:173
 halignmentgshower.cc:174
 halignmentgshower.cc:175
 halignmentgshower.cc:176
 halignmentgshower.cc:177
 halignmentgshower.cc:178
 halignmentgshower.cc:179
 halignmentgshower.cc:180
 halignmentgshower.cc:181
 halignmentgshower.cc:182
 halignmentgshower.cc:183
 halignmentgshower.cc:184
 halignmentgshower.cc:185
 halignmentgshower.cc:186
 halignmentgshower.cc:187
 halignmentgshower.cc:188
 halignmentgshower.cc:189
 halignmentgshower.cc:190
 halignmentgshower.cc:191
 halignmentgshower.cc:192
 halignmentgshower.cc:193
 halignmentgshower.cc:194
 halignmentgshower.cc:195
 halignmentgshower.cc:196
 halignmentgshower.cc:197
 halignmentgshower.cc:198
 halignmentgshower.cc:199
 halignmentgshower.cc:200
 halignmentgshower.cc:201
 halignmentgshower.cc:202
 halignmentgshower.cc:203
 halignmentgshower.cc:204
 halignmentgshower.cc:205
 halignmentgshower.cc:206
 halignmentgshower.cc:207
 halignmentgshower.cc:208
 halignmentgshower.cc:209
 halignmentgshower.cc:210
 halignmentgshower.cc:211
 halignmentgshower.cc:212
 halignmentgshower.cc:213
 halignmentgshower.cc:214
 halignmentgshower.cc:215
 halignmentgshower.cc:216
 halignmentgshower.cc:217
 halignmentgshower.cc:218
 halignmentgshower.cc:219
 halignmentgshower.cc:220
 halignmentgshower.cc:221
 halignmentgshower.cc:222
 halignmentgshower.cc:223
 halignmentgshower.cc:224
 halignmentgshower.cc:225
 halignmentgshower.cc:226
 halignmentgshower.cc:227
 halignmentgshower.cc:228
 halignmentgshower.cc:229
 halignmentgshower.cc:230
 halignmentgshower.cc:231
 halignmentgshower.cc:232
 halignmentgshower.cc:233
 halignmentgshower.cc:234
 halignmentgshower.cc:235
 halignmentgshower.cc:236
 halignmentgshower.cc:237
 halignmentgshower.cc:238
 halignmentgshower.cc:239
 halignmentgshower.cc:240
 halignmentgshower.cc:241
 halignmentgshower.cc:242
 halignmentgshower.cc:243
 halignmentgshower.cc:244
 halignmentgshower.cc:245
 halignmentgshower.cc:246
 halignmentgshower.cc:247
 halignmentgshower.cc:248
 halignmentgshower.cc:249
 halignmentgshower.cc:250
 halignmentgshower.cc:251
 halignmentgshower.cc:252
 halignmentgshower.cc:253
 halignmentgshower.cc:254
 halignmentgshower.cc:255
 halignmentgshower.cc:256
 halignmentgshower.cc:257
 halignmentgshower.cc:258
 halignmentgshower.cc:259
 halignmentgshower.cc:260
 halignmentgshower.cc:261
 halignmentgshower.cc:262
 halignmentgshower.cc:263
 halignmentgshower.cc:264
 halignmentgshower.cc:265
 halignmentgshower.cc:266
 halignmentgshower.cc:267
 halignmentgshower.cc:268
 halignmentgshower.cc:269
 halignmentgshower.cc:270
 halignmentgshower.cc:271
 halignmentgshower.cc:272
 halignmentgshower.cc:273
 halignmentgshower.cc:274
 halignmentgshower.cc:275
 halignmentgshower.cc:276
 halignmentgshower.cc:277
 halignmentgshower.cc:278
 halignmentgshower.cc:279
 halignmentgshower.cc:280
 halignmentgshower.cc:281
 halignmentgshower.cc:282
 halignmentgshower.cc:283