TSamuraiFDC2Physics.cxx 15.8 KB
Newer Older
Adrien Matta's avatar
Adrien Matta committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*****************************************************************************
 * Copyright (C) 2009-2016   this file is part of the NPTool Project         *
 *                                                                           *
 * For the licensing terms see $NPTOOL/Licence/NPTool_Licence                *
 * For the list of contributors see $NPTOOL/Licence/Contributors             *
 *****************************************************************************/

/*****************************************************************************
 * Original Author: Adrien MATTA  contact address: matta@lpccaen.in2p3.fr    *
 *                                                                           *
 * Creation Date  : October 2020                                             *
 * Last update    :                                                          *
 *---------------------------------------------------------------------------*
 * Decription:                                                               *
 *  This class hold SamuraiFDC2 treated data                                 *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * Comment:                                                                  *
 *                                                                           *
 *****************************************************************************/
#include "TSamuraiFDC2Physics.h"

//   STL
#include <sstream>
#include <iostream>
#include <cmath>
#include <stdlib.h>
#include <limits>

//   NPL
#include "RootInput.h"
#include "RootOutput.h"
#include "TAsciiFile.h"
#include "NPOptionManager.h"
#include "NPDetectorFactory.h"
Adrien Matta's avatar
Adrien Matta committed
36
#include "NPSystemOfUnits.h"
Adrien Matta's avatar
Adrien Matta committed
37 38
//   ROOT
#include "TChain.h"
Adrien Matta's avatar
Adrien Matta committed
39 40 41 42
#include "TVector2.h"
#include "Math/Minimizer.h"
#include "Math/Functor.h"
#include "Math/Factory.h"
Adrien Matta's avatar
Adrien Matta committed
43
using namespace NPUNITS;
Adrien Matta's avatar
Adrien Matta committed
44 45 46 47 48 49 50 51 52
///////////////////////////////////////////////////////////////////////////

