Updated version.
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerElectronics.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /*
17  
18  
19 EMCal trigger electronics manager L0/L1
20 can handle both simulated digits and raw data
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24 #include "AliEMCALTriggerElectronics.h"
25 #include "AliEMCALTriggerTRU.h"
26 #include "AliEMCALTriggerSTU.h"
27 #include "AliEMCALGeometry.h"
28 #include "AliRunLoader.h"
29 #include "AliEMCAL.h" 
30 #include "AliRun.h" 
31 #include "AliEMCALTriggerDCSConfig.h"
32 #include "AliEMCALTriggerData.h"
33 #include "AliEMCALDigit.h"
34 #include "AliCaloRawStreamV3.h"
35 #include "AliEMCALTriggerSTURawStream.h"
36 #include "AliEMCALDigit.h"
37 #include "AliEMCALRawDigit.h"
38
39 #include <TVector2.h>
40 #include <TClonesArray.h>
41
42 namespace
43 {
44         const Int_t kNTRU = 32;
45 }
46
47 ClassImp(AliEMCALTriggerElectronics)
48
49 //__________________
50 AliEMCALTriggerElectronics::AliEMCALTriggerElectronics(const AliEMCALTriggerDCSConfig *dcsConf) : TObject(),
51 fTRU(new TClonesArray("AliEMCALTriggerTRU",32)),
52 fSTU(0x0)
53 {
54         TVector2 rSize;
55         
56         rSize.Set( 24.,  4. );
57
58         // 32 TRUs
59         for (Int_t i=0;i<kNTRU;i++) 
60         {
61                 AliEMCALTriggerTRUDCSConfig* truConf = dcsConf->GetTRUDCSConfig(i);
62                 new ((*fTRU)[i]) AliEMCALTriggerTRU(truConf, rSize, int(i/3) % 2);
63         }
64         
65         rSize.Set( 48., 64. );
66         
67         // 1 STU
68         AliEMCALTriggerSTUDCSConfig* stuConf = dcsConf->GetSTUDCSConfig();
69         fSTU = new AliEMCALTriggerSTU(stuConf, rSize);
70         
71         for (Int_t i=0;i<kNTRU;i++) fSTU->BuildMap( i, 
72                                                                                           (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Map(), 
73                                                                                           (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
74                                                                                           );
75 }
76
77 //________________
78 AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
79 {
80         //
81         fTRU->Delete();
82         delete fSTU;
83 }
84
85 //__________________
86 void AliEMCALTriggerElectronics::Digits2Trigger(const TClonesArray* digits, const Int_t V0M[], AliEMCALTriggerData* data)
87 {
88         //
89         AliEMCALGeometry* geom = 0x0;
90         
91         AliRunLoader *rl = AliRunLoader::Instance();
92         if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL"))
93                 geom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
94         else 
95                 geom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
96
97         if (!geom) AliError("Cannot access geometry!");
98         
99         TIter NextDigit(digits);
100         while (AliEMCALRawDigit* digit = (AliEMCALRawDigit*)NextDigit())
101         {
102                 if ( digit )
103                 {
104                         Int_t id = digit->GetId();
105                         
106 //                      digit->Print();
107                         
108                         Int_t iTRU, iADC;
109                         Bool_t isOK1 = geom->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
110                         
111                         for (Int_t i = 0; i < digit->GetNSamples(); i++)
112                         {
113                                 Int_t time, amp;
114                                 Bool_t isOK2 = digit->GetTimeSample(i, time, amp);
115                                 
116                                 if (isOK1 && isOK2 && amp) (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)))->SetADC(iADC, time, amp);
117                         }
118                 }
119         }
120         /*
121         for (Int_t i=0; i<kNTRU; i++) 
122         {
123                 printf("===========< TRU %2d >============\n",i);
124                 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Scan();
125         }
126         */
127         Int_t iL0 = 0;
128
129         // At this point all FastOR are available for digitization
130         // digitization is done in the TRU and produces time samples
131         // Now run the trigger algo & consecutively write trigger outputs in TreeD dedicated branch
132
133         for (Int_t i=0; i<kNTRU; i++) 
134         {
135                 AliDebug(1,Form("===========< TRU %2d >============\n",i));
136                 
137                 AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
138
139                 iL0 += iTRU->L0();
140
141 //              Int_t vL0Peaks[96][2]; iTRU->Peaks( vL0Peaks );
142                         
143                 data->SetL0Patches( i , iTRU->Patches() );                      
144 //              data->SetL0Peaks(   i , vL0Peaks );
145
146                 if ( !i ) // do it once since identical for all TRU 
147                 {
148                         data->SetL0RegionSize(    *iTRU->RegionSize()    );
149                         data->SetL0SubRegionSize( *iTRU->SubRegionSize() );
150                 data->SetL0PatchSize(     *iTRU->PatchSize()     );
151                 } 
152                 
153                 //              if ( i == 31 ) i = 35;
154                 //
155                 //              if ( ( i / 3 ) % 2 ) {
156                 //                      TRU->Print( 15 - 2 + ( i - int( i / 3 ) * 3 ) - 3 * ( (i / 3) / 2 ) , runLoader->GetEventNumber() );
157                 //                      printf("print data of TRU: from %2d to %2d\n",i,15 - 2 + ( i - int( i / 3 ) * 3 ) - 3 * ( (i / 3) / 2));
158                 //              }
159                 //              else
160                 //              {
161                 //                      TRU->Print( 31 - i % 3 - 3 * ( (i / 3) / 2 ) , runLoader->GetEventNumber() );
162                 //                      printf("print data of TRU: from %2d to %2d\n",i,31 - i % 3 - 3 * ( (i / 3) / 2 ));
163                 //              }               
164         }
165
166         // A L0 has been issued, run L1
167         if ( iL0 ) 
168         {
169                 for (Int_t i=0; i<kNTRU; i++) fSTU->FetchFOR( i, 
170                                                                                                     (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Region(), 
171                                                                                                     (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
172                                                                                                     );
173                 
174                 fSTU->SetV0Multiplicity( V0M , 2 ); // C/A
175
176                 TVector2 size;
177
178                 size.Set( 1. , 1. );
179                 fSTU->SetSubRegionSize( size ); data->SetL1GammaSubRegionSize( size );
180                 
181                 size.Set( 2. , 2. );
182                 fSTU->SetPatchSize( size );     data->SetL1GammaPatchSize(     size );
183
184                 fSTU->L1( kGamma );
185
186                 data->SetL1GammaPatches(       fSTU->Patches()       );
187
188                 fSTU->Reset();
189
190                 size.Set( 4. , 4. ); 
191                 fSTU->SetSubRegionSize( size ); data->SetL1JetSubRegionSize( size );
192                 
193                 size.Set( 2. , 2. ); 
194                 fSTU->SetPatchSize( size );     data->SetL1JetPatchSize(     size );
195
196                 fSTU->L1( kJet );
197
198                 data->SetL1JetPatches(         fSTU->Patches()       );
199                 data->SetL1RegionSize(        *fSTU->RegionSize()    );
200
201                 Int_t** region = fSTU->Region();
202                 data->SetL1Region(      region      );
203                 const Int_t* mv0 = fSTU->V0(); 
204                 data->SetL1V0(          mv0         );
205         }
206
207         if ( AliDebugLevel() ) data->Scan();
208         
209         // Now reset the electronics for a fresh start with next event
210         Reset();
211 }
212
213 //__________________
214 void AliEMCALTriggerElectronics::Reset()
215 {
216         //
217         TIter NextTRU(fTRU);
218         while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)NextTRU() ) TRU->Reset();
219         
220         fSTU->Reset();
221 }