add getter for the array with digits and initialization inside, just for analysis...
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTriggerSTU.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
20
21 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
22 */
23
24 #include "AliEMCALTriggerSTU.h"
25 #include "AliCDBManager.h"
26 #include "AliCDBEntry.h"
27 #include "AliEMCALTriggerSTUDCSConfig.h"
28 #include "AliVZEROCalibData.h"
29 #include "AliVZEROdigit.h"
30 #include "AliEMCALTriggerPatch.h"
31 #include "AliESDVZERO.h"
32 #include "AliLog.h"
33
34 #include <TClonesArray.h>
35 #include <TSystem.h>
36 #include <TH2F.h>
37 #include <TFile.h>
38 #include <TTree.h>
39
40 #include <fstream>
41 #include <Riostream.h>
42 #include <cstdlib>
43
44 ClassImp(AliEMCALTriggerSTU)
45
46 //_______________
47 AliEMCALTriggerSTU::AliEMCALTriggerSTU() : AliEMCALTriggerBoard()
48 ,fGammaTh(0)
49 ,fJetTh(0)
50 ,fDCSConfig(0x0)
51 {
52         // Ctor
53 }
54
55 //_______________
56 AliEMCALTriggerSTU::AliEMCALTriggerSTU(AliEMCALTriggerSTUDCSConfig *dcsConf, const TVector2& RS) : AliEMCALTriggerBoard(RS)
57 ,fGammaTh(0)
58 ,fJetTh(0)
59 ,fDCSConfig(dcsConf)
60 {
61         // Ctor
62 }
63
64 //_______________
65 AliEMCALTriggerSTU::~AliEMCALTriggerSTU()
66 {
67         // Dtor
68 }
69
70 //_______________
71 void AliEMCALTriggerSTU::Build( TString& str, Int_t iTRU, Int_t** M, const TVector2* rSize )
72 {
73         // Build
74         
75         str.ToLower();
76         
77         Int_t ix = (iTRU % 2) ? 24 : 0;
78
79         Int_t iy =  iTRU / 2;
80         
81         Int_t** v = 0x0;
82         
83         if (str.Contains("map"))
84         {
85                 v = fMap;
86         }
87         else if (str.Contains("region"))
88         {
89                 v = fRegion;
90         }
91         else
92         {
93                 AliError("Operation not allowed: STU won't be configured properly!");
94         }
95   
96   if(v){        
97     for (Int_t i=0; i<rSize->X(); i++)
98       for (Int_t j=0; j<rSize->Y(); j++) v[i + ix][j + iy * 4] = M[i][j];
99   }
100 }
101
102 //_______________
103 void AliEMCALTriggerSTU::L1(TriggerType_t type)
104 {
105         // L1
106         
107         TVector2 s1, s2, s3, s4;
108         fDCSConfig->GetSegmentation(s1, s2, s3, s4);
109         
110         switch (type)
111         {
112                 case kL1Gamma:
113                         SetSubRegionSize(s1); 
114                         SetPatchSize(s2);
115                         break;
116                 case kL1Jet:
117                         SetSubRegionSize(s3);
118                         SetPatchSize(s4);
119                         break;
120                 default:
121                         AliError("Not supported L1 trigger type");
122                         return;
123                         break;
124         }
125         
126         SlidingWindow(type, GetThreshold(type));        
127 }
128
129 //________________
130 void AliEMCALTriggerSTU::PrintADC( TriggerType_t type, TVector2& pos, TVector2& idx )
131 {
132         // Dump
133         
134         Int_t ix = (Int_t) (( pos.X() + fPatchSize->X() ) * fSubRegionSize->X());
135         
136         Int_t iy = (Int_t) (( pos.Y() + fPatchSize->Y() ) * fSubRegionSize->Y());
137         
138         TString subRegionADC[] = {"0->15", "16->31", "32->47", "48->63", "64->79", "80->95"};
139         
140         switch ( type )
141         {
142                 case kL1Gamma:
143                 {
144                         Int_t iTRU = ( (ix-1) < 24 ) ? 31 - int(pos.Y() / 4) : 15 - int(pos.Y() / 4);
145                         
146                         printf("TRU #%d row #%d col #%d fastor: ",iTRU,int(idx.Y()),int(idx.X()));
147                         
148                         for (Int_t i=(Int_t)(pos.X() * fSubRegionSize->X());i<ix;i++) 
149                         {
150                           for (Int_t j=(Int_t) (pos.Y() * fSubRegionSize->Y());j<iy;j++) 
151                                 {
152                                         Int_t jtru = ( i < 24 ) ? 31 - j / 4 : 15 - j / 4;
153                                         printf("TRU#%d_%d ",jtru,fMap[i][j]);
154                                 }
155                         }
156                         
157                         cout << endl;
158                 }       
159                 break;
160                 case kL1Jet:
161                 {
162                         //Int_t jTRU = ( (ix-1) < 24 ) ? 31 - (iy-1) / 4 : 15 - (iy-1) / 4;
163                         
164                         printf("jet found at row : %d and col : %d",int(idx.X()),int(idx.Y()));
165                         
166                         Char_t vPair[100];
167                         Int_t nSubRegion = 0;
168                         
169                         for (Int_t i=(Int_t)(pos.X() * fSubRegionSize->X());i<ix;i++) 
170                         {
171                           for (Int_t j=(Int_t)(pos.Y() * fSubRegionSize->Y());j<iy;j++) 
172                                 {
173                                         Int_t itru = ( i < 24 ) ? 31 - j / 4 : 15 - j / 4;
174                                         
175                                         Int_t idSR = fMap[i][j]/16;
176                                         
177                                         Char_t value = ((itru << 3) & 0xF8) | (idSR & 0x7);
178                                         
179                                         Bool_t isFound = kFALSE;
180                                         
181                                         for (Int_t k=0;k<nSubRegion && nSubRegion;k++)
182                                         {
183                                                 if (vPair[k] == value) isFound = kTRUE;
184                                         }
185                                         
186                                         if (!isFound) 
187                                         {       
188                                                 nSubRegion++;
189                                                 
190                                                 vPair[nSubRegion-1] = value;
191                                         }
192                                 }
193                         }
194                         
195                         cout << " fastor:"; 
196                         for (Int_t i=0;i<nSubRegion;i++)
197                         {
198                                 cout << " TRU#" << ((vPair[i] & 0xF8) >> 3) << "_" << subRegionADC[(vPair[i] & 0x7)];
199                         }
200                 
201                         cout << endl;
202                 }
203                 break;
204                 default:
205                         AliError("AliEMCALTriggerSTU::PrintADC(): Undefined trigger type, pls check!");
206         }
207 }
208
209 //___________
210 void AliEMCALTriggerSTU::PatchGenerator(const TClonesArray* lpos, Int_t val)
211 {
212         // Patch generator
213         
214         ZeroRegion();
215         
216         Int_t vTRU[32][24][4] ;//= {0};
217         
218         for (Int_t i = 0; i < 32; i++) 
219                 for (Int_t j = 0; j < 24; j++) 
220                         for (Int_t k = 0; k < 4; k++) vTRU[i][j][k]=0;
221         
222         
223         AliWarning("AliEMCALTriggerSTU::PatchGenerator(): STU region has been reset!");
224         
225         // Fill the patch FOR at 'pos' w/ value 'val'
226         
227         TIter NextPosition( lpos );
228         
229         while ( TVector2 *pos = (TVector2*)NextPosition() )
230         {
231                 pos->Print();
232                 for (Int_t i=(Int_t)(pos->X()*fSubRegionSize->X()); i<int((pos->X()+fPatchSize->X())*fSubRegionSize->X()); i++)
233                   for (Int_t j=(Int_t)(pos->Y()*fSubRegionSize->Y()); j<int((pos->Y()+fPatchSize->Y())*fSubRegionSize->Y()); j++) 
234                                 fRegion[i][j] = val;
235         }
236         
237         for (Int_t i=0; i<2; i++)
238         {
239                 for (Int_t j=0; j<16; j++)
240                 {
241                         Int_t iTRU = ( !i ) ? 31 - j : 15 - j;
242                         
243                         for (Int_t i1=24*i; i1<24*(i+1); i1++)
244                         {
245                                 for (Int_t j1=4*j; j1<4*(j+1); j1++)
246                                 {
247                                         vTRU[iTRU][i1%24][j1%4] = fRegion[i1][j1];
248                                 }
249                         }
250                 }
251         }
252         
253         gSystem->Exec(Form("mkdir -p GP"));
254
255         for (Int_t i=0; i<32; i++)
256         {
257                 std::ofstream outfile(Form("GP/data_TRU%d.txt",i),std::ios::trunc);
258         
259                 for (Int_t j=0;j<96;j++) 
260                 {
261                         Int_t ietam, iphim;
262                         
263                         if ( int(i / 16) )
264                         {
265                                 ietam =      j/4;
266                                 iphim =  3 - j%4;
267                         }
268                         else
269                         {                                       
270                                 ietam = 23 - j/4;
271                                 iphim =      j%4;
272                         }
273                         
274                         outfile << vTRU[i][ietam][iphim] << endl;
275                 }
276         
277                 outfile.close();
278         }
279 }
280
281 //___________
282 void AliEMCALTriggerSTU::ComputeThFromV0(TriggerType_t type, const Int_t M[])
283 {
284         // Compute threshold from V0
285         
286         Short_t P[3] = {0};
287         
288         switch (type)
289         {
290                 case kL1Gamma:
291                         P[0] = fDCSConfig->GetGA();
292                         P[1] = fDCSConfig->GetGB();
293                         P[2] = fDCSConfig->GetGC();                     
294                         break;
295                 case kL1Jet:
296                         P[0] = fDCSConfig->GetJA();
297                         P[1] = fDCSConfig->GetJB();
298                         P[2] = fDCSConfig->GetJC();                     
299                         break;
300                 default:
301                         AliError("AliEMCALTriggerSTU::ComputeThFromV0(): Undefined trigger type, pls check!");
302                         return;
303         }
304         
305         ULong64_t v0sum = M[0] + M[1];
306         
307         ULong64_t sqrV0 = v0sum * v0sum;
308         
309         sqrV0 *= P[0];  
310         sqrV0 >>= 32;
311         
312         v0sum *= P[1];
313         
314         v0sum >>= 16;
315         
316         SetThreshold(type, (UShort_t)(sqrV0 + v0sum + P[2]));
317 }
318
319 //___________
320 void AliEMCALTriggerSTU::SetThreshold(TriggerType_t type, Int_t v)
321 {
322         // Set threshold
323         
324         switch (type)
325         {
326                 case kL1Gamma:
327                         fGammaTh = v;
328                         break;
329                 case kL1Jet:
330                         fJetTh = v;             
331                         break;
332                 default:
333                         AliError("AliEMCALTriggerSTU::SetThreshold(): Undefined trigger type, pls check!");
334         }
335 }
336
337 //___________
338 Int_t AliEMCALTriggerSTU::GetThreshold(TriggerType_t type)
339 {       
340         // Compute threshold FIXME: need an access to the OCDB
341         // to get f(V0) parameters depending on trigger type
342         
343         
344         switch (type)
345         {
346                 case kL1Gamma:
347                         return fGammaTh;
348                         break;
349                 case kL1Jet:
350                         return fJetTh;          
351                         break;
352                 default:
353                         AliError("AliEMCALTriggerSTU::GetThreshold(): Undefined trigger type, pls check!");
354         }
355         
356         return 0;
357 }
358
359 //__________
360 void AliEMCALTriggerSTU::Reset()
361 {
362         // Reset
363         
364         fPatches->Delete();
365 }