ClassImp(TSamuraiFDC2Physics)
  ///////////////////////////////////////////////////////////////////////////
  TSamuraiFDC2Physics::TSamuraiFDC2Physics(){
    m_EventData         = new TSamuraiFDC2Data ;
    m_PreTreatedData    = new TSamuraiFDC2Data ;
    m_EventPhysics      = this ;
    //m_Spectra           = NULL;
Adrien Matta's avatar
Adrien Matta committed
53
    ToTThreshold = 100;
Adrien Matta's avatar
Adrien Matta committed
54 55 56 57 58 59 60 61 62 63
  }

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::BuildSimplePhysicalEvent(){
  BuildPhysicalEvent();
}

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::BuildPhysicalEvent(){
  PreTreat();
Adrien Matta's avatar
Adrien Matta committed
64 65
  RemoveNoise();

Adrien Matta's avatar
Adrien Matta committed
66
  // Map[detector & plane angle, vector of spatial information]
Adrien Matta's avatar
Adrien Matta committed
67 68 69 70
  static map<std::pair<unsigned int,double>, vector<double> > X ; 
  static map<std::pair<unsigned int,double>, vector<double> > Z ; 
  static map<std::pair<unsigned int,double>, vector<double> > R ; 
  X.clear();Z.clear();R.clear();
Adrien Matta's avatar
Adrien Matta committed
71 72
  unsigned int size = Detector.size();
  for(unsigned int i = 0 ; i < size ; i++){
Adrien Matta's avatar
Adrien Matta committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    if(Matched[i]){
      int det = Detector[i];
      int layer = Layer[i];
      int wire = Wire[i]; 
      SamuraiDCIndex idx(det,layer,wire);
      std::pair<unsigned int, double> p(det,Wire_T[idx]);
      X[p].push_back(Wire_X[idx]); 
      Z[p].push_back(Wire_Z[idx]); 
      R[p].push_back(DriftLength[i]); 
    }
  }

  // Reconstruct the vector for each of the plane of each of the detector
  double dirX,dirZ,refX;
Adrien Matta's avatar
Adrien Matta committed
87 88 89
  static map<std::pair<unsigned int,double>, TVector3 > Pos ;  
  static map<std::pair<unsigned int,double>, TVector3 > Dir ;  
  Pos.clear();Dir.clear();
Adrien Matta's avatar
Adrien Matta committed
90
  for(auto it = X.begin();it!=X.end();++it){
Adrien Matta's avatar
Adrien Matta committed
91
      Track2D(X[it->first],Z[it->first],R[it->first],dirX,dirZ,refX); 
Adrien Matta's avatar
Adrien Matta committed
92 93
      Mult=X[it->first].size();
      // Position at z=0
Adrien Matta's avatar
Adrien Matta committed
94 95 96
      TVector3 P(refX,0,0);
      P.RotateZ(-it->first.second);
      Pos[it->first]=P;
Adrien Matta's avatar
Adrien Matta committed
97 98 99 100 101

      // Direction of the vector in the plane
      TVector3 D(dirX,dirZ,0);
      D.RotateZ(-it->first.second);
      Dir[it->first]=D;
Adrien Matta's avatar
Adrien Matta committed
102 103
    }

Adrien Matta's avatar
Adrien Matta committed
104
  // Reconstruct the central position (z=0) for each detector
Adrien Matta's avatar
Adrien Matta committed
105 106
  static map<unsigned int,vector<TVector3> > C ;  
  C.clear();
Adrien Matta's avatar
Adrien Matta committed
107 108 109 110 111 112 113 114 115
  TVector3 P;
  for(auto it1 = Pos.begin();it1!=Pos.end();++it1){
    for(auto it2 = it1;it2!=Pos.end();++it2){
      if(it1!=it2 && it1->first.first==it2->first.first){// different plane, same detector
        ResolvePlane(it1->second,it1->first.second,it2->second,it2->first.second,P);
        C[it1->first.first].push_back(P);
        }
      }
  }
Adrien Matta's avatar
Adrien Matta committed
116
  
Adrien Matta's avatar
Adrien Matta committed
117 118 119 120
  // Build the Reference position by averaging all possible pair 
  size = C[2].size();
  if(size){
    PosX=0;
Adrien Matta's avatar
Adrien Matta committed
121
    PosY=0;
Adrien Matta's avatar
Adrien Matta committed
122 123
    for(unsigned int i = 0 ; i < size ; i++){
      PosX+= C[2][i].X(); 
Adrien Matta's avatar
Adrien Matta committed
124
      PosY+= C[2][i].Y(); 
Adrien Matta's avatar
Adrien Matta committed
125 126
    } 
    PosX/=size; 
Adrien Matta's avatar
Adrien Matta committed
127
    PosY/=size;
Adrien Matta's avatar
Adrien Matta committed
128
  }
Adrien Matta's avatar
Adrien Matta committed
129 130
  return;
}
Adrien Matta's avatar
Adrien Matta committed
131 132 133 134 135 136 137 138 139
////////////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::ResolvePlane(const TVector3& H,const double& ThetaU ,const TVector3& L, const double& ThetaV, TVector3& PosXY){
  // direction of U and V wire 
  TVector3 u = TVector3(0,1,0);
  u.RotateZ(ThetaU); 
  
  TVector3 v = TVector3(0,1,0);
  v.RotateZ(ThetaV); 

Adrien Matta's avatar
Adrien Matta committed
140

Adrien Matta's avatar
Adrien Matta committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  // Compute the coeff of the two line of vecotr u (v) going through H (L)
  // dv : y = av*x+bv
  TVector3 HH = H+v;
  double av = (H.Y() - HH.Y())/(H.X()-HH.X());
  double bv =  H.Y() - av*H.X();   
   
  // du : y = au*x+bv
  TVector3 LL = L+u;
  double au = (L.Y() - LL.Y())/(L.X()-LL.X());
  double bu =  L.Y() - au*L.X();   

  // We look for M(xM, yM) that intersect du and dv:
  double xM,yM;
  if(!isinf(au) && !isinf(av)){ // au and av are not inf, i.e. not vertical line
    xM = (bu-bv)/(av-au); 
    yM = au*xM+bu;
   } 
  else if(isinf(au)){// av is inf, so v is along Y axis, H is direct measure of X
    xM = H.X(); 
    yM = au*xM+bu;
    }
  else if (isinf(av)){//au is inf, so u is along Y axis, L is direct measure of X
    xM = L.X(); 
    yM = av*xM+bv;
    }
  else{ // all is lost
    xM=-10000;
    yM=-10000;
    }
  PosXY=TVector3(xM,yM,0);

  };
