proper linking for libHLTrec.so
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerElectronics.cxx
index cb183ab..860ca74 100644 (file)
@@ -36,9 +36,9 @@ Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
 #include "AliEMCALDigit.h"
 #include "AliEMCALTriggerRawDigit.h"
 #include "AliEMCALTriggerPatch.h"
+#include "AliEMCALTriggerSTUDCSConfig.h"
 
 #include <TVector2.h>
-#include <TClonesArray.h>
 
 namespace
 {
@@ -50,17 +50,30 @@ ClassImp(AliEMCALTriggerElectronics)
 //__________________
 AliEMCALTriggerElectronics::AliEMCALTriggerElectronics(const AliEMCALTriggerDCSConfig *dcsConf) : TObject(),
 fTRU(new TClonesArray("AliEMCALTriggerTRU",32)),
-fSTU(0x0)
+fSTU(0x0),
+fGeometry(0)
 {
+       // Ctor
+       
        TVector2 rSize;
        
        rSize.Set( 24.,  4. );
-
+       
+       AliRunLoader *rl = AliRunLoader::Instance();
+       if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")) {
+               AliEMCAL *emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
+               if (emcal) fGeometry = emcal->GetGeometry();
+       }
+       
+       if (!fGeometry) {               
+               fGeometry =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
+               AliError("Cannot access geometry, create a new default one!");
+       }
+       
        // 32 TRUs
-       for (Int_t i=0;i<kNTRU;i++) 
-       {
-               AliEMCALTriggerTRUDCSConfig* truConf = dcsConf->GetTRUDCSConfig(i);
-               new ((*fTRU)[i]) AliEMCALTriggerTRU(truConf, rSize, i % 2);
+       for (Int_t i = 0; i < kNTRU; i++) {
+               AliEMCALTriggerTRUDCSConfig *truConf = dcsConf->GetTRUDCSConfig(fGeometry->GetOnlineIndexFromTRUIndex(i));
+               if (truConf) new ((*fTRU)[i]) AliEMCALTriggerTRU(truConf, rSize, i % 2);
        }
        
        rSize.Set( 48., 64. );
@@ -70,17 +83,23 @@ fSTU(0x0)
        fSTU = new AliEMCALTriggerSTU(stuConf, rSize);
        
        TString str = "map";
-       for (Int_t i=0;i<kNTRU;i++) fSTU->Build(str,
-                                                                                       i,
-                                                                                       (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Map(),
-                                                                                       (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
-                                                                                   );
+       for (Int_t i = 0; i < kNTRU; i++) {
+               AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
+               if (!iTRU) continue;
+
+               fSTU->Build(str,
+                                       i,
+                                       iTRU->Map(),
+                                       iTRU->RegionSize() 
+                                       );
+       }
 }
 
 //________________
 AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
 {
-       //
+       // Dtor
+       
        fTRU->Delete();
        delete fSTU;
 }
@@ -88,20 +107,9 @@ AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
 //__________________
 void AliEMCALTriggerElectronics::Digits2Trigger(TClonesArray* digits, const Int_t V0M[], AliEMCALTriggerData* data)
 {
-       //
-       AliEMCALGeometry* geom = 0x0;
-       
-       AliRunLoader *rl = AliRunLoader::Instance();
-       if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL")){
-         AliEMCAL* emcal = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"));
-         if(emcal)geom = emcal->GetGeometry();
-       }
+       // Digits to trigger
        
-       if(!geom) geom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
-       
-       if(!geom) AliError("Cannot access geometry!");
-       
-       //      digits->Sort();
+       Int_t pos, px, py, id; 
        
        Int_t region[48][64], posMap[48][64];
        for (Int_t i = 0; i < 48; i++) for (Int_t j = 0; j < 64; j++) 
@@ -114,180 +122,303 @@ void AliEMCALTriggerElectronics::Digits2Trigger(TClonesArray* digits, const Int_
        {
                AliEMCALTriggerRawDigit* digit = (AliEMCALTriggerRawDigit*)digits->At(i);
                
-               Int_t id = digit->GetId();
+               id = digit->GetId();
                
                Int_t iTRU, iADC;
                
-               Bool_t isOK1 = geom->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
+               Bool_t isOK1 = fGeometry->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
                
+               if (!isOK1) continue;
+
                for (Int_t j = 0; j < digit->GetNSamples(); j++)
                {
                        Int_t time, amp;
                        Bool_t isOK2 = digit->GetTimeSample(j, time, amp);
                        
-                       if (isOK1 && isOK2 && amp) (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)))->SetADC(iADC, time, amp);
-               }
-               
-               Int_t px, py;
-               if (geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py))
-               {
-                       posMap[px][py] = i;
-                       
-                       if (fSTU->GetRawData() && digit->GetL1TimeSum() >= 0) 
-                       {
-                               region[px][py] = digit->GetL1TimeSum();
+                       if (isOK1 && isOK2 && amp) {
+                               AliDebug(999, Form("=== TRU# %2d ADC# %2d time# %2d signal %d ===", iTRU, iADC, time, amp));
+                               
+                               AliEMCALTriggerTRU * etr = (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)));
+                               if (etr) {
+                                 if (data->GetMode())
+                                   etr->SetADC(iADC, time, 4 * amp);
+                                 else
+                                   etr->SetADC(iADC, time,     amp);
+                               }
                        }
                }
+               
+               if (fGeometry->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) posMap[px][py] = i;
        }
 
        Int_t iL0 = 0;
 
