]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTrigger.cxx
797244995b135b488a64ef4dd2fb8fa16c9e6be4
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALTrigger.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 /* $Id$ */
16
17 //_________________________________________________________________________  
18 //
19 //  Class for trigger analysis.
20 //  Digits are grouped in TRU's  (Trigger Units). A TRU consists of 384 
21 //  cells ordered fNTRUPhi x fNTRUEta. The algorithm searches all possible 2x2 
22 //  and nxn (n is a multiple of 2) cell combinations per each TRU,  adding the 
23 //  digits amplitude and finding the maximum. If found, look if it is isolated.
24 //  Maxima are transformed in ADC time samples. Each time bin is compared to the trigger 
25 //  threshold until it is larger and then, triggers are set. Thresholds need to be fixed.  
26 //  Thresholds need to be fixed. Last 2 modules are half size in Phi, I considered 
27 //  that the number of TRU is maintained for the last modules but decision not taken. 
28 //  If different, then this must be changed. 
29 //  Usage:
30 //
31 //  //Inside the event loop
32 //  AliEMCALTrigger *tr = new AliEMCALTrigger();//Init Trigger
33 //  tr->SetL0Threshold(100); //Arbitrary threshold values
34 //  tr->SetL1JetLowPtThreshold(1000);
35 //  tr->SetL1JetMediumPtThreshold(10000);
36 //  tr->SetL1JetHighPtThreshold(20000);
37 //  ...
38 //  tr->Trigger(); //Execute Trigger
39 //  tr->Print(""); //Print results
40 //
41 //*-- Author: Gustavo Conesa & Yves Schutz (IFIC, CERN) 
42 //////////////////////////////////////////////////////////////////////////////
43
44
45 // --- ROOT system ---
46
47 // --- ALIROOT system ---
48 #include "AliRun.h" 
49 #include "AliRunLoader.h" 
50 #include "AliTriggerInput.h"
51 #include "AliEMCAL.h" 
52 #include "AliEMCALLoader.h" 
53 #include "AliEMCALDigit.h"
54 #include "AliEMCALTrigger.h" 
55 #include "AliEMCALGeometry.h"
56 #include "AliEMCALRawUtils.h"
57
58 ClassImp(AliEMCALTrigger)
59
60 //______________________________________________________________________
61 AliEMCALTrigger::AliEMCALTrigger()
62   : AliTriggerDetector(),
63     f2x2MaxAmp(-1), f2x2CellPhi(-1),  f2x2CellEta(-1),
64     f2x2SM(0),
65     fnxnMaxAmp(-1), fnxnCellPhi(-1),  fnxnCellEta(-1),
66     fnxnSM(0),
67     fADCValuesHighnxn(0),fADCValuesLownxn(0),
68     fADCValuesHigh2x2(0),fADCValuesLow2x2(0),
69     fDigitsList(0),
70     fL0Threshold(100),fL1JetLowPtThreshold(200),
71     fL1JetMediumPtThreshold(500), fL1JetHighPtThreshold(1000),
72     fNTRU(3), fNTRUEta(3), fNTRUPhi(1), 
73     fNCellsPhi(24), fNCellsEta(16), 
74     fPatchSize(1),  fIsolPatchSize(1), 
75     f2x2AmpOutOfPatch(-1), fnxnAmpOutOfPatch(-1), 
76     f2x2AmpOutOfPatchThres(100000),  fnxnAmpOutOfPatchThres(100000), 
77     fIs2x2Isol(kFALSE), fIsnxnIsol(kFALSE),  
78     fSimulation(kTRUE), fIsolateInSuperModule(kTRUE)
79 {
80   //ctor 
81   fADCValuesHighnxn = 0x0; //new Int_t[fTimeBins];
82   fADCValuesLownxn  = 0x0; //new Int_t[fTimeBins];
83   fADCValuesHigh2x2 = 0x0; //new Int_t[fTimeBins];
84   fADCValuesLow2x2  = 0x0; //new Int_t[fTimeBins];
85
86   SetName("EMCAL");
87   CreateInputs();
88    
89    //Print("") ; 
90 }
91
92
93
94 //____________________________________________________________________________
95 AliEMCALTrigger::AliEMCALTrigger(const AliEMCALTrigger & trig) 
96   : AliTriggerDetector(trig),
97     f2x2MaxAmp(trig.f2x2MaxAmp), 
98     f2x2CellPhi(trig.f2x2CellPhi),  
99     f2x2CellEta(trig.f2x2CellEta),
100     f2x2SM(trig.f2x2SM),
101     fnxnMaxAmp(trig.fnxnMaxAmp), 
102     fnxnCellPhi(trig.fnxnCellPhi),  
103     fnxnCellEta(trig.fnxnCellEta),
104     fnxnSM(trig.fnxnSM),
105     fADCValuesHighnxn(trig.fADCValuesHighnxn),
106     fADCValuesLownxn(trig.fADCValuesLownxn),
107     fADCValuesHigh2x2(trig.fADCValuesHigh2x2),
108     fADCValuesLow2x2(trig.fADCValuesLow2x2),
109     fDigitsList(trig.fDigitsList),
110     fL0Threshold(trig.fL0Threshold),
111     fL1JetLowPtThreshold(trig.fL1JetLowPtThreshold),
112     fL1JetMediumPtThreshold(trig.fL1JetMediumPtThreshold), 
113     fL1JetHighPtThreshold(trig.fL1JetHighPtThreshold),
114     fNTRU(trig.fNTRU), fNTRUEta(trig.fNTRUEta), fNTRUPhi(trig.fNTRUPhi), 
115     fNCellsPhi(trig.fNCellsPhi), fNCellsEta(trig.fNCellsEta), 
116     fPatchSize(trig.fPatchSize),
117     fIsolPatchSize(trig.fIsolPatchSize), 
118     f2x2AmpOutOfPatch(trig.f2x2AmpOutOfPatch), 
119     fnxnAmpOutOfPatch(trig.fnxnAmpOutOfPatch), 
120     f2x2AmpOutOfPatchThres(trig.f2x2AmpOutOfPatchThres),  
121     fnxnAmpOutOfPatchThres(trig.fnxnAmpOutOfPatchThres), 
122     fIs2x2Isol(trig.fIs2x2Isol),
123     fIsnxnIsol(trig.fIsnxnIsol),  
124     fSimulation(trig.fSimulation),
125     fIsolateInSuperModule(trig.fIsolateInSuperModule)
126 {
127   // cpy ctor
128 }
129
130 //----------------------------------------------------------------------
131 void AliEMCALTrigger::CreateInputs()
132 {
133    // inputs 
134    
135    // Do not create inputs again!!
136    if( fInputs.GetEntriesFast() > 0 ) return;
137    
138    fInputs.AddLast( new AliTriggerInput( "EMCAL_L0",       "EMCAL L0", 0x02 ) );
139    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetHPt_L1","EMCAL Jet High Pt L1",   0x04 ) );
140    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetMPt_L1","EMCAL Jet Medium Pt L1", 0x08 ) );
141    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetLPt_L1","EMCAL Jet Low Pt L1",    0x016 ) );
142  
143 }
144
145 //____________________________________________________________________________
146 Bool_t AliEMCALTrigger::IsPatchIsolated(Int_t iPatchType, const TClonesArray * ampmatrixes, const Int_t iSM, const Int_t mtru, const Float_t maxamp, const Int_t maxphi, const Int_t maxeta) {
147
148   //Calculate if the maximum patch found is isolated, find amplitude around maximum (2x2 or nxn) patch, 
149   //inside isolation patch . iPatchType = 0 means calculation for 2x2 patch, 
150   //iPatchType = 1 means calculation for nxn patch.
151   //In the next table there is an example of the different options of patch size and isolation patch size:
152   //                                                                                 Patch Size (fPatchSize)
153   //                                                             0                          1                                  2
154   //          fIsolPatchSize                 2x2 (not overlap)   4x4 (overlapped)        6x6(overlapped) ...
155   //                   1                                       4x4                      8x8                              10x10
156   //                   2                                       6x6                     12x12                           14x14    
157   //                   3                                       8x8                     16x16                           18x18
158                           
159   Bool_t b = kFALSE;
160   Float_t amp = 0;
161  
162   //Get matrix of TRU or Module with maximum amplitude patch.
163   Int_t itru = mtru+iSM*fNTRU ; //number of tru, min 0 max 8*5.
164   TMatrixD * ampmatrix   = 0x0;
165   Int_t colborder = 0;
166   Int_t rowborder = 0;
167   
168   if(fIsolateInSuperModule){
169     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(iSM)) ;
170     rowborder = fNCellsPhi*fNTRUPhi;
171     colborder = fNCellsEta*fNTRUEta;
172     AliDebug(2,"Isolate trigger in Module");
173   }
174   else{
175     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(itru)) ;
176     rowborder = fNCellsPhi;
177     colborder = fNCellsEta;
178     AliDebug(2,"Isolate trigger in TRU");
179   }
180   
181   //Define patch cells
182   Int_t isolcells   = fIsolPatchSize*(1+iPatchType);
183   Int_t ipatchcells = 2*(1+fPatchSize*iPatchType);
184   Int_t minrow      = maxphi - isolcells;
185   Int_t mincol      = maxeta - isolcells;
186   Int_t maxrow      = maxphi + isolcells + ipatchcells;
187   Int_t maxcol      = maxeta + isolcells + ipatchcells;
188
189   AliDebug(2,Form("Number of added Isol Cells %d, Patch Size %d",isolcells, ipatchcells));
190   AliDebug(2,Form("Patch: minrow %d, maxrow %d, mincol %d, maxcol %d",minrow,maxrow,mincol,maxcol));
191   
192   if(minrow < 0 || mincol < 0 || maxrow > rowborder || maxcol > colborder){
193     AliDebug(1,Form("Out of Module/TRU range, cannot isolate patch"));
194     return kFALSE;
195   }
196   
197   //Add amplitudes in all isolation patch
198   for(Int_t irow = minrow ; irow <  maxrow; irow ++)
199     for(Int_t icol = mincol ; icol < maxcol ; icol ++)
200       amp += (*ampmatrix)(irow,icol);
201   
202   AliDebug(2,Form("Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
203
204   if(amp < maxamp){
205     AliError(Form("Bad sum: Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
206     return kFALSE;
207   }
208   else
209     amp-=maxamp; //Calculate energy in isolation patch that do not comes from maximum patch.
210   
211   AliDebug(2, Form("Maximum amplitude %f, Out of patch %f",maxamp, amp));
212
213   //Fill isolation amplitude data member and say if patch is isolated.
214   if(iPatchType == 0){ //2x2 case
215     f2x2AmpOutOfPatch = amp;   
216     if(amp < f2x2AmpOutOfPatchThres)
217       b=kTRUE;
218   }
219   else  if(iPatchType == 1){ //nxn case
220     fnxnAmpOutOfPatch = amp;   
221     if(amp < fnxnAmpOutOfPatchThres)
222       b=kTRUE;
223   }
224
225   return b;
226
227 }
228
229 //____________________________________________________________________________
230 void AliEMCALTrigger::MakeSlidingCell(const TClonesArray * amptrus, const TClonesArray * timeRtrus, const Int_t isupermod,TMatrixD *ampmax2, TMatrixD *ampmaxn){
231   
232   //Sums energy of all possible 2x2 (L0) and nxn (L1) cells per each TRU. 
233   //Fast signal in the experiment is given by 2x2 cells, 
234   //for this reason we loop inside the TRU cells by 2. 
235
236   //Declare and initialize variables
237   Int_t nCellsPhi  = fNCellsPhi;//geom->GetNPhi()*2/geom->GetNTRUPhi() ;
238   if(isupermod > 9)
239     nCellsPhi =  nCellsPhi / 2 ; //Half size SM. Not Final.
240   // 12(tow)*2(cell)/1 TRU, cells in Phi in one TRU
241   Int_t nCellsEta  = fNCellsEta ;//geom->GetNEta()*2/geom->GetNTRUEta() ;
242   // 24(mod)*2(tower)/3 TRU, cells in Eta in one TRU
243   //Int_t nTRU          = geom->GeNTRU();//3 TRU per super module
244
245   Float_t amp2 = 0 ;
246   Float_t ampn = 0 ; 
247   for(Int_t i = 0; i < 4; i++){
248     for(Int_t j = 0; j < fNTRU; j++){
249       (*ampmax2)(i,j) = -1;
250       (*ampmaxn)(i,j) = -1;
251     }
252   }
253
254   //Create matrix that will contain 2x2 amplitude sums
255   //used to calculate the nxn sums
256   TMatrixD  * tru2x2 = new TMatrixD(nCellsPhi/2,nCellsEta/2) ;
257   for(Int_t i = 0; i < nCellsPhi/2; i++)
258     for(Int_t j = 0; j < nCellsEta/2; j++)
259       (*tru2x2)(i,j) = -1;
260   
261   //Loop over all TRUS in a supermodule
262   for(Int_t itru = 0 + isupermod * fNTRU ; itru < (isupermod+1)*fNTRU ; itru++) {
263     TMatrixD * amptru   = dynamic_cast<TMatrixD *>(amptrus->At(itru)) ;
264     TMatrixD * timeRtru = dynamic_cast<TMatrixD *>(timeRtrus->At(itru)) ;
265     Int_t mtru = itru-isupermod*fNTRU ; //Number of TRU in Supermodule
266    
267     //Sliding 2x2, add 2x2 amplitudes (NOT OVERLAP)
268     for(Int_t irow = 0 ; irow <  nCellsPhi; irow += 2){ 
269       for(Int_t icol = 0 ; icol < nCellsEta ; icol += 2){
270         amp2 = (*amptru)(irow,icol)+(*amptru)(irow+1,icol)+
271           (*amptru)(irow,icol+1)+(*amptru)(irow+1,icol+1);
272
273         //Fill matrix with added 2x2 cells for use in nxn sums
274         (*tru2x2)(irow/2,icol/2) = amp2 ;
275         //Select 2x2 maximum sums to select L0 
276         if(amp2 > (*ampmax2)(0,mtru)){
277           (*ampmax2)(0,mtru) = amp2 ; 
278           (*ampmax2)(1,mtru) = irow;
279           (*ampmax2)(2,mtru) = icol;
280         }
281       }
282     }
283     
284     //Find most recent time in the selected 2x2 cell
285     (*ampmax2)(3,mtru) = 1 ;
286     Int_t row2 =  static_cast <Int_t> ((*ampmax2)(1,mtru));
287     Int_t col2 =  static_cast <Int_t> ((*ampmax2)(2,mtru));
288     for(Int_t i = 0; i<2; i++){
289       for(Int_t j = 0; j<2; j++){
290         if((*amptru)(row2+i,col2+j) > 0 &&  (*timeRtru)(row2+i,col2+j)> 0){       
291           if((*timeRtru)(row2+i,col2+j) <  (*ampmax2)(3,mtru)  )
292             (*ampmax2)(3,mtru) =  (*timeRtru)(row2+i,col2+j);
293         }
294       }
295     }
296
297     //Sliding nxn, add nxn amplitudes  (OVERLAP)
298     if(fPatchSize > 0){
299       for(Int_t irow = 0 ; irow <  nCellsPhi/2; irow++){ 
300         for(Int_t icol = 0 ; icol < nCellsEta/2 ; icol++){
301           ampn = 0;
302           if( (irow+fPatchSize) < nCellsPhi/2 && (icol+fPatchSize) < nCellsEta/2){//Avoid exit the TRU
303             for(Int_t i = 0 ; i <= fPatchSize ; i++)
304               for(Int_t j = 0 ; j <= fPatchSize ; j++)
305                 ampn += (*tru2x2)(irow+i,icol+j);
306             //Select nxn maximum sums to select L1 
307             if(ampn > (*ampmaxn)(0,mtru)){
308               (*ampmaxn)(0,mtru) = ampn ; 
309               (*ampmaxn)(1,mtru) = irow*2;
310               (*ampmaxn)(2,mtru) = icol*2;
311             }
312           }
313         }
314       }
315       
316       //Find most recent time in selected nxn cell
317       (*ampmaxn)(3,mtru) = 1 ;
318       Int_t rown =  static_cast <Int_t> ((*ampmaxn)(1,mtru));
319       Int_t coln =  static_cast <Int_t> ((*ampmaxn)(2,mtru));
320       for(Int_t i = 0; i<4*fPatchSize; i++){
321         for(Int_t j = 0; j<4*fPatchSize; j++){
322           if( (rown+i) < nCellsPhi && (coln+j) < nCellsEta/2){//Avoid exit the TRU
323             if((*amptru)(rown+i,coln+j) > 0 &&  (*timeRtru)(rown+i,coln+j)> 0){
324               if((*timeRtru)(rown+i,coln+j) <  (*ampmaxn)(3,mtru)  )
325                 (*ampmaxn)(3,mtru) =  (*timeRtru)(rown+i,coln+j);
326             }
327           }
328         }
329       }
330     }
331     else {  
332         (*ampmaxn)(0,mtru) =  (*ampmax2)(0,mtru); 
333         (*ampmaxn)(1,mtru) =  (*ampmax2)(1,mtru);
334         (*ampmaxn)(2,mtru) =  (*ampmax2)(2,mtru);
335         (*ampmaxn)(3,mtru) =  (*ampmax2)(3,mtru);
336       }
337   }
338 }
339
340 //____________________________________________________________________________
341 void AliEMCALTrigger::Print(const Option_t * opt) const 
342 {
343   
344   //Prints main parameters
345   
346   if(! opt)
347     return;
348   AliTriggerInput* in = 0x0 ;
349
350   printf( "             Maximum Amplitude after Sliding Cell, \n") ; 
351   printf( "               -2x2 cells sum (not overlapped): %10.2f, in Super Module %d\n",
352           f2x2MaxAmp,f2x2SM) ; 
353   printf( "               -2x2 from row %d to row %d and from column %d to column %d\n", f2x2CellPhi, f2x2CellPhi+2, f2x2CellEta, f2x2CellEta+2) ; 
354   printf( "               -2x2 Isolation Patch %d x %d, Amplitude out of 2x2 patch is %f, threshold %f, Isolated? %d \n", 
355           2*fIsolPatchSize+2, 2*fIsolPatchSize+2,  f2x2AmpOutOfPatch,  f2x2AmpOutOfPatchThres,static_cast<Int_t> (fIs2x2Isol)) ; 
356   if(fPatchSize > 0){
357     printf( "             Patch Size, n x n: %d x %d cells\n",2*(fPatchSize+1), 2*(fPatchSize+1));
358     printf( "               -nxn cells sum (overlapped)    : %10.2f, in Super Module %d\n",
359             fnxnMaxAmp,fnxnSM) ; 
360     printf( "               -nxn from row %d to row %d and from column %d to column %d\n", fnxnCellPhi, fnxnCellPhi+4*fPatchSize, fnxnCellEta, fnxnCellEta+4*fPatchSize) ; 
361     printf( "               -nxn Isolation Patch %d x %d, Amplitude out of nxn patch is %f, threshold %f, Isolated? %d \n", 
362             4*fIsolPatchSize+2*(fPatchSize+1),4*fIsolPatchSize+2*(fPatchSize+1) ,  fnxnAmpOutOfPatch,  fnxnAmpOutOfPatchThres,static_cast<Int_t> (fIsnxnIsol) ) ; 
363   }
364   
365   printf( "             Isolate in SuperModule? %d\n",  
366           fIsolateInSuperModule) ;  
367
368   printf( "             Threshold for LO %10.2f\n", 
369           fL0Threshold) ;  
370   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_L0" );
371   if(in->GetValue())
372     printf( "             *** EMCAL LO is set ***\n") ; 
373   
374   printf( "             Jet Low Pt Threshold for L1 %10.2f\n", 
375           fL1JetLowPtThreshold) ;
376   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_JetLPt_L1" );
377   if(in->GetValue())
378     printf( "             *** EMCAL Jet Low Pt for L1 is set ***\n") ;
379
380   printf( "             Jet Medium Pt Threshold for L1 %10.2f\n", 
381           fL1JetMediumPtThreshold) ;  
382   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_JetMPt_L1" );
383   if(in->GetValue())
384     printf( "             *** EMCAL Jet Medium Pt for L1 is set ***\n") ;
385
386   printf( "             Jet High Pt Threshold for L1 %10.2f\n", 
387           fL1JetHighPtThreshold) ;  
388   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_JetHPt_L1" );
389   if(in->GetValue())
390     printf( "             *** EMCAL Jet High Pt for L1 is set ***\n") ;
391
392 }
393
394 //____________________________________________________________________________
395 void AliEMCALTrigger::SetTriggers(const TClonesArray * ampmatrix,const Int_t iSM, 
396                                   const TMatrixD *ampmax2, 
397                                   const TMatrixD *ampmaxn, const AliEMCALGeometry *geom)  
398 {
399
400   //Checks the 2x2 and nxn maximum amplitude per each TRU and 
401   //compares with the different L0 and L1 triggers thresholds
402   Float_t max2[] = {-1,-1,-1,-1} ;
403   Float_t maxn[] = {-1,-1,-1,-1} ;
404   Int_t   mtru2  = -1 ;
405   Int_t   mtrun  = -1 ;
406
407   //Find maximum summed amplitude of all the TRU 
408   //in a Super Module
409     for(Int_t i = 0 ; i < fNTRU ; i++){
410       if(max2[0] < (*ampmax2)(0,i) ){
411         max2[0] =  (*ampmax2)(0,i) ; // 2x2 summed max amplitude
412         max2[1] =  (*ampmax2)(1,i) ; // corresponding phi position in TRU
413         max2[2] =  (*ampmax2)(2,i) ; // corresponding eta position in TRU
414         max2[3] =  (*ampmax2)(3,i) ; // corresponding most recent time
415         mtru2   = i ;
416       }
417       if(maxn[0] < (*ampmaxn)(0,i) ){
418         maxn[0] =  (*ampmaxn)(0,i) ; // nxn summed max amplitude
419         maxn[1] =  (*ampmaxn)(1,i) ; // corresponding phi position in TRU
420         maxn[2] =  (*ampmaxn)(2,i) ; // corresponding eta position in TRU
421         maxn[3] =  (*ampmaxn)(3,i) ; // corresponding most recent time
422         mtrun   = i ;
423       }
424     }
425
426   //--------Set max amplitude if larger than in other Super Modules------------
427   Float_t maxtimeR2 = -1 ;
428   Float_t maxtimeRn = -1 ;
429   static AliEMCALRawUtils rawUtil;
430   Int_t nTimeBins = rawUtil.GetRawFormatTimeBins() ;
431
432   //Set max of 2x2 amplitudes and select L0 trigger
433   if(max2[0] > f2x2MaxAmp ){
434     f2x2MaxAmp  = max2[0] ;
435     f2x2SM      = iSM ;
436     maxtimeR2   = max2[3] ;
437     geom->GetCellPhiEtaIndexInSModuleFromTRUIndex(mtru2, 
438                                                   static_cast<Int_t>(max2[1]),
439                                                   static_cast<Int_t>(max2[2]),
440                                                   f2x2CellPhi,f2x2CellEta) ;
441     
442     //Isolated patch?
443     if(fIsolateInSuperModule)
444       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iSM, mtru2,  f2x2MaxAmp, f2x2CellPhi,f2x2CellEta) ;
445     else
446       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iSM, mtru2,  f2x2MaxAmp,  static_cast<Int_t>(max2[1]), static_cast<Int_t>(max2[2])) ;
447
448     //Transform digit amplitude in Raw Samples
449     fADCValuesLow2x2  = new Int_t[nTimeBins];
450     fADCValuesHigh2x2 = new Int_t[nTimeBins];
451     rawUtil.RawSampledResponse(maxtimeR2, f2x2MaxAmp, fADCValuesHigh2x2, fADCValuesLow2x2) ; 
452     
453     //Set Trigger Inputs, compare ADC time bins until threshold is attained
454     //Set L0
455     for(Int_t i = 0 ; i < nTimeBins ; i++){
456       if(fADCValuesHigh2x2[i] >= fL0Threshold          || fADCValuesLow2x2[i] >= fL0Threshold){
457         SetInput("EMCAL_L0") ;
458         break;
459       }
460     }
461   }
462   
463   //------------Set max of nxn amplitudes and select L1 trigger---------
464   if(maxn[0] > fnxnMaxAmp ){
465     fnxnMaxAmp  = maxn[0] ;
466     fnxnSM      = iSM ;
467     maxtimeRn   = maxn[3] ;
468     geom->GetCellPhiEtaIndexInSModuleFromTRUIndex(mtrun, 
469                                                   static_cast<Int_t>(maxn[1]),
470                                                   static_cast<Int_t>(maxn[2]),
471                                                   fnxnCellPhi,fnxnCellEta) ; 
472     
473     //Isolated patch?
474     if(fIsolateInSuperModule)
475       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iSM, mtrun,  fnxnMaxAmp, fnxnCellPhi, fnxnCellEta) ;
476     else
477       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iSM, mtrun,  fnxnMaxAmp,  static_cast<Int_t>(maxn[1]), static_cast<Int_t>(maxn[2])) ;
478     
479     //Transform digit amplitude in Raw Samples
480     fADCValuesHighnxn = new Int_t[nTimeBins];
481     fADCValuesLownxn  = new Int_t[nTimeBins];
482     rawUtil.RawSampledResponse(maxtimeRn, fnxnMaxAmp, fADCValuesHighnxn, fADCValuesLownxn) ;
483     
484     //Set Trigger Inputs, compare ADC time bins until threshold is attained
485     //SetL1 Low
486     for(Int_t i = 0 ; i < nTimeBins ; i++){
487       if(fADCValuesHighnxn[i] >= fL1JetLowPtThreshold  || fADCValuesLownxn[i] >= fL1JetLowPtThreshold){
488         SetInput("EMCAL_JetLPt_L1") ;
489         break; 
490       }
491     }
492     
493     //SetL1 Medium
494     for(Int_t i = 0 ; i < nTimeBins ; i++){
495       if(fADCValuesHighnxn[i] >= fL1JetMediumPtThreshold || fADCValuesLownxn[i] >= fL1JetMediumPtThreshold){
496         SetInput("EMCAL_JetMPt_L1") ;
497         break;
498       }
499     }
500     
501     //SetL1 High
502     for(Int_t i = 0 ; i < nTimeBins ; i++){
503       if(fADCValuesHighnxn[i] >= fL1JetHighPtThreshold || fADCValuesLownxn[i] >= fL1JetHighPtThreshold){
504         SetInput("EMCAL_JetHPt_L1") ;
505         break;
506       }
507     }
508   } 
509 }
510
511 //____________________________________________________________________________
512 void AliEMCALTrigger::Trigger() 
513 {
514   //Main Method to select triggers.
515   AliRunLoader *runLoader = AliRunLoader::GetRunLoader();
516   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>
517     (runLoader->GetDetectorLoader("EMCAL"));
518  
519   //Load EMCAL Geometry
520   AliEMCALGeometry * geom = 0;
521   if (runLoader->GetAliRun() && runLoader->GetAliRun()->GetDetector("EMCAL"))
522     geom = dynamic_cast<AliEMCAL*>(runLoader->GetAliRun()->GetDetector("EMCAL"))->GetGeometry();
523   if (geom == 0)
524     geom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaulGeometryName());
525
526   if (geom==0)
527     AliFatal("Did not get geometry from EMCALLoader");
528   
529   //Define parameters
530   Int_t nSuperModules = geom->GetNumberOfSuperModules() ; //12 SM in EMCAL
531   fNTRU       = geom->GetNTRU();    //3 TRU per super module
532   fNTRUEta    = geom->GetNTRUEta(); //3 TRU in eta per super module
533   fNTRUPhi    = geom->GetNTRUPhi(); //1 TRU  in phi per super module
534   fNCellsPhi  = geom->GetNPhi()*2/geom->GetNTRUPhi() ;
535   fNCellsEta  = geom->GetNEta()*2/geom->GetNTRUEta() ;
536
537   //Intialize data members each time the trigger is called in event loop
538   f2x2MaxAmp = -1; f2x2CellPhi = -1;  f2x2CellEta = -1;
539   fnxnMaxAmp = -1; fnxnCellPhi = -1;  fnxnCellEta = -1;
540
541   //Take the digits list if simulation
542   if(fSimulation){
543     runLoader->LoadDigits("EMCAL");
544     fDigitsList = emcalLoader->Digits() ;
545   }
546   if(!fDigitsList)
547     AliFatal("Digits not found !") ;
548   
549   //Take the digits list 
550   
551   //Fill TRU Matrix  
552   TClonesArray * amptrus   = new TClonesArray("TMatrixD",1000);
553   TClonesArray * ampsmods  = new TClonesArray("TMatrixD",1000);
554   TClonesArray * timeRtrus = new TClonesArray("TMatrixD",1000);
555   
556   geom->FillTRU(fDigitsList, amptrus, ampsmods, timeRtrus) ;
557
558   //Do Cell Sliding and select Trigger
559   //Initialize varible that will contain maximum amplitudes and 
560   //its corresponding cell position in eta and phi, and time.
561   TMatrixD  * ampmax2 = new TMatrixD(4,fNTRU) ;
562   TMatrixD  * ampmaxn = new TMatrixD(4,fNTRU) ;
563   
564   for(Int_t iSM = 0 ; iSM < nSuperModules ; iSM++) {
565     //Do 2x2 and nxn sums, select maximums. 
566     MakeSlidingCell(amptrus, timeRtrus, iSM, ampmax2, ampmaxn);
567     
568     //Set the trigger
569     if(fIsolateInSuperModule)
570       SetTriggers(ampsmods,iSM,ampmax2,ampmaxn,geom) ;
571     if(!fIsolateInSuperModule)
572       SetTriggers(amptrus,iSM,ampmax2,ampmaxn,geom) ;
573   }
574   
575   //Print();
576
577 }