Adrien Matta's avatar
Adrien Matta committed
173 174
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::Track2D(const vector<double>& X,const vector<double>& Z,const vector<double>& R,double& dirX, double& dirZ,double& refX ){
Adrien Matta's avatar
Adrien Matta committed
175 176 177 178 179 180
  fitX=X;
  fitZ=Z;
  fitR=R;
  // assume all X,Z,R of same size
  unsigned int size = X.size();
  // Define the starting point of the fit: a straight line passing through the 
Adrien Matta's avatar
Adrien Matta committed
181
  // the first and last wire
Adrien Matta's avatar
Adrien Matta committed
182 183 184 185 186 187 188 189 190
  // z = ax+b -> x=(z-b)/a
  double a = fitZ[size-1]-fitZ[0]/(fitX[size-1]-fitX[0]);
  double b = fitZ[0]-a*fitX[0];
  double parameter[2]={a,b};
  static ROOT::Math::Minimizer* min = ROOT::Math::Factory::CreateMinimizer("Minuit2", "Migrad");
  static ROOT::Math::Functor f(this,&TSamuraiFDC2Physics::SumD,2); 
  min->SetFunction(f);
  min->SetVariable(0,"a",parameter[0], 1);
  min->SetVariable(1,"b",parameter[1], 1);
Adrien Matta's avatar
Adrien Matta committed
191
  //cout << "start" << endl;  
Adrien Matta's avatar
Adrien Matta committed
192 193 194
  min->Minimize(); 
  const double *xs = min->X();
  refX=(-xs[1])/xs[0];
Adrien Matta's avatar
Adrien Matta committed
195 196 197 198 199 200 201 202
  // compute the reference vector
  // M is reference point at Z=1
  double zM=xs[0]+xs[1];
  dirX=1;
  dirZ=zM;
  double n = sqrt(1+dirZ*dirZ);
  dirX/=n;
  dirZ/=n;
Adrien Matta's avatar
Adrien Matta committed
203 204 205 206 207 208
}
////////////////////////////////////////////////////////////////////////////////
double TSamuraiFDC2Physics::SumD(const double* parameter ){
  unsigned int size = fitX.size();
  // Compute the sum P of the distance between the circle and the track
  double P = 0;
Adrien Matta's avatar
Adrien Matta committed
209 210 211 212 213
  double a = parameter[0];
  double b = parameter[1];
  double ab= a*b;
  double a2=a*a;

Adrien Matta's avatar
Adrien Matta committed
214
  for(unsigned int i = 0 ; i < size ; i++){
Adrien Matta's avatar
Adrien Matta committed
215 216 217 218 219
    double c = fitX[i];
    double d = fitZ[i];
    double x = (a*d-ab+c)/(1+a2);
    double z = a*x+b;
    P+= sqrt(abs( (x-c)*(x-c)+(z-d)*(z-d)-fitR[i]*fitR[i]));
Adrien Matta's avatar
Adrien Matta committed
220
    }
Adrien Matta's avatar
Adrien Matta committed
221

Adrien Matta's avatar
Adrien Matta committed
222 223
  return P;
  }