-       for (Int_t i=0; i<kNTRU; i++) 
+       Int_t timeL0[kNTRU] = {0}, timeL0min = 999;
+       
+       for (Int_t i = 0; i < kNTRU; i++) 
        {
-               AliDebug(999, Form("===========< TRU %2d >============\n", i));
+               AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
+               if (!iTRU) continue;
+               
+               AliDebug(999, Form("===========< TRU %2d >============", i));
                
-               AliEMCALTriggerTRU* iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
+               if (iTRU->L0()) // L0 recomputation: *ALWAYS* done from FALTRO
+               {
+                       iL0++;
+                       
+                       timeL0[i] = iTRU->GetL0Time();
+                       
+                       if (!timeL0[i]) AliWarning(Form("TRU# %d has 0 trigger time",i));
+                       
+                       if (timeL0[i] < timeL0min) timeL0min = timeL0[i];
+                       
+                       data->SetL0Trigger(0, i, 1); // TRU# i has issued a L0
+               }
+               else
+                       data->SetL0Trigger(0, i, 0);
+       }
 
-               // L0 is always computed from F-ALTRO
-               if (iTRU->L0()) 
+       AliDebug(999, Form("=== %2d TRU (out of %2d) has issued a L0 / Min L0 time: %d", iL0, fTRU->GetEntriesFast(), timeL0min));
+       
+       AliEMCALTriggerRawDigit* dig = 0x0;
+       
+       if (iL0 && (!data->GetMode() || !fSTU->GetDCSConfig()->GetRawData())) 
+       {
+               // Update digits after L0 calculation
+               for (Int_t i = 0; i < kNTRU; i++)
                {
-                       iL0 += iTRU->L0();
+                       AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
+                       if (!iTRU) continue;
                        
-                       Int_t sizeX = (Int_t) ((iTRU->PatchSize())->X() * (iTRU->SubRegionSize())->X());
+                       Int_t reg[24][4];
+                       for (int j = 0; j < 24; j++) for (int k = 0; k < 4; k++) reg[j][k] = 0;
+                                       
+                       iTRU->GetL0Region(timeL0min, reg);
                        
-                       Int_t sizeY = (Int_t) ((iTRU->PatchSize())->Y() * (iTRU->SubRegionSize())->Y());
+                       for (int j = 0; j < iTRU->RegionSize()->X(); j++)
+                       {
+                               for (int k = 0; k < iTRU->RegionSize()->Y(); k++)
+                               {
+                                       if (reg[j][k]
+                                               && 
+                                               fGeometry->GetAbsFastORIndexFromPositionInTRU(i, j, k, id)
+                                               &&
+                                               fGeometry->GetPositionInEMCALFromAbsFastORIndex(id, px, py))
+                                       {
+                                               pos = posMap[px][py];
+                                                                       
+                                               if (pos == -1)
+                                               {
+                                                       // Add a new digit
+                                                       posMap[px][py] = digits->GetEntriesFast();
+
+                                                       new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
+                                                                               
+                                                       dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);                                                       
+                                               }
+                                               else
+                                               {
+                                                       dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
+                                               }
+                                               
+                                               // 14b to 12b STU time sums
+                                               reg[j][k] >>= 2; 
+                                               
+                                               dig->SetL1TimeSum(reg[j][k]);
+                                       }
+                               }
+                       }
+               }
+       }
+
+       if (iL0 && !data->GetMode())
+       {
+               for (Int_t i = 0; i < kNTRU; i++)
+               {
+                       AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
+                       if (!iTRU) continue;
                        
-                       // transform local to global 
-                       TIter Next(&iTRU->Patches());
-                       while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)Next())
+                       AliDebug(999, Form("=== TRU# %2d found %d patches", i, (iTRU->Patches()).GetEntriesFast()));
+                       
+                       TIter next(&iTRU->Patches());
+                       while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)next())
                        {
-                               Int_t px, py, id; p->Position(px, py);
-                               
-                               if (geom->GetAbsFastORIndexFromPositionInTRU(i, px, py, id) 
+                               p->Position(px, py);
+                       
+                               // Local 2 Global
+                               if (fGeometry->GetAbsFastORIndexFromPositionInTRU(i, px, py, id) 
                                        && 
-                                       geom->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) p->SetPosition(px, py);
+                                       fGeometry->GetPositionInEMCALFromAbsFastORIndex(id, px, py)) p->SetPosition(px, py);
                                
-                               if (!data->GetMode())
-                               {
-                                       Int_t peaks = p->Peaks();
+                               if (AliDebugLevel()) p->Print("");
+                               
+                               Int_t peaks = p->Peaks();
+                                                               
+                               Int_t sizeX = (Int_t) ((iTRU->PatchSize())->X() * (iTRU->SubRegionSize())->X());
+                               Int_t sizeY = (Int_t) ((iTRU->PatchSize())->Y() * (iTRU->SubRegionSize())->Y());
                                        
-                                       for (Int_t j = 0; j < sizeX * sizeY; j++)
+                               for (Int_t j = 0; j < sizeX * sizeY; j++)
+                               {
+                                       if (peaks & (1 << j))
                                        {
-                                               if (peaks & (1 << j))
-                                               {
-                                                       
-                                                       Int_t pos = posMap[px + j % sizeX][py + j / sizeX];
-                                                       
-//                                                     cout << "px: " << px << " py: " << py << " j: " << j << " mod: " << j%sizeX << " ratio: " << j / sizeX << " pos: " << pos << endl;
-                                                       
-                                                       AliEMCALTriggerRawDigit* dig = 0x0;
+                                               pos = posMap[px + j % sizeX][py + j / sizeX];
                                                        
-                                                       if (pos == -1)
-                                                       {
-                                                               new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
+                                               if (pos == -1) {
+                                                       // Add a new digit
+                                                       posMap[px + j % sizeX][py + j / sizeX] = digits->GetEntriesFast();
+
+                                                       new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
                                                                
-                                                               dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
-                                                       }
-                                                       else
-                                                       {
-                                                               dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
-                                                       }
-                                                       
-                                                       dig->SetL0Time(p->Time());
+                                                       dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);                                                       
+                                               } else {
+                                                       dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
                                                }
-                                       }                                                               
+                                                       
+                                               dig->SetL0Time(p->Time());
+                                       }
                                }
+                                       
+                               pos = posMap[px][py];
+                                       
+                               if (pos == -1) {
+                                       // Add a new digit
+                                       posMap[px][py] = digits->GetEntriesFast();
+
+                                       new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
+                                               
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
+                                       
+                               }
+                               else
+                               {
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(pos);
+                               }
+                                       
+                               dig->SetTriggerBit(kL0, 0);
                        }
