Macro to calculate the needed HV values as a function of the required MIP signal.
[u/mrichter/AliRoot.git] / VZERO / AliVZEROTriggerSimulator.cxx
index 0885d1c..78c377c 100644 (file)
  * about the suitability of this software for any purpose. It is          *\r
  * provided "as is" without express or implied warranty.                  *\r
  **************************************************************************/\r
-\r
-\r
+// \r
+// Class AliVZEROTriggerSimulator\r
+// ------------------------------\r
 //  Simulate the VZERO Trigger response\r
 // Use FEE parameters stored in Database\r
 // Can work on real data or in simulation\r
-#include "TTree.h"\r
-#include "TClonesArray.h"\r
+//\r
+\r
+#include <TTree.h>\r
+#include <TClonesArray.h>\r
+#include <TParameter.h>\r
 \r
 #include "AliLog.h"\r
 #include "AliCDBManager.h"\r
 #include "AliVZEROLogicalSignal.h"\r
 #include "AliVZEROTriggerSimulator.h"\r
 #include "AliVZEROdigit.h"\r
+#include "AliVZEROCalibData.h"\r
+#include "AliVZEROConst.h"\r
+#include "AliCTPTimeParams.h"\r
 \r
 ClassImp(AliVZEROTriggerSimulator)\r
 \r
-//________________________________________________________________\r
-AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(const AliVZEROTriggerSimulator& triggersim) : \r
-TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
-\r
-{\r
-       // copy constructor\r
-       \r
-       AliInfo("Not implemented");\r
-}\r
-\r
 //_____________________________________________________________________________\r
 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(TTree * digitsTree, TClonesArray* digits) : \r
-TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(digitsTree),fDigits(digits),fTriggerWord(0)\r
+TObject(),fTriggerData(NULL),fDigitsTree(digitsTree),fDigits(digits),fTriggerWord(0)\r
 {\r
+       // constructor\r
+       fTriggerData = LoadTriggerData();\r
+       LoadClockOffset();\r
+       \r
        for(int i=0;i<64;i++) {\r
                fBBFlags[i] = fBGFlags[i] = kFALSE;\r
-               fCharges[i] = 0;\r
+               fCharges[i] = 0.;\r
        }\r
        GenerateBBWindows();\r
        GenerateBGWindows();\r
@@ -61,8 +62,12 @@ TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(digitsTree),fDigits(digits
 }\r
 //_____________________________________________________________________________\r
 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator() : \r
-TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
+TObject(),fTriggerData(NULL),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
 {\r
+       // Default constructor\r
+       fTriggerData = LoadTriggerData();\r
+       LoadClockOffset();\r
+\r
        for(int i=0;i<64;i++) {\r
                fBBFlags[i] = fBGFlags[i] = kFALSE;\r
                fCharges[i] = 0;\r
@@ -79,30 +84,35 @@ TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(NULL),fDigits(NULL),fTrigg
 \r
 //_____________________________________________________________________________\r
 AliVZEROTriggerSimulator::~AliVZEROTriggerSimulator(){\r
-       if(fBBGate) delete [] fBBGate;\r
-       if(fBGGate) delete [] fBGGate;\r
-       if(fBBLatch) delete [] fBBLatch;\r
-       if(fBBReset) delete [] fBBReset;\r
-       if(fBGLatch) delete [] fBGLatch;\r
-       if(fBGReset) delete [] fBGReset;\r
+// Destructor\r
+  for (Int_t i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
+    delete fBBGate[i];\r
+    delete fBGGate[i];\r
+    delete fBBLatch[i];\r
+    delete fBBReset[i];\r
+    delete fBGLatch[i];\r
+    delete fBGReset[i];\r
+  }\r
 }\r
 \r
 //_____________________________________________________________________________\r
 void AliVZEROTriggerSimulator::GenerateBBWindows() \r
 {\r
+       // Generates the BB observation window\r
        for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
-               AliVZEROLogicalSignal Clk1BB(fTriggerData->GetClk1Win1(i),fTriggerData->GetDelayClk1Win1(i));\r
-               AliVZEROLogicalSignal Clk2BB(fTriggerData->GetClk2Win1(i),fTriggerData->GetDelayClk2Win1(i));\r
-               fBBGate[i] = new AliVZEROLogicalSignal(Clk1BB & Clk2BB);\r
+               AliVZEROLogicalSignal clk1BB(fTriggerData->GetClk1Win1(i),fTriggerData->GetDelayClk1Win1(i));\r
+               AliVZEROLogicalSignal clk2BB(fTriggerData->GetClk2Win1(i),fTriggerData->GetDelayClk2Win1(i));\r
+               fBBGate[i] = new AliVZEROLogicalSignal(clk1BB & clk2BB);\r
        }\r
 }\r
 //_____________________________________________________________________________\r
 void AliVZEROTriggerSimulator::GenerateBGWindows() \r
 {\r
+       // Generates the BG observation window\r
        for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
-               AliVZEROLogicalSignal Clk1BG(fTriggerData->GetClk1Win2(i),fTriggerData->GetDelayClk1Win2(i));\r
-               AliVZEROLogicalSignal Clk2BG(fTriggerData->GetClk2Win2(i),fTriggerData->GetDelayClk2Win2(i));\r
-               fBGGate[i] = new AliVZEROLogicalSignal(Clk1BG & Clk2BG);\r
+               AliVZEROLogicalSignal clk1BG(fTriggerData->GetClk1Win2(i),fTriggerData->GetDelayClk1Win2(i));\r
+               AliVZEROLogicalSignal clk2BG(fTriggerData->GetClk2Win2(i),fTriggerData->GetDelayClk2Win2(i));\r
+               fBGGate[i] = new AliVZEROLogicalSignal(clk1BG & clk2BG);\r
        }\r
 }\r
 \r
@@ -110,7 +120,7 @@ void AliVZEROTriggerSimulator::GenerateBGWindows()
 AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const \r
 {\r
        // Gets Trigger object for VZERO set\r
-       AliInfo("Loading Trigger parameters");\r
+        AliDebug(1,"Loading Trigger parameters");\r
        AliCDBManager *man = AliCDBManager::Instance();\r
        \r
        \r
@@ -118,11 +128,8 @@ AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const
        \r
        entry = man->Get("VZERO/Trigger/Data");\r
        if(!entry){\r
-               AliWarning("Load of calibration data from default storage failed!");\r
-               AliWarning("Calibration data will be loaded from local storage ($ALICE_ROOT)");\r
-               \r
-               man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");\r
-               entry = man->Get("VZERO/Trigger/Data",0);\r
+               AliFatal("Load of trigger calibration data from default storage failed!");\r
+               return NULL;\r
        }\r
        \r
        // Retrieval of data in directory VZERO/Calib/Trigger:\r
@@ -135,6 +142,46 @@ AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const
        return triggerData;\r
 }\r
 \r
+\r
+//_____________________________________________________________________________\r
+void AliVZEROTriggerSimulator::LoadClockOffset()\r
+{\r
+  // This method is used in order to\r
+  // retrieve the TDC clock offset including\r
+  // roll-over, trig count and CTP L0->L1 delay\r
+\r
+  AliCDBEntry *entry0 = AliCDBManager::Instance()->Get("VZERO/Calib/Data");\r
+  if (!entry0) {\r
+    AliFatal("V0 Calib object is not found in OCDB !");\r
+    return;\r
+  }\r
+  AliVZEROCalibData *calibdata = (AliVZEROCalibData*) entry0->GetObject();\r
+\r
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");\r
+  if (!entry) {\r
+    AliFatal("CTP timing parameters are not found in OCDB !");\r
+    return;\r
+  }\r
+  AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();\r
+  Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;\r
+\r
+  AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");\r
+  if (!entry1) {\r
+    AliFatal("CTP time-alignment is not found in OCDB !");\r
+    return;\r
+  }\r
+  AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();\r
+  l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);\r
+\r
+  for(Int_t board = 0; board < AliVZEROTriggerData::kNCIUBoards; ++board) {\r
+    fClockOffset[board] = (((Float_t)calibdata->GetRollOver(board)-\r
+                           (Float_t)calibdata->GetTriggerCountOffset(board))*25.0-\r
+                          l1Delay+\r
+                          kV0Offset);\r
+    AliDebug(1,Form("Board %d Offset %f",board,fClockOffset[board]));\r
+  }\r
+}\r
+\r
 //_____________________________________________________________________________\r
 void AliVZEROTriggerSimulator::Run() {\r
        //AliInfo("Generating VZERO Triggers");\r
@@ -151,25 +198,34 @@ void AliVZEROTriggerSimulator::Run() {
                        \r
                        Int_t integrator = digit->Integrator();\r
                        Int_t pmNumber   = digit->PMNumber();\r
-                       Int_t board   = pmNumber / 8;\r
-                       Int_t channel = pmNumber % 8;\r
+                       Int_t board   = AliVZEROCalibData::GetBoardNumber(pmNumber);\r
+                       Int_t channel = AliVZEROCalibData::GetFEEChannelNumber(pmNumber);\r
+                       if (board < 0 || channel < 0) continue;\r
                        \r
                        if(fTriggerData->GetEnableCharge(board,channel)) {\r
-                               fCharges[pmNumber] = digit->ADC();\r
-                               if(fTriggerData->GetPedestalSubtraction(board)) \r
-                                       if(fCharges[pmNumber]>=fTriggerData->GetPedestalCut(integrator,board,channel)){ \r
-                                               fCharges[pmNumber] -= fTriggerData->GetPedestal(integrator,board,channel);\r
+                               fCharges[pmNumber] = digit->ChargeADC(AliVZEROdigit::kNClocks/2);\r
+                               if(fTriggerData->GetPedestalSubtraction(board)) {\r
+                                       if(fCharges[pmNumber]>=(Float_t) fTriggerData->GetPedestalCut(integrator,board,channel)){ \r
+                                               fCharges[pmNumber] -= (Float_t) fTriggerData->GetPedestal(integrator,board,channel);\r
                                        } else {\r
-                                               fCharges[pmNumber] = 0;\r
+                                               fCharges[pmNumber] = 0.;\r
                                        }\r
+                               }\r
                        } else {\r
-                               fCharges[pmNumber] = 0;\r
+                               fCharges[pmNumber] = 0.;\r
                        }\r
                        \r
-                       Float_t time = (Float_t)digit->Time() / 10.; // digit->Time() in bin of 100 picoseconds. Divide by 10 to have it in nanoseconds\r
-                       time += fTriggerData->GetDelayHit(board,channel);\r
-                       \r
-                       AliInfo(Form(" PM nb : %d ; ADC= %d ; TDC= %f  Enable Time %d charge %d",pmNumber,digit->ADC(),time,fTriggerData->GetEnableTiming(board,channel),fTriggerData->GetEnableCharge(board,channel)));\r
+                       Float_t time = digit->Time();\r
+                       time -= fClockOffset[board];\r
+\r
+                       AliDebug(10,Form(" Digit: %f %d %d %d %d %d %d %d %d",digit->Time(),\r
+                                        digit->ChargeADC(8),digit->ChargeADC(9),digit->ChargeADC(10),\r
+                                        digit->ChargeADC(11),digit->ChargeADC(12),digit->ChargeADC(13),\r
+                                        digit->ChargeADC(14),digit->ChargeADC(15)));\r
+                       AliDebug(10,Form(" PM nb : %d ; TDC= %f(%f)  Enable Time %d charge %d inCoin %d charge %f",\r
+                                        pmNumber,time,digit->Time(),\r
+                                        fTriggerData->GetEnableTiming(board,channel),fTriggerData->GetEnableCharge(board,channel),\r
+                                        fBBGate[board]->IsInCoincidence(time),fCharges[pmNumber]));\r
                        fBBFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBBGate[board]->IsInCoincidence(time);\r
                        fBGFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBGGate[board]->IsInCoincidence(time);\r
                        \r
@@ -180,21 +236,35 @@ void AliVZEROTriggerSimulator::Run() {
        Int_t nBBflagsV0C = 0;\r
        Int_t nBGflagsV0A = 0;\r
        Int_t nBGflagsV0C = 0;\r
-       Int_t chargeV0A   = 0;\r
-       Int_t chargeV0C   = 0;\r
+       Float_t chargeV0A   = 0.;\r
+       Float_t chargeV0C   = 0.;\r
+       Int_t aBBflagsV0A = 0;\r
+       Int_t aBBflagsV0C = 0;\r
+       Int_t aBGflagsV0A = 0;\r
+       Int_t aBGflagsV0C = 0;\r
 \r
        for(int i=0;i<64;i++) {\r
                if(i<32) {\r
                        nBBflagsV0C += fBBFlags[i]; \r
                        nBGflagsV0C += fBGFlags[i];\r
                        chargeV0C += fCharges[i];\r
+                       if (fBBFlags[i]) aBBflagsV0C |= (1 << i);\r
+                       if (fBGFlags[i]) aBGflagsV0C |= (1 << i);\r
                } else {\r
                        nBBflagsV0A += fBBFlags[i]; \r
                        nBGflagsV0A += fBGFlags[i];\r
                        chargeV0A += fCharges[i];\r
+                       if (fBBFlags[i]) aBBflagsV0A |= (1 << (i-32));\r
+                       if (fBGFlags[i]) aBGflagsV0A |= (1 << (i-32));\r
                }\r
                //AliInfo(Form("Ch %d BB=%d BG=%d",i,fBBFlags[i],fBGFlags[i] )); \r
        }\r
+\r
+       // Store the BB and BG flags in the digits tree (user info)\r
+       fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0A",aBBflagsV0A));\r
+       fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0C",aBBflagsV0C));\r
+       fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0A",aBGflagsV0A));\r
+       fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0C",aBGflagsV0C));\r
        \r
        // BBA\r
        if(nBBflagsV0A>=fTriggerData->GetBBAThreshold())  SetBBA();\r
@@ -248,9 +318,11 @@ void AliVZEROTriggerSimulator::Run() {
        // (BGA and BBC) or (BGC and BBA) (Beam Gas from one of the two sides)\r
        if(GetBGAandBBC() || GetBGCandBBA()) SetBeamGas();\r
 \r
-       AliInfo(Form("BB Flags : V0A = %d  V0C = %d ",nBBflagsV0A, nBBflagsV0C )); \r
-       AliInfo(Form("BG Flags : V0A = %d  V0C = %d ",nBGflagsV0A, nBGflagsV0C )); \r
-       AliInfo(Form("Charges  : V0A = %d  V0C = %d ",chargeV0A, chargeV0C )); \r
+//     AliInfo(Form("BB Flags : V0A = %d  V0C = %d ",nBBflagsV0A, nBBflagsV0C )); \r
+//     AliInfo(Form("BG Flags : V0A = %d  V0C = %d ",nBGflagsV0A, nBGflagsV0C )); \r
+//     AliInfo(Form("Charges  : V0A = %d  V0C = %d ",chargeV0A, chargeV0C )); \r
        \r
 }\r
 \r
+\r
+\r