Adrien Matta's avatar
Adrien Matta committed
224 225 226
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::PreTreat(){
  ClearPreTreatedData();
Adrien Matta's avatar
Adrien Matta committed
227 228 229 230 231 232 233
  static CalibrationManager* Cal = CalibrationManager::getInstance();
  static string channel;
   // one map per detector
  map<unsigned int, vector<double> > X ; 
  map<unsigned int, vector<double> > Z ; 
  map<unsigned int, vector<double> > R ; 

Adrien Matta's avatar
Adrien Matta committed
234 235
  unsigned int size = m_EventData->Mult();
  for(unsigned int i = 0 ; i < size ; i++){
Adrien Matta's avatar
Adrien Matta committed
236 237 238
    // EDGE=1 is the leading edge, IE, the real time.
    // EDGE=0 is the trailing edge, so it helps build Tot
    if(m_EventData->GetEdge(i)==1){
Adrien Matta's avatar
Adrien Matta committed
239 240 241 242 243 244 245
      int det   = m_EventData->GetDetectorNbr(i); 
      int layer = m_EventData->GetLayerNbr(i); 
      int wire  = m_EventData->GetWireNbr(i); 
      double time = m_EventData->GetTime(i);
      double etime = 0;
      // look for matching trailing edge   
      for(unsigned int j = 0 ; j < size ; j++){
Adrien Matta's avatar
Adrien Matta committed
246
        if(m_EventData->GetEdge(j)==0){
Adrien Matta's avatar
Adrien Matta committed
247 248 249 250 251 252 253 254
          int edet   = m_EventData->GetDetectorNbr(j); 
          int elayer = m_EventData->GetLayerNbr(j); 
          int ewire  = m_EventData->GetWireNbr(j); 
          // same wire
          if(wire==ewire && layer==elayer && det==edet){
            etime = m_EventData->GetTime(j); 
          }    
        }
Adrien Matta's avatar
Adrien Matta committed
255 256 257 258
        if(etime && etime>time)
          break;
        else
          etime=0;
Adrien Matta's avatar
Adrien Matta committed
259 260
      }
      // a valid wire must have an edge
Adrien Matta's avatar
Adrien Matta committed
261
      if(etime && time && etime-time>ToTThreshold){
Adrien Matta's avatar
Adrien Matta committed
262 263 264 265
        Detector.push_back(det);
        Layer.push_back(layer);       
        Wire.push_back(wire);
        Time.push_back(time);
Adrien Matta's avatar
Adrien Matta committed
266
        ToT.push_back(etime-time);
Adrien Matta's avatar
Adrien Matta committed
267 268
        channel="SamuraiFDC2/L" + NPL::itoa(layer);
        DriftLength.push_back(Cal->ApplySigmoid(channel,etime));
Adrien Matta's avatar
Adrien Matta committed
269
      }
Adrien Matta's avatar
Adrien Matta committed
270 271 272 273 274
    }

  }
  return;
}
Adrien Matta's avatar
Adrien Matta committed
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::RemoveNoise(){
  // Remove the noise by looking if a matching wire exist in the adjacent layer
  // this done by looking at the closest plane with the same orientation
  unsigned int size = Detector.size(); 
  for(unsigned int i = 0 ; i < size ; i++){
    bool match=false;
    int det = Detector[i];
    int layer = Layer[i];
    int wire = Wire[i];
    // look for matching adjacent wire   

    for(unsigned int j = 0 ; j < size ; j++){
      int adet = Detector[j];
      int alayer = Layer[j];
      int awire = Wire[j];
      bool blayer = false;
      if(layer%2==0){
        if(layer+1==alayer)
          blayer=true;
      }

      else{
        if(layer-1==alayer)
          blayer=true;
      }
Adrien Matta's avatar
Adrien Matta committed
301

Adrien Matta's avatar
Adrien Matta committed
302 303 304 305 306
      if(det==adet && blayer && abs(wire-awire)<=1){
        match=true;
        break;
      }
    }
Adrien Matta's avatar
Adrien Matta committed
307 308 309 310 311 312

    if(match)
      Matched.push_back(true);
    else
      Matched.push_back(false);
  }
Adrien Matta's avatar
Adrien Matta committed
313 314
  return;
}
Adrien Matta's avatar
Adrien Matta committed
315 316
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::Clear(){
Adrien Matta's avatar
Adrien Matta committed
317 318
  Mult=0;
  PosX=PosY=-10000;
Adrien Matta's avatar
Adrien Matta committed
319
  DriftLength.clear();
Adrien Matta's avatar
Adrien Matta committed
320 321 322 323 324 325 326
  Detector.clear();
  Layer.clear();
  Wire.clear();
  Time.clear();
  ToT.clear();
  ParticleDirection.clear();
  MiddlePosition.clear();
Adrien Matta's avatar
Adrien Matta committed
327
  Matched.clear();
Adrien Matta's avatar
Adrien Matta committed
328 329 330 331 332 333 334
}
///////////////////////////////////////////////////////////////////////////

