]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDtrapConfigHandler.cxx
bugfix: '-nextevent' command in configuration file for publisher inserted correctly...
[u/mrichter/AliRoot.git] / TRD / AliTRDtrapConfigHandler.cxx
index 9ee5918c14d42c5360b8fae101e2487d6b115a19..2af3857b893e03874ea8bd5127b98cab17b507df 100644 (file)
@@ -1,4 +1,3 @@
-
 /**************************************************************************
  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
  *                                                                        *
 //                                                                        //
 ////////////////////////////////////////////////////////////////////////////
 
-
-
 #include "AliTRDtrapConfigHandler.h"
 
 #include <iostream>
+#include <sstream>
 #include <iomanip>
 
-#include "AliTRDpadPlane.h"
-#include "AliTRDtrapConfig.h"
-#include "AliTRDcalibDB.h"
-#include "AliTRDCommonParam.h"
 #include "AliLog.h"
 
-#include "AliTRDarrayDictionary.h"
-
-#include "AliMagF.h"
-#include "TGeoGlobalMagField.h"
-#include "AliMagWrapCheb.h"
+#include "AliTRDtrapConfig.h"
 #include "AliTRDgeometry.h"
+#include "AliTRDcalibDB.h"
 
 #include "TMath.h"
 #include "TGeoMatrix.h"
+#include "TGraph.h"
+
+#include "Cal/AliTRDCalOnlineGainTable.h"
+#include "Cal/AliTRDCalOnlineGainTableROC.h"
+#include "Cal/AliTRDCalOnlineGainTableMCM.h"
 
 using namespace std;
 
 ClassImp(AliTRDtrapConfigHandler)
 
-
 AliTRDtrapConfigHandler::AliTRDtrapConfigHandler() :
-     fGeo(NULL)
-     , fDet(0)
-     , fBField(0)
-     , fOmegaTau(1)
-     , fPtMin(0)
-     , fNTimebins(0)
-     , fScaleQ0(1)
-     , fScaleQ1(1)
-     , fPidTracklengthCorr(kFALSE)
-     , fTiltCorr(kTRUE)
+     ltuParam()
+     , fRestrictiveMask((0x3ffff << 11) | (0x1f << 6) | 0x3f)
+     , fGtbl()
 {
-   fGeo = new AliTRDgeometry;
+
 }
 
 
 AliTRDtrapConfigHandler::~AliTRDtrapConfigHandler()
 {
-   delete fGeo;
+
 }
 
 void AliTRDtrapConfigHandler::ResetMCMs()
@@ -83,7 +71,127 @@ void AliTRDtrapConfigHandler::ResetMCMs()
 }
 
 
-Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename, Int_t det)
+Int_t AliTRDtrapConfigHandler::LoadConfig()
+{
+  // load a default configuration which is suitable for simulation
+  // for a detailed description of the registers see the TRAP manual
+  // if you want to resimulate tracklets on real data use the appropriate config instead
+
+  AliTRDtrapConfig *cfg = AliTRDtrapConfig::Instance();
+
+  // HC header configuration bits
+  cfg->SetTrapReg(AliTRDtrapConfig::kC15CPUA, 0x2102); // zs, deh
+
+  // no. of timebins
+  cfg->SetTrapReg(AliTRDtrapConfig::kC13CPUA, 24);
+
+  // pedestal filter
+  cfg->SetTrapReg(AliTRDtrapConfig::kFPNP, 4*10);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFPTC, 0);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFPBY, 0); // bypassed!
+
+  // gain filter
+  for (Int_t adc = 0; adc < 20; adc++) {
+    cfg->SetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGA0+adc), 40);
+    cfg->SetTrapReg(AliTRDtrapConfig::TrapReg_t(AliTRDtrapConfig::kFGF0+adc), 15);
+  }
+  cfg->SetTrapReg(AliTRDtrapConfig::kFGTA, 20);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFGTB, 2060);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFGBY, 0);  // bypassed!
+
+  // tail cancellation
+  cfg->SetTrapReg(AliTRDtrapConfig::kFTAL, 200);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFTLL, 0);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFTLS, 200);
+  cfg->SetTrapReg(AliTRDtrapConfig::kFTBY, 0);
+
+  // tracklet calculation
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPQS0, 5);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPQE0, 10);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPQS1, 11);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPQE1, 20);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPFS, 5);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPFE, 20);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPVBY, 0);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPVT, 10);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPHT, 150);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPFP, 40);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPCL, 1);
+  cfg->SetTrapReg(AliTRDtrapConfig::kTPCT, 10);
+
+  // ndrift (+ 5 binary digits)
+  ltuParam.SetNtimebins(20 << 5);
+  // deflection + tilt correction
+  ltuParam.SetRawOmegaTau(0.16133);
+  // deflection range table
+  ltuParam.SetRawPtMin(0.1);
+  // magnetic field
+  ltuParam.SetRawMagField(0.0);
+  // scaling factors for q0, q1
+  ltuParam.SetRawScaleQ0(0);
+  ltuParam.SetRawScaleQ1(0);
+  // disable length correction and tilting correction
+  ltuParam.SetRawLengthCorrectionEnable(kFALSE);
+  ltuParam.SetRawTiltCorrectionEnable(kFALSE);
+
+  // apply ltuParams to all detectors
+  for(Int_t det=0; det<AliTRDgeometry::Ndet(); det++) {
+     ConfigureDyCorr(det);
+     ConfigureDRange(det); // deflection range
+     ConfigureNTimebins(det);  // timebins in the drift region
+     ConfigurePIDcorr(det);  // scaling parameters for the PID
+  }
+
+  // ****** hit position LUT
+
+  // now calculate it from PRF
+  AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
+
+  Double_t padResponse[3]; // pad response left, central, right
+  Double_t padResponseR[3]; // pad response left, central, right
+  Double_t padResponseL[3]; // pad response left, central, right
+
+  for (Int_t iBin = 0; iBin < 128; iBin++)
+    cfg->SetTrapReg((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + iBin), 0, 0, 0, 0);
+
+  for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
+    TGraph gr(128);
+    for (Int_t iBin = 0; iBin < 256*0.5; iBin++) {
+      cal->PadResponse(1., iBin*1./256.,    iLayer, padResponse);
+      cal->PadResponse(1., iBin*1./256.-1., iLayer, padResponseR);
+      cal->PadResponse(1., iBin*1./256.+1., iLayer, padResponseL);
+      gr.SetPoint(iBin, (0.5 * (padResponseR[1] - padResponseL[1])/padResponse[1] * 256), iBin);
+    }
+    for (Int_t iBin = 0; iBin < 128; iBin++) {
+      Int_t corr = (Int_t) (gr.Eval(iBin)) - iBin;
+      if (corr < 0)
+        corr = 0;
+      else if (corr > 31)
+        corr = 31;
+      for (Int_t iStack = 0; iStack < 540/6; iStack++) {
+        cfg->SetTrapReg((AliTRDtrapConfig::TrapReg_t) (AliTRDtrapConfig::kTPL00 + iBin), corr, 6*iStack + iLayer);
+      }
+    }
+  }
+  // ****** hit position LUT configuration end
+
+  // event buffer
+  cfg->SetTrapReg(AliTRDtrapConfig::kEBSF, 1);  // 0: store filtered; 1: store unfiltered
+
+  // zs applied to data stored in event buffer (sel. by EBSF)
+  cfg->SetTrapReg(AliTRDtrapConfig::kEBIS, 15 << 2); // single indicator threshold (plus two digits)
+  cfg->SetTrapReg(AliTRDtrapConfig::kEBIT, 30 << 2); // sum indicator threshold (plus two digits)
+  cfg->SetTrapReg(AliTRDtrapConfig::kEBIL, 0xf0);   // lookup table
+  cfg->SetTrapReg(AliTRDtrapConfig::kEBIN, 0);      // neighbour sensitivity
+
+  // raw data
+  cfg->SetTrapReg(AliTRDtrapConfig::kNES, (0x0000 << 16) | 0x1000);
+
+  return 0;
+}
+
+
+Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename)
 {
    //
   // load a TRAP configuration from a file
@@ -93,8 +201,6 @@ Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename, Int_t det)
    // which are two tools to inspect/export configurations from wingDB
    //
 
-
-   fDet = det;
    Int_t ignoredLines=0;
    Int_t ignoredCmds=0;
    Int_t readLines=0;
@@ -113,24 +219,92 @@ Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename, Int_t det)
    UInt_t cmd;
    Int_t extali, addr, data;
 
-   while(infile.good()) {
+   // reset restrictive mask
+   fRestrictiveMask = (0x3ffff << 11) | (0x1f << 6) | 0x3f;
+   char linebuffer[512];
+   istringstream line;
+
+   while(infile.getline(linebuffer, 512) && infile.good()) {
+      line.clear();
+      line.str(linebuffer);
       cmd=999;
       extali=-1;
       addr=-1;
       data=-1;
-      infile >> std::skipws >> cmd >> addr >> data >> extali;
-      //      std::cout << "no: " << no << ", cmd " << cmd << ", extali " << extali << ", addr " << addr << ", data " << data <<  endl;
+      line >> std::skipws >> cmd >> addr >> data >> extali;  // the lines read from config file can contain additional columns.
+      // Therefore the detour via istringstream
+
+      if(cmd!=999 && addr != -1 && data!= -1 && extali!=-1) {
+
+        if(cmd==fgkScsnCmdWrite) {
+           for(Int_t det=0; det<AliTRDgeometry::Ndet(); det++) {
+              UInt_t rocpos = (1 << (AliTRDgeometry::GetSector(det)+11)) | (1 << (AliTRDgeometry::GetStack(det)+6)) | (1 << AliTRDgeometry::GetLayer(det));
+              AliDebug(1, Form("checking restriction: mask=0x%08x, rocpos=0x%08x", fRestrictiveMask, rocpos));
+              if ((fRestrictiveMask & rocpos) == rocpos) {
+                AliDebug(1, Form("match: %i %i %i %i", cmd, extali, addr, data));
+                 cfg->AddValues(det, cmd, extali, addr, data);
+              }
+           }
+        }
 
-      if(cmd!=999 && extali!=-1 && addr != -1 && data!= -1 && extali!=-1) {
-        if(cmd==fgkScsnCmdWrite)
-           cfg->AddValues(det, cmd, extali, addr, data);
-        else if(cmd == fgkScsnLTUparam)
+        else if(cmd == fgkScsnLTUparam) {
            ProcessLTUparam(extali, addr, data);
-        else
+        }
+
+        else if(cmd == fgkScsnCmdRestr) {
+           fRestrictiveMask = data;
+          AliDebug(1, Form("updated restrictive mask to 0x%08x", fRestrictiveMask));
+        }
+
+        else if((cmd == fgkScsnCmdReset) ||
+                (cmd == fgkScsnCmdRobReset)) {
+          cfg->ResetRegs();
+        }
+
+        else if (cmd == fgkScsnCmdSetHC) {
+          Int_t fullVersion = ((data & 0x7F00) >> 1) | (data & 0x7f);
+
+          for (Int_t iDet = 0; iDet < AliTRDgeometry::Ndet(); iDet++) {
+            Int_t smls = (AliTRDgeometry::GetSector(iDet) << 6) | (AliTRDgeometry::GetLayer(iDet) << 3) | AliTRDgeometry::GetStack(iDet);
+
+            for (Int_t iRob = 0; iRob < 8; iRob++) {
+              // HC mergers
+              cfg->SetTrapReg(AliTRDtrapConfig::kC14CPUA, 0xc << 16, iDet, iRob, 17);
+              cfg->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, 17);
+
+              // board mergers
+              cfg->SetTrapReg(AliTRDtrapConfig::kC14CPUA, 0, iDet, iRob, 16);
+              cfg->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, 16);
+
+              // and now for the others
+              for (Int_t iMcm = 0; iMcm < 16; iMcm++) {
+                cfg->SetTrapReg(AliTRDtrapConfig::kC14CPUA, iMcm | (iRob << 4) | (3 << 16), iDet, iRob, iMcm);
+                cfg->SetTrapReg(AliTRDtrapConfig::kC15CPUA, ((1<<29) | (fullVersion<<15) | (1<<12) | (smls<<1) | (iRob%2)), iDet, iRob, iMcm);
+              }
+            }
+          }
+        }
+
+        else if((cmd == fgkScsnCmdRead) ||
+                (cmd == fgkScsnCmdPause) ||
+                (cmd == fgkScsnCmdPtrg) ||
+                (cmd == fgkScsnCmdHwPtrg) ||
+                (cmd == fgkScsnCmdRobPower) ||
+                (cmd == fgkScsnCmdTtcRx) ||
+                (cmd == fgkScsnCmdMcmTemp) ||
+                (cmd == fgkScsnCmdOri) ||
+                (cmd == fgkScsnCmdPM) ) {
+          AliDebug(2, Form("ignored SCSN command: %i %i %i %i", cmd, addr, data, extali));
+        }
+
+        else {
+            AliWarning(Form("unknown SCSN command: %i %i %i %i", cmd, addr, data, extali));
            ignoredCmds++;
+        }
 
         readLines++;
       }
+
       else if(!infile.eof() && !infile.good()) {
         infile.clear();
         infile.ignore(256, '\n');
@@ -155,6 +329,13 @@ Int_t AliTRDtrapConfigHandler::LoadConfig(TString filename, Int_t det)
 
 
 
+Int_t AliTRDtrapConfigHandler::SetGaintable(AliTRDCalOnlineGainTable const &gtbl)
+{
+   fGtbl=gtbl;
+   return 0;
+}
+
+
 void AliTRDtrapConfigHandler::ProcessLTUparam(Int_t dest, Int_t addr, UInt_t data)
 {
    //
@@ -165,24 +346,26 @@ void AliTRDtrapConfigHandler::ProcessLTUparam(Int_t dest, Int_t addr, UInt_t dat
    switch (dest) {
 
    case 0: // set the parameters in AliTRDtrapConfig
-      ConfigureDyCorr();
-      ConfigureDRange(); // deflection range
-      ConfigureNTimebins();  // timebins in the drift region
-      ConfigurePIDcorr();  // scaling parameters for the PID
+      for(Int_t det=0; det<AliTRDgeometry::Ndet(); det++) {
+        ConfigureDyCorr(det);
+        ConfigureDRange(det); // deflection range
+        ConfigureNTimebins(det);  // timebins in the drift region
+        ConfigurePIDcorr(det);  // scaling parameters for the PID
+      }
       break;
 
    case 1: // set variables
       switch (addr) {
 
-      case 0: fPtMin =  float(data) / 1000.; break; // pt_min in GeV/c (*1000)
-      case 1: fBField = float(data) / 1000. ; break; // B in T (*1000)
-      case 2: fOmegaTau = float(data) / 1.0E6  ; break; // omega*tau
-      case 3: fNTimebins = data; break;
+      case 0: ltuParam.SetPtMin(data); break; // pt_min in GeV/c (*1000)
+      case 1: ltuParam.SetMagField(data); break; // B in T (*1000)
+      case 2: ltuParam.SetOmegaTau(data); break; // omega*tau
+      case 3: ltuParam.SetNtimebins(data); break;
        // ntimbins: drift time (for 3 cm) in timebins (5 add. bin. digits)
-      case 4: fScaleQ0 = data; break;
-      case 5: fScaleQ1 = data; break;
-      case 6: fPidTracklengthCorr = (bool) data; break;
-      case 7: fTiltCorr = (bool) data; break;
+      case 4: ltuParam.SetScaleQ0(data); break;
+      case 5: ltuParam.SetScaleQ1(data); break;
+      case 6: ltuParam.SetLengthCorrectionEnable(data); break;
+      case 7: ltuParam.SetTiltCorrectionEnable(data); break;
       }
       break;
 
@@ -193,17 +376,17 @@ void AliTRDtrapConfigHandler::ProcessLTUparam(Int_t dest, Int_t addr, UInt_t dat
 }
 
 
-void AliTRDtrapConfigHandler::ConfigureNTimebins()
+void AliTRDtrapConfigHandler::ConfigureNTimebins(Int_t det)
 {
    //
    // Set timebins in the drift region
    //
-   AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, 127, AliTRDtrapConfig::fgkDmemAddrNdrift, fNTimebins);
+  AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, 127, AliTRDtrapConfig::fgkDmemAddrNdrift, ltuParam.GetNtimebins());
 }
 
 
 
-void AliTRDtrapConfigHandler::ConfigureDyCorr()
+void AliTRDtrapConfigHandler::ConfigureDyCorr(Int_t det)
 {
    //
    //  Deflection length correction
@@ -211,48 +394,22 @@ void AliTRDtrapConfigHandler::ConfigureDyCorr()
    //  This correction is in units of padwidth / (256*32)
    //
 
-   Int_t nRobs;
-   if(fGeo->GetStack(fDet) == 2)
-      nRobs=6;
-   else
-      nRobs=8;
+   Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
 
-   Double_t dyLorentz = - fOmegaTau * fGeo->CdrHght();    // CdrHght: Height of the drift region
-   Double_t globalPos[3];
-   Double_t tiltingAngle = fGeo->GetPadPlane(fDet)->GetTiltingAngle();
-   Double_t scalePad = 256. * 32.;
-   Int_t dyCorrInt=0;
-
-    for (Int_t r = 0; r < nRobs; r++) {
-       for (Int_t m = 0; m < 16; m++) {
-          if(GetPadPosNonRot(r, m, 9, globalPos)==0) {
-             double dyTilt = ( fGeo->CdrHght() *  TMath::Tan(tiltingAngle * TMath::DegToRad()) * globalPos[2]/globalPos[0]);
-
-             double dyCorr;
-             if(fTiltCorr==kTRUE)
-                dyCorr = dyLorentz + dyTilt;
-             else
-                dyCorr = dyTilt;
-
-
-             dyCorrInt =  TMath::Nint(dyCorr * scalePad /  fGeo->GetPadPlane(fDet)->GetWidthIPad());  // The correction is in units of 1/256 of the
-                                                                                                      // pad width, including 5 binary digits
-          }
-          else {
-             AliError("No transformation matrix available");
-             dyCorrInt=0;
-          }
-          Int_t dest =  1<<10 | r<<7 | m;
-          AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, dest, AliTRDtrapConfig::fgkDmemAddrDeflCorr, dyCorrInt);
-       }
+  for (Int_t r = 0; r < nRobs; r++) {
+    for (Int_t m = 0; m < 16; m++) {
+      Int_t dest =  1<<10 | r<<7 | m;
+      Int_t dyCorrInt = ltuParam.GetDyCorrection(det, r, m);
+      AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, dest, AliTRDtrapConfig::fgkDmemAddrDeflCorr, dyCorrInt);
     }
+  }
 }
 
 
 
 
 
-void AliTRDtrapConfigHandler::ConfigureDRange()
+void AliTRDtrapConfigHandler::ConfigureDRange(Int_t det)
 {
    //
    // deflection range LUT
@@ -261,72 +418,23 @@ void AliTRDtrapConfigHandler::ConfigureDRange()
    // deflection (-64..63) is used
    //
 
-   static const int x=0;
-   static const int y=1;
-   static const Double_t dyBin = 140e-6;
+  Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
 
-   Int_t dyMinInt=fgkDyMinCut;
-   Int_t dyMaxInt=fgkDyMaxCut;
-   Double_t mcmPos[3];
-
-   Int_t nRobs=-1;
-   if(fGeo->GetStack(fDet) == 2)
-      nRobs=6;
-   else
-      nRobs=8;
+  Int_t dyMinInt;
+  Int_t dyMaxInt;
 
    for (Int_t r = 0; r < nRobs; r++) {
       for (Int_t m = 0; m < 16; m++) {
         for (Int_t c = 0; c < 18; c++) {
 
-           if(fPtMin<0.1) {
-              dyMinInt=fgkDyMinCut;
-              dyMaxInt=fgkDyMaxCut;
-           }
-           else {
-              if(GetPadPosNonRot(r, m, c, mcmPos)==0) {
-                 Double_t radius = fPtMin/(0.3*fBField);
-
-                 double vertexPos[2] = {0,0};
-
-                 Double_t distanceX = (vertexPos[x]-mcmPos[x]) / 100.; // cm -> m
-                 Double_t distanceY = (vertexPos[y]-mcmPos[y]) / 100.; // cm -> m
-
-                 Double_t maxDeflTemp = (TMath::Sqrt( Square(distanceX) + Square(distanceY)) / 2) / radius;
-                 Double_t localPhi = TMath::ATan2(distanceY, distanceX);
-
-                 Double_t maxDeflAngle=0;
-                 if(maxDeflTemp < 1. ) {
-                    maxDeflAngle = TMath::ASin(maxDeflTemp);
-                    Double_t dyMin = fGeo->CdrHght()/100. * TMath::Tan(localPhi - maxDeflAngle);  // CdrHght: Height of the drift region in cm
-                    Double_t dyMax = fGeo->CdrHght()/100. * TMath::Tan(localPhi + maxDeflAngle);
-
-                    dyMinInt = Int_t (dyMin / dyBin);
-                    dyMaxInt = Int_t (dyMax / dyBin);
-
-                    if(dyMinInt < fgkDyMinCut)
-                       dyMinInt = fgkDyMinCut;
-                    if(dyMaxInt > fgkDyMaxCut)
-                       dyMaxInt = fgkDyMaxCut;
-                 }
-                 else {
-                    dyMinInt = fgkDyMinCut;
-                    dyMaxInt = fgkDyMaxCut;
-                 }
-
-//               cout << "maxdefl: " << maxDeflAngle << ", localPhi " << localPhi << endl;
-//               cout << "r " << r << ", m" << m << ", c " << c << ", min angle: " << localPhi-maxDeflAngle << ", max: " << localPhi+maxDeflAngle
-//                    << ", min int: " << dyMinInt << ", max int: " << dyMaxInt << endl;
-              }
-              else {
-                 AliError("No geometry model loaded\n");
-              }
-           }
-
-           Int_t dest =  1<<10 | r<<7 | m;
-           Int_t lutAddr = AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*c;
-           AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, dest, lutAddr+0, dyMinInt);
-           AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, dest, lutAddr+1, dyMaxInt);
+          // cout << "maxdefl: " << maxDeflAngle << ", localPhi " << localPhi << endl;
+          // cout << "r " << r << ", m" << m << ", c " << c << ", min angle: " << localPhi-maxDeflAngle << ", max: " << localPhi+maxDeflAngle
+          //   << ", min int: " << dyMinInt << ", max int: " << dyMaxInt << endl;
+          Int_t dest =  1<<10 | r<<7 | m;
+          Int_t lutAddr = AliTRDtrapConfig::fgkDmemAddrDeflCutStart + 2*c;
+          ltuParam.GetDyRange(det, r, m, c, dyMinInt, dyMaxInt);
+          AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, dest, lutAddr+0, dyMinInt);
+          AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, dest, lutAddr+1, dyMaxInt);
         }
       }
    }
@@ -338,19 +446,19 @@ void AliTRDtrapConfigHandler::PrintGeoTest()
    // Prints some information about the geometry. Only for debugging
    //
 
-   Double_t mcmPos[3];
    int sm=0;
    //   for(int sm=0; sm<6; sm++) {
    for(int stack=0; stack<5; stack++) {
       for(int layer=0; layer<6; layer++) {
 
-        fDet = sm*30+stack*6+layer;
+        Int_t det = sm*30+stack*6+layer;
         for (Int_t r = 0; r < 6; r++) {
            for (Int_t m = 0; m < 16; m++) {
               for (Int_t c = 7; c < 8; c++) {
-                 GetPadPosNonRot(r, m, c, mcmPos);
-                 cout << stack << ";" << layer << ";" << r << ";" << m
-                      << ";" << mcmPos[0] << ";" << mcmPos[1] << ";" << mcmPos[2] << endl;
+                cout << stack << ";" << layer << ";" << r << ";" << m
+                     << ";" << ltuParam.GetX(det, r, m)
+                     << ";" << ltuParam.GetLocalY(det, r, m, c)
+                     << ";" << ltuParam.GetLocalZ(det, r, m) << endl;
               }
            }
         }
@@ -360,8 +468,7 @@ void AliTRDtrapConfigHandler::PrintGeoTest()
 }
 
 
-
-void AliTRDtrapConfigHandler::ConfigurePIDcorr()
+void AliTRDtrapConfigHandler::ConfigurePIDcorr(Int_t det)
 {
    //
    // Calculate the MCM individual correction factors for the PID
@@ -374,127 +481,17 @@ void AliTRDtrapConfigHandler::ConfigurePIDcorr()
    UInt_t cor0;
    UInt_t cor1;
 
-   Double_t globalPos[3];
-
-   Int_t nRobs=-1;
-   if(fGeo->GetStack(fDet) == 2)
-      nRobs=6;
-   else
-      nRobs=8;
+   Int_t nRobs = AliTRDgeometry::GetStack(det) == 2 ? 6 : 8;
 
    for (Int_t r=0; r<nRobs; r++) {
       for(Int_t m=0; m<16; m++) {
-
-        if(GetPadPosNonRot(r, m, 9, globalPos)==0) {
-           Double_t elongation = TMath::Abs(TMath::Sqrt(globalPos[0]*globalPos[0] + globalPos[1]*globalPos[1] + globalPos[2]*globalPos[2]) / globalPos[0]);
-
-           if(fPidTracklengthCorr==kFALSE) {
-              cor0 = fScaleQ0;
-              cor1 = fScaleQ1;
-           }
-           else {
-              cor0 = Int_t ((1.0*fScaleQ0* (1/elongation) ));
-              cor1 = Int_t ((1.0*fScaleQ1* (1/elongation) ));
-           }
-
-           Int_t dest =  1<<10 | r<<7 | m;
-           AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, dest, addrLUTcor0, cor0);
-           AliTRDtrapConfig::Instance()->AddValues(fDet, fgkScsnCmdWrite, dest, addrLUTcor1, cor1);
-        }
-        else {
-           AliError("No transformation matrix available");
-        }
-      }
-   }
-}
-
-
-
-Int_t AliTRDtrapConfigHandler::GetPadPosNonRot(Int_t rob, Int_t mcm, Int_t channel, Double_t trackCoor[3])
-{
-   //
-   // Calcutate the gobal coordinates for an mcm channel in the supermodule at position -0.5
-   //
-
-   Int_t stack = fGeo->GetStack(fDet);
-   Int_t layer = fGeo->GetLayer(fDet);
-
-   AliTRDpadPlane *plane = fGeo->GetPadPlane(layer, stack);
-   if(plane==NULL) {
-      AliError(Form("stack %i, layer %i, det %i", stack, layer, fDet));
-      return 1;
-   }
-
-   Double_t locYZ[2];
-   Double_t loc[3];
-
-   Double_t radialPos = fGeo->AnodePos()-0.83; //
-   //   Double_t radialPos = 300.65 + 12.60 * layer; // cm // taken from libTRD/geometry/geometry.cc, probably not the final value
-
-   GetLocalPadPos(plane, rob, mcm, channel, locYZ);
-
-   loc[0] = radialPos;
-   loc[1] = locYZ[0];
-   loc[2] = locYZ[1];
-
-   //transform from loc[3] - coordinates of the pad
-   // Go to tracking coordinates
-
-   TGeoHMatrix *fMatrix  = fGeo->GetClusterMatrix(fDet);
-   if(fMatrix==NULL) {
-      AliError(Form("stack %i, layer %i, det %i", stack, layer, fDet));
-      return 2;
-   }
-   fMatrix->LocalToMaster(loc, trackCoor);
-   return 0;
-}
-
-
-void AliTRDtrapConfigHandler::GetLocalPadPos(AliTRDpadPlane *plane, Int_t rob, Int_t mcm, Int_t channel, Double_t result[2])
-{
-   //
-   // calculate the local coordinates for an mcm channel
-   //
-
-   Double_t localY, localZ;
-
-    Int_t padCol;
-    if(rob%2 == 0) //side a
-       padCol  = (mcm % fgkMCMperROBCol) * fgkPadsPerMCM + channel;
-    else
-       padCol  = (mcm % fgkMCMperROBCol) * fgkPadsPerMCM + (plane->GetNcols()/2) + channel;
-
-    Int_t padRow = ((Int_t) floor(rob/2.0)) * fgkMCMperROBRow + ((Int_t) floor(mcm/4));
-
-    if(padCol<0 || padCol>= plane->GetNcols())
-       AliError(Form("Invalid pad col: %i\n", padCol));
-
-    if(padRow<0 || padRow>= plane->GetNrows())
-       AliError(Form("Invalid pad row: %i\n", padRow));
-
-    if(padCol+1 == plane->GetNcols()) // last pad
-       localY = plane->GetColPos(padCol) + (plane->GetColEnd()-plane->GetColPos(padCol))/2;
-    else
-       localY = plane->GetColPos(padCol) + (plane->GetColPos(padCol+1)-plane->GetColPos(padCol))/2;
-
-    if(padRow+1 == plane->GetNrows())
-       localZ = plane->GetRowPosROC(padRow) + (plane->GetRowEndROC() - plane->GetRowPosROC(padRow))/2;
-    else
-       localZ = plane->GetRowPosROC(padRow) + (plane->GetRowPosROC(padRow+1) - plane->GetRowPosROC(padRow))/2;
-
-    //    std::cout << "pad col " << padCol << ", pad row " << padRow << std::endl;
-    //    std::cout << "pos y (col) " << localY << ", pos z (row) " << localZ << std::endl;
-
-    result[0]=localY;
-    result[1]=localZ;
-}
-
-
-Double_t AliTRDtrapConfigHandler::Square(Double_t val)
-{
-   //
-   // calculate the square of the argument
-   //
-
-   return val*val;
+        Int_t dest =  1<<10 | r<<7 | m;
+        if(fGtbl.GetGainTableROC(det) && fGtbl.GetGainTableROC(det)->GetGainTableMCM(r, m))
+           ltuParam.GetCorrectionFactors(det, r, m, 9, cor0, cor1, fGtbl.GetGainTableROC(det)->GetGainTableMCM(r, m)->GetMCMGain());
+        else
+           ltuParam.GetCorrectionFactors(det, r, m, 9, cor0, cor1);
+        AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, dest, addrLUTcor0, cor0);
+        AliTRDtrapConfig::Instance()->AddValues(det, fgkScsnCmdWrite, dest, addrLUTcor1, cor1);
+    }
+  }
 }