+               }
+       }
+       
+       // Prepare STU for L1 calculation
+       for (int i = 0; i < (fSTU->RegionSize())->X(); i++) {
+               for (int j = 0; j < (fSTU->RegionSize())->Y(); j++) {
                        
-                       data->SetL0Trigger(0, i, 1);
-                                               
-               data->SetPatches(kL0, 0, iTRU->Patches());
+                       pos = posMap[i][j];
+               
+                       if (pos >= 0) {
+                               
+                               AliEMCALTriggerRawDigit *digit = (AliEMCALTriggerRawDigit*)digits->At(pos);
+               
+                               if (digit->GetL1TimeSum() > -1) region[i][j] = digit->GetL1TimeSum();
+                       }
                }
-               else
-                       data->SetL0Trigger(0, i, 0);
        }
-
-       // A L0 has been issued, run L1
-       // Depending on raw data enabled or not in STU data: L1 computation 
-       // should be done from F-ALTRO or directly on TRU time sums in STU raw data
-       if (iL0) 
+       
+       AliDebug(999,"==================== STU  ====================");
+       if (AliDebugLevel() >= 999) fSTU->Scan();
+       AliDebug(999,"==============================================");
+       
+       fSTU->SetRegion(region);
+       
+       if (data->GetMode()) 
        {
-               // Use L1 threshold from raw data when reconstructing raw data
-               if (data->GetMode())
-               {
-                       fSTU->SetThreshold(kL1Gamma, data->GetL1GammaThreshold());
-                       fSTU->SetThreshold(kL1Jet,   data->GetL1JetThreshold()  );                      
+               for (int ithr = 0; ithr < 2; ithr++) {
+                       AliDebug(999, Form(" THR %d / EGA %d / EJE %d", ithr, data->GetL1GammaThreshold(ithr), data->GetL1JetThreshold(ithr)));
+                                                          
+                       fSTU->SetThreshold(kL1GammaHigh + ithr, data->GetL1GammaThreshold(ithr));
+                       fSTU->SetThreshold(kL1JetHigh + ithr, data->GetL1JetThreshold(  ithr));
                }
-               else
-               {
-                       fSTU->ComputeThFromV0(V0M); // C/A
-                       data->SetL1GammaThreshold(fSTU->GetThreshold(kL1Gamma));
-                       data->SetL1JetThreshold(  fSTU->GetThreshold(kL1Jet)  );
+       }
+       else
+       {
+               for (int ithr = 0; ithr < 2; ithr++) {
+                       //
+                       fSTU->ComputeThFromV0(kL1GammaHigh + ithr, V0M); 
+                       data->SetL1GammaThreshold(ithr, fSTU->GetThreshold(kL1GammaHigh + ithr));
+                       
+                       fSTU->ComputeThFromV0(kL1JetHigh + ithr,   V0M);
+                       data->SetL1JetThreshold(ithr, fSTU->GetThreshold(kL1JetHigh + ithr)  );
+                       
+                       AliDebug(999, Form("STU THR %d EGA %d EJE %d", ithr, fSTU->GetThreshold(kL1GammaHigh + ithr), fSTU->GetThreshold(kL1JetHigh + ithr)));
                }
+       }
+
+       for (int ithr = 0; ithr < 2; ithr++) {
+               //
+               fSTU->Reset();
                
-               if (fSTU->GetRawData())
-               {
-                       // Compute L1 from STU raw data
-                       fSTU->SetRegion(region);
-               }
-               else
-               {
-                       // Build STU raw data from F-ALTRO
-                       TString str = "region";
-                       for (Int_t i = 0; i < kNTRU; i++) fSTU->Build(str,
-                                                                                                                 i, 
-                                                                                                                 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Region(), 
-                                                                                                                 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize());
+               fSTU->L1(kL1GammaHigh + ithr);
+               
+               TIterator* nP = 0x0;
+               
+               nP = (fSTU->Patches()).MakeIterator();
+               
+               AliDebug(999, Form("=== STU found %d gamma patches", (fSTU->Patches()).GetEntriesFast()));
+               
+               while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)nP->Next()) 
+               {                       
+                       p->Position(px, py);
+                       
+                       if (AliDebugLevel()) p->Print("");
+                       
+                       if (fGeometry->GetAbsFastORIndexFromPositionInEMCAL(px, py, id))
+                       {
+                               if (posMap[px][py] == -1)
+                               {
+                                       posMap[px][py] = digits->GetEntriesFast();
+                                       
+                                       // Add a new digit
+                                       new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
+                                       
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
+                               } 
+                               else
+                               {
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[px][py]);                                                             
+                               }
+                               
+                               if (AliDebugLevel()) dig->Print("");
+                               
+                               dig->SetTriggerBit(kL1GammaHigh + ithr, 0);
+                       }
                }
-
-               fSTU->L1(kL1Gamma);
                
-               data->SetPatches(kL1Gamma, 0, fSTU->Patches());
-
                fSTU->Reset();
-
-               fSTU->L1(kL1Jet);
                
-               data->SetPatches(kL1Jet,   0, fSTU->Patches());
+               fSTU->L1(kL1JetHigh + ithr);
                
-               Int_t** reg = fSTU->Region();
+               nP = (fSTU->Patches()).MakeIterator();
                
-               if (!fSTU->GetRawData())
-               {
-                       // Update digits w/ L1 time sum
-                       // Done in raw digit maker when raw data enabled
-                       for (Int_t i = 0; i < 48; i++)
+               AliDebug(999, Form("=== STU found %d jet patches", (fSTU->Patches()).GetEntriesFast()));
+               
+               while (AliEMCALTriggerPatch* p = (AliEMCALTriggerPatch*)nP->Next()) 
+               {                       
+                       p->Position(px, py);
+                       
+                       if (AliDebugLevel()) p->Print("");
+                       
+                       if (fGeometry->GetAbsFastORIndexFromPositionInEMCAL(px, py, id))
                        {
-                               for (Int_t j = 0; j < 64; j++)
+                               if (posMap[px][py] == -1)
                                {
-                                       if (reg[i][j])
-                                       {
-                                               Int_t id;
-                                               if (geom->GetAbsFastORIndexFromPositionInEMCAL(i, j, id))
-                                               {
-                                                       AliEMCALTriggerRawDigit* dig = 0x0;
-
-                                                       if (posMap[i][j] == -1)
-                                                       {
-                                                               // Add a new digit with L1 time sum
-                                                               new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
-                                                               
-                                                               dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
-                                                       } 
-                                                       else
-                                                       {
-                                                               dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[i][j]);                                                               
-                                                       }
-                                                       
-                                                       dig->SetL1TimeSum(reg[i][j]);
-                                               }
-                                       }
+                                       posMap[px][py] = digits->GetEntriesFast();
+                                       
+                                       // Add a new digit
+                                       new((*digits)[digits->GetEntriesFast()]) AliEMCALTriggerRawDigit(id, 0x0, 0);
+                                       
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(digits->GetEntriesFast() - 1);
+                               } 
+                               else
+                               {
+                                       dig = (AliEMCALTriggerRawDigit*)digits->At(posMap[px][py]);
                                }
+                               
+                               if (AliDebugLevel()) dig->Print("");
+                               
+                               dig->SetTriggerBit(kL1JetHigh + ithr, 0);
                        }
                }
        }
-
-       if (AliDebugLevel() >= 999) data->Scan();
        
        // Now reset the electronics for a fresh start with next event
        Reset();
@@ -296,9 +427,10 @@ void AliEMCALTriggerElectronics::Digits2Trigger(TClonesArray* digits, const Int_
 //__________________
 void AliEMCALTriggerElectronics::Reset()
 {
-       //
-       TIter NextTRU(fTRU);
-       while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)NextTRU() ) TRU->Reset();
+       // Reset
+       
+       TIter nextTRU(fTRU);
+       while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)nextTRU() ) TRU->Reset();
        
        fSTU->Reset();
 }