////   Innherited from VDetector Class   ////

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::ReadConfiguration(NPL::InputParser parser){
Adrien Matta's avatar
Adrien Matta committed
335 336 337 338 339 340 341 342 343 344 345 346 347
  vector<NPL::InputBlock*> blocks = parser.GetAllBlocksWithToken("SAMURAIFDC2");
  if(NPOptionManager::getInstance()->GetVerboseLevel())
    cout << "//// " << blocks.size() << " detector(s) found " << endl; 

  vector<string> token= {"XML"};

  for(unsigned int i = 0 ; i < blocks.size() ; i++){
    cout << endl << "////  Samurai FDC2 (" << i+1 << ")" << endl;
    string xmlpath = blocks[i]->GetString("XML");
    NPL::XmlParser xml;
    xml.LoadFile(xmlpath);
    AddDC("SAMURAIFDC2",xml);
  }
Adrien Matta's avatar
Adrien Matta committed
348
}
Adrien Matta's avatar
Adrien Matta committed
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::AddDC(string name, NPL::XmlParser& xml){
  std::vector<NPL::block*> b = xml.GetAllBlocksWithName(name);  
  // FDC2 case
  if(name=="SAMURAIFDC2"){
    unsigned int det=2;
    unsigned int size = b.size();
    for(unsigned int i = 0 ; i < size ; i++){
      unsigned int layer = b[i]->AsInt("layer"); 
      unsigned int wire  = b[i]->AsInt("wireid"); 
      double X = b[i]->AsDouble("wirepos");  
      double Z = b[i]->AsDouble("wirez");  
      string sDir = b[i]->AsString("anodedir");
      double T=0;
      if(sDir=="X")
        T=0*deg;
      else if(sDir=="Y")
        T=90*deg;
      else if(sDir=="U")
        T=30*deg;
      else if(sDir=="V")
        T=-30*deg;
      else{
        cout << "ERROR: Unknown layer orientation for Samurai FDC2"<< endl;
        exit(1);
      }
Adrien Matta's avatar
Adrien Matta committed
376 377 378 379
      SamuraiDCIndex idx(det,layer,wire);
      Wire_X[idx]=X;
      Wire_Z[idx]=Z;
      Wire_T[idx]=T;
Adrien Matta's avatar
Adrien Matta committed
380 381 382 383
    }
  }
}

Adrien Matta's avatar
Adrien Matta committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::InitSpectra(){  
  //m_Spectra = new TSamuraiFDC2Spectra(m_NumberOfDetector);
}

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::FillSpectra(){  
  //  m_Spectra -> FillRawSpectra(m_EventData);
  //  m_Spectra -> FillPreTreatedSpectra(m_PreTreatedData);
  //  m_Spectra -> FillPhysicsSpectra(m_EventPhysics);
}
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::CheckSpectra(){  
  //  m_Spectra->CheckSpectra();  
}
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::ClearSpectra(){  
  // To be done
}
///////////////////////////////////////////////////////////////////////////
map< string , TH1*> TSamuraiFDC2Physics::GetSpectra() {
  /*  if(m_Spectra)
      return m_Spectra->GetMapHisto();
      else{
      map< string , TH1*> empty;
      return empty;
      }*/
  map< string , TH1*> empty;
  return empty;

} 

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::WriteSpectra(){
  // m_Spectra->WriteSpectra();
}
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::AddParameterToCalibrationManager(){
Adrien Matta's avatar
Adrien Matta committed
422 423 424 425 426 427 428
  CalibrationManager* Cal = CalibrationManager::getInstance();

  // each layer
  for( int l = 0 ; l < 14 ; ++l){
    Cal->AddParameter("SamuraiFDC2", "L"+ NPL::itoa(l),"FDC2_L"+ NPL::itoa(l));
  }

Adrien Matta's avatar
Adrien Matta committed
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::InitializeRootInputRaw(){
  TChain* inputChain = RootInput::getInstance()->GetChain()   ;
  inputChain->SetBranchStatus( "SamuraiFDC2" , true );
  // The following line is necessary only for system were the tree is splitted
  // (older root version). The found argument silenced the Branches not found
  // warning for non splitted tree.
  if(inputChain->FindBranch("fDC_*"))
    inputChain->SetBranchStatus( "fDC_*",true);
  inputChain->SetBranchAddress( "SamuraiFDC2" , &m_EventData );

}

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::InitializeRootInputPhysics(){
}

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::InitializeRootOutput(){
  TTree* outputTree = RootOutput::getInstance()->GetTree();
  outputTree->Branch( "SamuraiFDC2" , "TSamuraiFDC2Physics" , &m_EventPhysics );
}

////////////////////////////////////////////////////////////////////////////////
//            Construct Method to be pass to the DetectorFactory              //
////////////////////////////////////////////////////////////////////////////////
NPL::VDetector* TSamuraiFDC2Physics::Construct(){
  return (NPL::VDetector*) new TSamuraiFDC2Physics();
}

////////////////////////////////////////////////////////////////////////////////
//            Registering the construct method to the factory                 //
////////////////////////////////////////////////////////////////////////////////
extern "C"{
  class proxy_samuraiFDC2{
    public:
      proxy_samuraiFDC2(){
        NPL::DetectorFactory::getInstance()->AddToken("SAMURAIFDC2","Samurai");
        NPL::DetectorFactory::getInstance()->AddDetector("SAMURAIFDC2",TSamuraiFDC2Physics::Construct);
      }
  };

  proxy_samuraiFDC2 p_samuraiFDC2;
}