TSamuraiFDC2Physics.cxx 13 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 67 68 69
  // Map[detector & plane angle, vector of spatial information]
  map<std::pair<unsigned int,double>, vector<double> > X ; 
  map<std::pair<unsigned int,double>, vector<double> > Z ; 
  map<std::pair<unsigned int,double>, vector<double> > R ; 
Adrien Matta's avatar
Adrien Matta committed
70 71
  unsigned int size = Detector.size();
  for(unsigned int i = 0 ; i < size ; i++){
Adrien Matta's avatar
Adrien Matta committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    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;
  for(auto it = X.begin();it!=X.end();++it){
    if(it->first.second==0)
      Track2D(X[it->first],Z[it->first],R[it->first],dirX,dirZ,PosX); 
Adrien Matta's avatar
Adrien Matta committed
89 90
    }

Adrien Matta's avatar
Adrien Matta committed
91 92 93
  return;
}

Adrien Matta's avatar
Adrien Matta committed
94 95
///////////////////////////////////////////////////////////////////////////
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
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  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 
  // the two first and last wire
  // 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
112
  
Adrien Matta's avatar
Adrien Matta committed
113 114 115 116 117 118 119 120 121 122 123 124 125
  min->Minimize(); 
  const double *xs = min->X();
  refX=(-xs[1])/xs[0];
}
////////////////////////////////////////////////////////////////////////////////
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;
  for(unsigned int i = 0 ; i < size ; i++){
    double x=(fitZ[i]-parameter[1])/parameter[0];
    P+= sqrt(abs( (x-fitX[i])*(x-fitX[i])-fitR[i]*fitR[i]));
    }
Adrien Matta's avatar
Adrien Matta committed
126

Adrien Matta's avatar
Adrien Matta committed
127 128
  return P;
  }
Adrien Matta's avatar
Adrien Matta committed
129 130 131
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::PreTreat(){
  ClearPreTreatedData();
Adrien Matta's avatar
Adrien Matta committed
132 133 134 135 136 137 138
  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
139 140
  unsigned int size = m_EventData->Mult();
  for(unsigned int i = 0 ; i < size ; i++){
Adrien Matta's avatar
Adrien Matta committed
141 142 143
    // 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
144 145 146 147 148 149 150
      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
151
        if(m_EventData->GetEdge(j)==0){
Adrien Matta's avatar
Adrien Matta committed
152 153 154 155 156 157 158 159
          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
160 161 162 163
        if(etime && etime>time)
          break;
        else
          etime=0;
Adrien Matta's avatar
Adrien Matta committed
164 165
      }
      // a valid wire must have an edge
Adrien Matta's avatar
Adrien Matta committed
166
      if(etime && time && etime-time>ToTThreshold){
Adrien Matta's avatar
Adrien Matta committed
167 168 169 170
        Detector.push_back(det);
        Layer.push_back(layer);       
        Wire.push_back(wire);
        Time.push_back(time);
Adrien Matta's avatar
Adrien Matta committed
171
        ToT.push_back(etime-time);
Adrien Matta's avatar
Adrien Matta committed
172 173
        channel="SamuraiFDC2/L" + NPL::itoa(layer);
        DriftLength.push_back(Cal->ApplySigmoid(channel,etime));
Adrien Matta's avatar
Adrien Matta committed
174
      }
Adrien Matta's avatar
Adrien Matta committed
175 176 177 178 179
    }

  }
  return;
}
Adrien Matta's avatar
Adrien Matta committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
///////////////////////////////////////////////////////////////////////////
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
206

Adrien Matta's avatar
Adrien Matta committed
207 208 209 210 211
      if(det==adet && blayer && abs(wire-awire)<=1){
        match=true;
        break;
      }
    }
Adrien Matta's avatar
Adrien Matta committed
212 213 214 215 216 217

    if(match)
      Matched.push_back(true);
    else
      Matched.push_back(false);
  }
Adrien Matta's avatar
Adrien Matta committed
218 219
  return;
}
Adrien Matta's avatar
Adrien Matta committed
220 221
///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::Clear(){
Adrien Matta's avatar
Adrien Matta committed
222 223
  PosX=-10000;
  DriftLength.clear();
Adrien Matta's avatar
Adrien Matta committed
224 225 226 227 228 229 230
  Detector.clear();
  Layer.clear();
  Wire.clear();
  Time.clear();
  ToT.clear();
  ParticleDirection.clear();
  MiddlePosition.clear();
Adrien Matta's avatar
Adrien Matta committed
231
  Matched.clear();
Adrien Matta's avatar
Adrien Matta committed
232 233 234 235 236 237 238
}
///////////////////////////////////////////////////////////////////////////

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

///////////////////////////////////////////////////////////////////////////
void TSamuraiFDC2Physics::ReadConfiguration(NPL::InputParser parser){
Adrien Matta's avatar
Adrien Matta committed
239 240 241 242 243 244 245 246 247 248 249 250 251
  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
252
}
Adrien Matta's avatar
Adrien Matta committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

///////////////////////////////////////////////////////////////////////////
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
280 281 282 283
      SamuraiDCIndex idx(det,layer,wire);
      Wire_X[idx]=X;
      Wire_Z[idx]=Z;
      Wire_T[idx]=T;
Adrien Matta's avatar
Adrien Matta committed
284 285 286 287
    }
  }
}

Adrien Matta's avatar
Adrien Matta committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
///////////////////////////////////////////////////////////////////////////
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
326 327 328 329 330 331 332
  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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 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 376 377 378 379
}

///////////////////////////////////////////////////////////////////////////
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;
}