memory leak fixed
[u/mrichter/AliRoot.git] / T0 / AliT0Parameters.cxx
index 895bbc7..4d82cd6 100644 (file)
 // Eventually, this class will use the Conditions DB to get the
 // various parameters, which code can then request from here.
 //                                                       
+#include "AliT0.h"
 #include "AliLog.h"              
 #include "AliT0Parameters.h"     
 #include "AliT0CalibData.h"   
+#include "AliT0CalibWalk.h"   
+#include "AliT0CalibTimeEq.h"   
+#include "AliT0CalibLatency.h"   
+#include "AliT0LookUpKey.h"
 #include "AliT0LookUpValue.h"
 #include <AliCDBManager.h>        
 #include <AliCDBEntry.h>          
 #include <AliCDBStorage.h>  
 #include <TMath.h>
 #include <TSystem.h>
-#include <Riostream.h>
+//#include <Riostream.h>
+#include <TGeoManager.h>
+#include <TGeoPhysicalNode.h>
+#include <TGeoMatrix.h>
+#include <AliGeomManager.h>
 
-AliT0CalibData* AliT0Parameters::fgCalibData = 0;
+AliT0CalibTimeEq* AliT0Parameters::fgCalibData = 0;
 AliT0CalibData* AliT0Parameters::fgLookUp = 0;
-AliT0CalibData* AliT0Parameters::fgSlewCorr =0;
+AliT0CalibWalk* AliT0Parameters::fgSlewCorr =0;
+AliT0CalibLatency *AliT0Parameters::fgLatency=0;
 //====================================================================
 ClassImp(AliT0Parameters)
 #if 0
@@ -47,25 +57,46 @@ ClassImp(AliT0Parameters)
 //____________________________________________________________________
 AliT0Parameters* AliT0Parameters::fgInstance = 0;
 //____________________________________________________________________
-AliT0Parameters* 
-AliT0Parameters::Instance() 
+AliT0Parameters* AliT0Parameters::Instance() 
 {
   // Get static instance 
-  if (!fgInstance) fgInstance = new AliT0Parameters;
+  if (!fgInstance) {
+    fgInstance = new AliT0Parameters;
+  }
   return fgInstance;
 }
 
 //____________________________________________________________________
 AliT0Parameters::AliT0Parameters()
-  :fIsInit(kFALSE),fPh2Mip(0),fmV2Mip(0),fChannelWidth(0),fmV2Channel(0),fQTmin(0),fQTmax(0),fSlewingLED(),fSlewingRec(),fPMTeff(),fTimeDelayLED(0),fTimeDelayCFD(0),fTimeDelayTVD(0),fCalibentry(), fLookUpentry(),fSlewCorr()
+  :fIsInit(kFALSE),
+   fPh2Mip(0),fmV2Mip(0),
+   fChannelWidth(0),fmV2Channel(0),
+   fQTmin(0),fQTmax(0),
+   fAmpLEDRec(0), 
+   fPMTeff(),
+   fWalk(0),
+   fQTC(0),
+   fAmpLED(0),
+   fTimeDelayCFD(0), 
+ //  fTimeV0(0), 
+   fTimeDelayTVD(0),
+   fMeanT0(512),
+   fMeanVertex(0),
+   fLatencyHPTDC(0),
+   fLatencyL1(0),
+   fLatencyL1A(0),
+   fLatencyL1C(0),
+   fLookUp(0),
+   fNumberOfTRMs(2),
+   fCalibentry(), 
+   fLookUpentry(),
+   fSlewCorr(),
+   fLatency()
+  
 {
   // Default constructor 
-
   for (Int_t ipmt=0; ipmt<24; ipmt++)
     {
-      SetSlewingLED(ipmt);
-      SetSlewingRec(ipmt);
-      SetWalk(ipmt);
       SetPh2Mip();      
       SetmV2Mip();      
       SetChannelWidth();
@@ -73,11 +104,10 @@ AliT0Parameters::AliT0Parameters()
       SetQTmin();
       SetQTmax();
       SetPMTeff(ipmt);
-
-   }
+    }
   SetTimeDelayTVD();
   SetZposition();
-  
+    
 }
 
 //__________________________________________________________________
