New Trigger Emulation and Trigger access from data (Rachid Guernane)
[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 "AliEMCALTriggerData.h"
32 #include "AliEMCALDigit.h"
33 #include "AliCaloRawStreamV3.h"
34 //#include "AliVZERORawStream.h"
35 #include "AliEMCALTriggerSTURawStream.h"
36 #include "AliEMCALDigit.h"
37 #include "AliEMCALRawDigit.h"
38
39 #include <TVector2.h>
40 #include <TClonesArray.h>
41 //#include <Riostream.h>
42
43 namespace
44 {
45         const Int_t kNTRU = 32;
46 }
47
48 ClassImp(AliEMCALTriggerElectronics)
49
50 //__________________
51 AliEMCALTriggerElectronics::AliEMCALTriggerElectronics(AliEMCALCalibData *calibData) : TObject(),
52 fTRU(new TClonesArray("AliEMCALTriggerTRU",32)),
53 fSTU(0x0)
54 {
55         TVector2 rSize;
56         
57         rSize.Set( 24.,  4. );
58
59         // 32 TRUs
60         for (Int_t i=0;i<kNTRU;i++) new ((*fTRU)[i]) AliEMCALTriggerTRU(calibData, rSize, int(i/3) % 2);
61    
62         rSize.Set( 48., 64. );
63         
64         // 1 STU
65         fSTU = new AliEMCALTriggerSTU(calibData, rSize);
66         
67         for (Int_t i=0;i<kNTRU;i++) fSTU->BuildMap( i, 
68                                                                                           (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Map(), 
69                                                                                           (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
70                                                                                           );
71 }
72
73 //________________
74 AliEMCALTriggerElectronics::~AliEMCALTriggerElectronics()
75 {
76         //
77         fTRU->Delete();
78         delete fSTU;
79 }
80
81 //__________________
82 void AliEMCALTriggerElectronics::Digits2Trigger(const TClonesArray* digits, const TTree* treeV0, AliEMCALTriggerData* data)
83 {
84         //
85         AliEMCALGeometry* geom = 0x0;
86         
87         AliRunLoader *rl = AliRunLoader::Instance();
88         if (rl->GetAliRun() && rl->GetAliRun()->GetDetector("EMCAL"))
89                 geom = dynamic_cast<AliEMCAL*>(rl->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
90         else 
91                 geom =  AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());
92
93         if (!geom) AliError("Cannot access geometry!");
94         
95         TIter NextDigit(digits);
96         while (AliEMCALRawDigit* digit = (AliEMCALRawDigit*)NextDigit())
97         {
98                 if ( digit )
99                 {
100                         Int_t id = digit->GetId();
101                         
102 //                      digit->Print();
103                         
104                         Int_t iTRU, iADC;
105                         Bool_t isOK1 = geom->GetTRUFromAbsFastORIndex(id, iTRU, iADC);
106                         
107                         for (Int_t i = 0; i < digit->GetNSamples(); i++)
108                         {
109                                 Int_t time, amp;
110                                 Bool_t isOK2 = digit->GetTimeSample(i, time, amp);
111                                 
112                                 if (isOK1 && isOK2 && amp) (static_cast<AliEMCALTriggerTRU*>(fTRU->At(iTRU)))->SetADC(iADC, time, amp);
113                         }
114                 }
115         }
116         /*
117         for (Int_t i=0; i<kNTRU; i++) 
118         {
119                 printf("===========< TRU %2d >============\n",i);
120                 (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Scan();
121         }
122         */
123         Int_t iL0 = 0;
124
125         // At this point all FastOR are available for digitization
126         // digitization is done in the TRU and produces time samples
127         // Now run the trigger algo & consecutively write trigger outputs in TreeD dedicated branch
128
129         for (Int_t i=0; i<kNTRU; i++) 
130         {
131                 AliDebug(1,Form("===========< TRU %2d >============\n",i));
132                 
133                 AliEMCALTriggerTRU *iTRU = static_cast<AliEMCALTriggerTRU*>(fTRU->At(i));
134
135                 iL0 += iTRU->L0v1();
136
137                 Int_t vL0Peaks[96][2]; iTRU->Peaks( vL0Peaks );
138                         
139                 data->SetL0Patches( i , iTRU->Patches() );                      
140                 data->SetL0Peaks(   i , vL0Peaks );
141
142                 if ( !i ) // do it once since identical for all TRU 
143                 {
144                         data->SetL0RegionSize(    *iTRU->RegionSize()    );
145                         data->SetL0SubRegionSize( *iTRU->SubRegionSize() );
146                 data->SetL0PatchSize(     *iTRU->PatchSize()     );
147                 } 
148                 
149                 //              if ( i == 31 ) i = 35;
150                 //
151                 //              if ( ( i / 3 ) % 2 ) {
152                 //                      TRU->Print( 15 - 2 + ( i - int( i / 3 ) * 3 ) - 3 * ( (i / 3) / 2 ) , runLoader->GetEventNumber() );
153                 //                      printf("print data of TRU: from %2d to %2d\n",i,15 - 2 + ( i - int( i / 3 ) * 3 ) - 3 * ( (i / 3) / 2));
154                 //              }
155                 //              else
156                 //              {
157                 //                      TRU->Print( 31 - i % 3 - 3 * ( (i / 3) / 2 ) , runLoader->GetEventNumber() );
158                 //                      printf("print data of TRU: from %2d to %2d\n",i,31 - i % 3 - 3 * ( (i / 3) / 2 ));
159                 //              }               
160         }
161
162         // A L0 has been issued, run L1
163         if ( iL0 ) 
164         {
165                 for (Int_t i=0; i<kNTRU; i++) fSTU->FetchFOR( i, 
166                                                                                                     (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->Region(), 
167                                                                                                     (static_cast<AliEMCALTriggerTRU*>(fTRU->At(i)))->RegionSize() 
168                                                                                                     );
169
170 //              fSTU->Scan();
171                 
172                 TTree* tr = const_cast<TTree*>(treeV0);
173                 if ( tr ) fSTU->V0Multiplicity( *tr );
174
175                 TVector2 size;
176
177                 size.Set( 1. , 1. );
178                 fSTU->SetSubRegionSize( size ); data->SetL1GammaSubRegionSize( size );
179                 
180                 size.Set( 2. , 2. );
181                 fSTU->SetPatchSize( size );     data->SetL1GammaPatchSize(     size );
182
183                 fSTU->L1( kGamma );
184
185                 data->SetL1GammaPatches(       fSTU->Patches()       );
186
187                 fSTU->Reset();
188
189                 size.Set( 4. , 4. ); 
190                 fSTU->SetSubRegionSize( size ); data->SetL1JetSubRegionSize( size );
191                 
192                 size.Set( 2. , 2. ); 
193                 fSTU->SetPatchSize( size );     data->SetL1JetPatchSize(     size );
194
195                 fSTU->L1( kJet );
196
197                 data->SetL1JetPatches(         fSTU->Patches()       );
198                 data->SetL1RegionSize(        *fSTU->RegionSize()    );
199
200                 Int_t** region = fSTU->Region();
201                 data->SetL1Region(      region      );
202                 const Int_t* mv0 = fSTU->V0(); 
203                 data->SetL1V0(          mv0         );
204         }
205
206         // Now reset the electronics for a fresh start with next event
207         Reset();
208 }
209
210 //__________________
211 void AliEMCALTriggerElectronics::Reset()
212 {
213         //
214         TIter NextTRU(fTRU);
215         while ( AliEMCALTriggerTRU *TRU = (AliEMCALTriggerTRU*)NextTRU() ) TRU->Reset();
216         
217         fSTU->Reset();
218 }