@@ -86,55 +116,108 @@ AliT0Parameters::Init()
 {
   // Initialize the parameters manager.  We need to get stuff from the
   // CDB here. 
-  //   if (fIsInit) return;
-  
-  // AliCDBManager* cdb      = AliCDBManager::Instance();
-  // fCalibentry  = cdb->Get("T0/Calib/Gain_TimeDelay_Slewing_Walk");
-
-  AliCDBStorage *stor =AliCDBManager::Instance()->GetStorage("local://$ALICE_ROOT");
-  //time equalizing
-  AliCDBEntry* fCalibentry  = stor->Get("T0/Calib/TimeDelay",0);
- if (fCalibentry){
-   fgCalibData  = (AliT0CalibData*)fCalibentry->GetObject();
-  }
- else 
-   { AliError(" ALARM !!!! No time delays in CDB "); }
+   if (fIsInit) return;
+
+   AliCDBManager *stor =AliCDBManager::Instance();
+   //time equalizing
+   fCalibentry  = stor->Get("T0/Calib/TimeDelay");
+   if (fCalibentry)
+     fgCalibData  = (AliT0CalibTimeEq*)fCalibentry->GetObject();
+   else {
+         AliFatal(" ALARM !!!! No time delays in CDB "); 
+     fIsInit = kFALSE;
+     return;
+   }
  //slewing correction
-  AliCDBEntry* fSlewCorr  = stor->Get("T0/Calib/Slewing_Walk",0);
- if (fSlewCorr){
-   fgSlewCorr  = (AliT0CalibData*)fSlewCorr->GetObject();
+  fSlewCorr  = stor->Get("T0/Calib/Slewing_Walk");
+  if (fSlewCorr){
+    fgSlewCorr  = (AliT0CalibWalk*)fSlewCorr->GetObject();
+  }
+  else {
+      AliFatal(" ALARM !!!! No slewing correction in CDB "); 
+    fIsInit = kFALSE;
+    return;
   }
- // fLookUpentry  = cdb->Get("T0/Calib/LookUp_Table");
-  fLookUpentry  = stor->Get("T0/Calib/LookUp_Table",0);
+  //lookup table
+  fLookUpentry  = stor->Get("T0/Calib/LookUp_Table");
   if (fLookUpentry){
     fgLookUp  = (AliT0CalibData*)fLookUpentry->GetObject();
-    //   fgLookUp->Dump();
   }
   else {
-    fgLookUp->ReadAsciiLookup("$ALICE_ROOT/T0/lookUpTable.txt");
+     AliFatal(" ALARM !!!! No Lookup table  in CDB "); 
+    fIsInit = kFALSE;
+    return;
   }
-
-  fIsInit = kTRUE;
+  //latency
+  
+ fLatency  = stor->Get("T0/Calib/Latency");
+  if (fLatency){
+    fgLatency  = (AliT0CalibLatency*)fLatency->GetObject();
+  }
+  else {
+     AliWarning(" !!! no latency  in CDB "); 
+    return;
+  }
+  
+fIsInit = kTRUE;
 }
 
 
 //__________________________________________________________________
-Float_t
-AliT0Parameters::GetTimeDelayLED(Int_t ipmt) 
+
+void AliT0Parameters::InitIfOnline()
 {
-  // return time delay for LED channel
-  // 
-  if (!fCalibentry) {
-    fTimeDelayLED = 0;
-    return  fTimeDelayLED;
-  } 
-  return fgCalibData ->GetTimeDelayLED(ipmt);
+// should be used in online
+// for switching to this one should write
+  // AliT0RawReader myrawreader(rawReader);
+//     myrawreader.SetOnlineMode(kTRUE);
+  
+  if (fIsInit) return;
+  //standart configuration (used for simulation)
+  //Int_t trm=0; Int_t tdc=0; Int_t chain=0; Int_t channel=0;
+  // configuration for test Jun07.
+  fgLookUp = new AliT0CalibData("T0");
+  
+  fNumberOfTRMs = 1;
+  fgLookUp-> SetNumberOfTRMs(fNumberOfTRMs);
+  Int_t trm=7; Int_t tdc=0; Int_t chain=0; Int_t channel=0;
+  for (Int_t ik=0; ik<105; ik++)
+    {
+      AliT0LookUpKey * lookkey= new AliT0LookUpKey();
+      AliT0LookUpValue * lookvalue= new AliT0LookUpValue();
+      lookvalue->SetTRM(trm);
+      lookvalue->SetTDC(tdc);
+      lookvalue->SetChain(chain);
+      lookvalue->SetChannel(channel);
+      lookkey->SetKey(ik);
+      fgLookUp->GetMapLookup()->Add((TObject*)lookvalue,(TObject*)lookkey);    
+      if (channel<6) channel +=2;
+      else {channel = 0; tdc++;}
+      if(ik==56) { tdc=0; channel=0; chain = 1;}
+    }
+  //2 recent open channels
+  trm=7; tdc=14; channel=2; chain=0; 
+  for (Int_t ik=105; ik<107; ik++)
+    {
+      AliT0LookUpKey * lookkey= new AliT0LookUpKey();
+      AliT0LookUpValue * lookvalue= new AliT0LookUpValue();
+      
+      lookvalue->SetTRM(trm);
+      lookvalue->SetTDC(tdc);
+      lookvalue->SetChain(chain);
+      lookvalue->SetChannel(channel);
+      lookkey->SetKey(ik);
+      fgLookUp->GetMapLookup()->Add((TObject*)lookvalue,(TObject*)lookkey);    
+      if (channel<6) channel +=2;
+      else {channel = 0; tdc++;}
+    }
+  
+  fIsInit=kTRUE;
 }
 //__________________________________________________________________
 Float_t
 AliT0Parameters::GetTimeDelayCFD(Int_t ipmt) 
-{
+  {
   // return time delay for CFD channel
    // 
   if (!fCalibentry) 
@@ -143,167 +226,113 @@ AliT0Parameters::GetTimeDelayCFD(Int_t ipmt)
       return fTimeDelayCFD;
     }
    
-  return fgCalibData->GetTimeDelayCFD(ipmt);
+  return fgCalibData->GetTimeEq(ipmt);
 }
-
 //__________________________________________________________________
+Float_t
+AliT0Parameters::GetCFD(Int_t ipmt) 
+  {
+  // return  CFD channel
+   
+    return fgCalibData->GetCFDvalue(ipmt,0);
+}
 
-void 
-AliT0Parameters::SetSlewingLED(Int_t ipmt)
-{
-  //  Set Slweing Correction for LED channel 
-     Float_t mv[23] = {25, 30,40,60, 80,100,150,200,250,300,
-                      400,500,600,800,1000,1500, 2000, 3000, 4000, 5500,
-                      6000, 7000,8000};
-      Float_t y[23] = {5044, 4719, 3835, 3224, 2847, 2691,2327, 2067, 1937, 1781,
-                      1560, 1456 ,1339, 1163.5, 1027, 819, 650, 520, 370.5, 234,
-                      156, 78, 0};
-      
-      TGraph* gr = new TGraph(23,mv,y);
-      fSlewingLED.AddAtAndExpand(gr,ipmt);
-  }
 //__________________________________________________________________
-
-Float_t AliT0Parameters::GetSlewingLED(Int_t ipmt, Float_t mv) const
-{
-  if (!fCalibentry) {
-    return ((TGraph*)fSlewingLED.At(ipmt))->Eval(mv); 
-  } 
-  return fgCalibData->GetSlewingLED(ipmt, mv) ;
+Float_t
+AliT0Parameters::GetLatencyHPTDC() 
+  {
+  // return LatencyHPTDC for CFD channel
+  if (!fLatency) 
+    {
+      fLatencyHPTDC=9000.;
+      return fLatencyHPTDC;
+    }
+   
+  return fgLatency->GetLatencyHPTDC();
 }
-
-
 //__________________________________________________________________
-
-TGraph *AliT0Parameters::GetSlew(Int_t ipmt) const
-{
-  if (!fCalibentry) {
-    return  (TGraph*)fSlewingLED.At(ipmt); 
-  } 
-  return fgCalibData -> GetSlew(ipmt) ;
+Float_t
+AliT0Parameters::GetLatencyL1() 
+  {
+  // return time delay for CFD channel
+   
+  return fgLatency->GetLatencyL1();
 }
 
 //__________________________________________________________________
+Float_t
+AliT0Parameters::GetLatencyL1A() 
+  {
+  // return time delay for CFD channel
+   
+  return fgLatency->GetLatencyL1A();
+}
 
-
-void 
-AliT0Parameters::SetSlewingRec(Int_t ipmt)
-{
-  //  Set Slweing Correction for LED channel 
-      Float_t mv[23] = {25, 30, 40,60, 80,100,150,200,250,300,
-                       400,500,600,800,1000,1500, 2000, 3000, 4000, 5500,
-                       6000, 7000,8000};
-      Float_t y[23] = {5044, 4719, 3835, 3224, 2847, 2691,2327, 2067, 1937, 1781, 
-                      1560, 1456 ,1339, 1163.5, 1027, 819, 650, 520, 370.5, 234, 
-                      156, 78, 0};
-      Float_t y1[23], mv1[23];
-      for (Int_t i=0; i<23; i++){
-       y1[i] = y[22-i]; mv1[i] = mv[22-i];}
-
-      TGraph* gr = new TGraph(23,y1,mv1);
-      fSlewingRec.AddAtAndExpand(gr,ipmt);
-
+//__________________________________________________________________
+Float_t
+AliT0Parameters::GetLatencyL1C() 
+  {
+  // return time delay for CFD channel
+   
+  return fgLatency->GetLatencyL1C();
 }
 //__________________________________________________________________
 
-Float_t AliT0Parameters::GetSlewingRec(Int_t ipmt, Float_t mv) const
-{
-  if (!fCalibentry) {
-    return ((TGraph*)fSlewingRec.At(ipmt))->Eval(mv); 
-  } 
-  return fgCalibData -> GetSlewingRec(ipmt, mv) ;
+Float_t
+AliT0Parameters:: GetMeanVertex()
+{ 
+  if (!fCalibentry) 
+    {
+      fMeanVertex=0;
+      return fMeanVertex;
+    }
+   
+  return fgCalibData->GetMeanVertex();
 }
-
 //__________________________________________________________________
 
-TGraph *AliT0Parameters::GetSlewRec(Int_t ipmt) const
+TGraph *AliT0Parameters::GetAmpLEDRec(Int_t ipmt) const
 {
-  if (!fCalibentry) {
-    return  (TGraph*)fSlewingRec.At(ipmt); 
+   if (!fSlewCorr) {
+     AliError("No slewing correction is available!");
+     return  (TGraph*)fAmpLEDRec.At(ipmt); 
   } 
-  return fgCalibData -> GetSlewRec(ipmt) ;
+  return fgSlewCorr -> GetAmpLEDRec(ipmt) ;
 }
 
-
-//________________________________________________________________
-void AliT0Parameters::SetWalk(Int_t ipmt)
-{
-
- Int_t mv, ps; 
-  Int_t x[70000], y[70000], index[70000];
-  Float_t time[10000],amplitude[10000];
-  string buffer;
-  Bool_t down=false;
-  
-  const char * filename = gSystem->ExpandPathName("$ALICE_ROOT/T0/data/CFD-Amp.txt");
-  ifstream inFile(filename);
-  //  if(!inFile) AliError(Form("Cannot open file %s !",filename));
-  
-  Int_t i=0, i1=0, i2=0;
-  while(getline(inFile,buffer)){
-    inFile >> ps >> mv;
-
-    x[i]=ps; y[i]=mv;
-    i++;
-  }
-  inFile.close();
-  TMath::Sort(i, y, index,down);
-  Int_t amp=0, iin=0, isum=0, sum=0;
-  Int_t ind=0;
-  for (Int_t ii=0; ii<i; ii++)
-    {
-      ind=index[ii];
-      if(y[ind] == amp)
-       {
-         sum +=x[ind];
-         iin++;
-       }
-      else
-       {
-         if(iin>0)
-           time[isum] = Float_t (sum/(iin));
-         else
-           time[isum] =Float_t (x[ind]);
-         amplitude[isum] = Float_t (amp);
-         amp=y[ind];
-         //      cout<<ii<<" "<<ind<<" "<<y[ind]<<" "<<x[ind]<<" iin "<<iin<<" mean "<<time[isum]<<" amp "<< amplitude[isum]<<" "<<isum<<endl;
-         iin=0;
-         isum++;
-         sum=0;
-       }
-
-
-    }
-
-  inFile.close();
-
-   TGraph* gr = new TGraph(isum, amplitude, time);
-  fWalk.AddAtAndExpand(gr,ipmt);
-  
-  
-}
 //__________________________________________________________________
 
 TGraph *AliT0Parameters::GetWalk(Int_t ipmt) const
 {
-  if (!fCalibentry) {
+  if (!fSlewCorr) {
+    AliError("No walk correction is available!");
     return  (TGraph*)fWalk.At(ipmt); 
   } 
-  return fgCalibData -> GetWalk(ipmt) ;
+  return fgSlewCorr -> GetWalk(ipmt) ;
 }
 
 //__________________________________________________________________
 
-Float_t AliT0Parameters::GetWalkVal(Int_t ipmt, Float_t mv) const
+TGraph *AliT0Parameters::GetQTC(Int_t ipmt) const
 {
-  if (!fCalibentry) {
-    return ((TGraph*)fWalk.At(ipmt))->Eval(mv); 
+  if (!fSlewCorr) {
+    AliError("No walk correction is available!");
+    //    return  (TGraph*)fQTC.At(ipmt); 
+   return  0; 
   } 
-  return fgCalibData -> GetWalkVal(ipmt, mv) ;
+  return fgSlewCorr -> GetQTC(ipmt) ;
 }
 
 //__________________________________________________________________
+TGraph *AliT0Parameters::GetAmpLED(Int_t ipmt) const
+{
+  if (!fSlewCorr) {
+    AliError("No walk correction is available!");
+    //    return  (TGraph*)fQTC.At(ipmt); 
+   return  0; 
+  } 
+  return fgSlewCorr -> GetAmpLED(ipmt) ;
+}
 
 //__________________________________________________________________
 void 
@@ -331,18 +360,91 @@ Int_t
 AliT0Parameters::GetChannel(Int_t trm,  Int_t tdc, Int_t chain, Int_t channel)
 {
 
-  
-  AliT0LookUpKey * lookkey;  //= new AliT0LookUpKey();
-  AliT0LookUpValue * lookvalue= new AliT0LookUpValue(trm,tdc,chain,channel);
-    
-   lookkey = (AliT0LookUpKey*) fgLookUp->GetMapLookup()->GetValue((TObject*)lookvalue);
-  if (!lookkey ) {
-    cout<<" no such address "<<endl; return -1;
+  if (fgLookUp) {
+    AliT0LookUpValue key(trm,tdc,chain,channel);
+      AliT0LookUpKey *val = (AliT0LookUpKey*) fgLookUp->GetMapLookup()->GetValue((TObject*)&key);
+      // AliT0LookUpKey *val = (AliT0LookUpKey*) fLookUp.GetValue((TObject*)&key);
+    if (val )
+      return val->GetKey();
+    else {
+      AliWarning(Form("No such address (%d %d %d %d)!",trm,tdc,chain,channel));
+      return -1;
+    }
   }
-  
+  else {
+    AliError("No look up table has been loader!");
+    return -1;
+  }
+
+}
+//__________________________________________________________________
+TMap *AliT0Parameters::GetMapLookup()
+{
+  if (!fgLookUp){
+    cout<<" No look up table in OCDB";
+    return 0;
+  }
+  return   fgLookUp->GetMapLookup();
+}
+//__________________________________________________________________
 
-  //cout<<"AliT0Parameters:: key "<<lookkey->GetKey()<<endl;
-  return lookkey->GetKey();
+Int_t
+AliT0Parameters::GetNumberOfTRMs() 
+{
+  // return number of trms
+  // 
+  if (!fgLookUp) {
+    //  fNumberOfTRMs = 2;
+    return  fNumberOfTRMs;
+  } 
+  return  fgLookUp ->GetNumberOfTRMs();
+}
+/*
+//________________________________________________________________________________
+Double_t AliT0Parameters::GetZPosition(const char* symname){
+// Get the global z coordinate of the given T0 alignable volume
+//
+  Double_t *tr = AliGeomManager::GetMatrix(symname)->GetTranslation();
+
+  return tr[2];
+}
+*/
+//________________________________________________________________________________
+Double_t AliT0Parameters::GetZPosition(const char* symname){
+// Get the global z coordinate of the given T0 alignable volume
+//
+  Double_t *tr;
+  TGeoPNEntry *pne = gGeoManager->GetAlignableEntry(symname);
+  if (!pne) return 0;
   
 
+  TGeoPhysicalNode *pnode = pne->GetPhysicalNode();
+  if(pnode){
+          TGeoHMatrix* hm = pnode->GetMatrix();
+           tr = hm->GetTranslation();
+  }else{
+          const char* path = pne->GetTitle();
+          if(!gGeoManager->cd(path)){
+                  AliErrorClass(Form("Volume path %s not valid!",path));
+                  return 0;
+          }
+         tr = gGeoManager->GetCurrentMatrix()->GetTranslation();
+  }
+  return tr[2];
+
 }
+//________________________________________________________________________________
+
+Double_t AliT0Parameters::GetZPositionShift(const char* symname)
+{
+// Get the global z coordinate of the given T0 alignable volume
+//
+  Double_t *tr = AliGeomManager::GetMatrix(symname)->GetTranslation();
+
+  TGeoHMatrix origmat;
+  AliGeomManager::GetOrigGlobalMatrix(symname,origmat);
+  Double_t *otr = origmat.GetTranslation();
+
+  return (tr[2]-otr[2]);
+}
+