]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTrigger.cxx
fix compilation
[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 //  modules 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->SetL1GammaLowPtThreshold(1000);
35 //  tr->SetL1GammaMediumPtThreshold(10000);
36 //  tr->SetL1GammaHighPtThreshold(20000);
37 //  ...
38 //  tr->Trigger(); //Execute Trigger
39 //  tr->Print(""); //Print results
40 //
41 //*-- Author: Gustavo Conesa & Yves Schutz (IFIC, CERN) 
42 //////////////////////////////////////////////////////////////////////////////
43
44 #include <cassert>
45
46 // --- ROOT system ---
47 #include <TTree.h>
48 #include <TBranch.h>
49 #include <TBrowser.h>
50 #include <TH2F.h>
51
52 // --- ALIROOT system ---
53 #include "AliRun.h" 
54 #include "AliRunLoader.h" 
55 #include "AliTriggerInput.h"
56 #include "AliEMCAL.h" 
57 #include "AliEMCALLoader.h" 
58 #include "AliEMCALDigit.h"
59 #include "AliEMCALTrigger.h" 
60 #include "AliEMCALGeometry.h"
61 #include "AliEMCALRawUtils.h"
62 #include "AliLog.h"
63 #include "AliCaloConstants.h"
64 #include "AliEMCALRawResponse.h"
65
66 using namespace CALO;
67
68 ClassImp(AliEMCALTrigger)
69
70 TString AliEMCALTrigger::fgNameOfJetTriggers("EMCALJetTriggerL1");
71
72 //______________________________________________________________________
73 AliEMCALTrigger::AliEMCALTrigger()
74   : AliTriggerDetector(), fGeom(0),
75     f2x2MaxAmp(-1), f2x2ModulePhi(-1),  f2x2ModuleEta(-1),
76     f2x2SM(0),
77     fnxnMaxAmp(-1), fnxnModulePhi(-1),  fnxnModuleEta(-1),
78     fnxnSM(0),
79     fADCValuesHighnxn(0),fADCValuesLownxn(0),
80     fADCValuesHigh2x2(0),fADCValuesLow2x2(0),
81     fDigitsList(0),
82     fL0Threshold(100),fL1GammaLowPtThreshold(200),
83     fL1GammaMediumPtThreshold(500), fL1GammaHighPtThreshold(1000),
84     fPatchSize(1),  fIsolPatchSize(1), 
85     f2x2AmpOutOfPatch(-1), fnxnAmpOutOfPatch(-1), 
86     f2x2AmpOutOfPatchThres(100000),  fnxnAmpOutOfPatchThres(100000), 
87     fIs2x2Isol(kFALSE), fIsnxnIsol(kFALSE),  
88     fSimulation(kTRUE), fIsolateInSuperModule(kTRUE), fTimeKey(kFALSE),
89     fAmpTrus(0),fTimeRtrus(0),fAmpSMods(0),
90     fTriggerPosition(6), fTriggerAmplitudes(4), 
91     fNJetPatchPhi(3), fNJetPatchEta(3), fNJetThreshold(3), fL1JetThreshold(0), fJetMaxAmp(0),
92     fAmpJetMatrix(0), fJetMatrixE(0), fAmpJetMax(6,1), fVZER0Mult(0.)
93 {
94   //ctor 
95   fADCValuesHighnxn = 0x0; //new Int_t[fTimeBins];
96   fADCValuesLownxn  = 0x0; //new Int_t[fTimeBins];
97   fADCValuesHigh2x2 = 0x0; //new Int_t[fTimeBins];
98   fADCValuesLow2x2  = 0x0; //new Int_t[fTimeBins];
99
100   SetName("EMCAL");
101   // Define jet threshold - can not change from outside now
102   // fNJetThreshold  = 7; // For MB Pythia suppression
103   fNJetThreshold  = 10;   // Hijing  
104   fL1JetThreshold = new Double_t[fNJetThreshold];
105   if(fNJetThreshold == 7) {
106     fL1JetThreshold[0] =  5./0.0153;
107     fL1JetThreshold[1] =  8./0.0153;
108     fL1JetThreshold[2] = 10./0.0153;
109     fL1JetThreshold[3] = 12./0.0153;
110     fL1JetThreshold[4] = 13./0.0153;
111     fL1JetThreshold[5] = 14./0.0153;
112     fL1JetThreshold[6] = 15./0.0153;
113   } else if(fNJetThreshold == 10) {
114     Double_t thGev[10]={5.,8.,10., 12., 13.,14.,15., 17., 20., 25.};
115     for(Int_t i=0; i<fNJetThreshold; i++) fL1JetThreshold[i] =  thGev[i]/0.0153;
116   } else {
117     fL1JetThreshold[0] =  5./0.0153;
118     fL1JetThreshold[1] = 10./0.0153;
119     fL1JetThreshold[2] = 15./0.0153;
120     fL1JetThreshold[3] = 20./0.0153;
121     fL1JetThreshold[4] = 25./0.0153;
122   }
123   //
124   CreateInputs();
125
126   fInputs.SetName("TriggersInputs");   
127    //Print("") ; 
128 }
129
130 //____________________________________________________________________________
131 AliEMCALTrigger::~AliEMCALTrigger() {
132         
133   //dtor
134         
135   if(GetTimeKey()) {
136     delete [] fADCValuesHighnxn; 
137     delete [] fADCValuesLownxn;
138     delete [] fADCValuesHigh2x2;
139     delete [] fADCValuesLow2x2;
140   }
141   if(fAmpTrus)      {fAmpTrus->Delete();   delete fAmpTrus;}
142   if(fTimeRtrus)    {fTimeRtrus->Delete(); delete fTimeRtrus;}
143   if(fAmpSMods)     {fAmpSMods->Delete();  delete fAmpSMods;}
144   if(fAmpJetMatrix) delete fAmpJetMatrix;
145   if(fJetMatrixE)   delete fJetMatrixE;
146   if(fL1JetThreshold) delete [] fL1JetThreshold;
147 }
148
149 //----------------------------------------------------------------------
150 void AliEMCALTrigger::CreateInputs()
151 {
152    // inputs 
153    
154    // Do not create inputs again!!
155    if( fInputs.GetEntriesFast() > 0 ) return;
156
157    // Second parameter should be detector name = "EMCAL"
158    TString det("EMCAL"); // Apr 29, 2008
159    fInputs.AddLast( new AliTriggerInput( det+"_L0",          det, 0x02) );
160    fInputs.AddLast( new AliTriggerInput( det+"_GammaHPt_L1", det, 0x04 ) );
161    fInputs.AddLast( new AliTriggerInput( det+"_GammaMPt_L1", det, 0x08 ) );
162    fInputs.AddLast( new AliTriggerInput( det+"_GammaLPt_L1", det, 0x016 ) );
163    fInputs.AddLast( new AliTriggerInput( det+"_JetHPt_L1", det, 0x032 ) );
164    fInputs.AddLast( new AliTriggerInput( det+"_JetMPt_L1", det, 0x048 ) );
165    fInputs.AddLast( new AliTriggerInput( det+"_JetLPt_L1", det, 0x064 ) );
166
167    if(fNJetThreshold<=0) return;
168    // Jet Trigger(s)
169    UInt_t level = 0x032;
170    for(Int_t i=0; i<fNJetThreshold; i++ ) {
171      TString name(GetNameOfJetTrigger(i));
172      TString title("EMCAL Jet triger L1 :"); // unused now
173      // 0.0153 - hard coded now
174      title += Form("Th %i(%5.1f GeV) :", (Int_t)fL1JetThreshold[i], fL1JetThreshold[i] * 0.0153); 
175      title += Form("patch %ix%i~(%3.2f(#phi)x%3.2f(#eta)) ", 
176                  fNJetPatchPhi, fNJetPatchEta, 0.11*(fNJetPatchPhi), 0.11*(fNJetPatchEta)); 
177      fInputs.AddLast( new AliTriggerInput(name, det, UChar_t(level)) );
178      level *= 2;
179    }
180  
181 }
182
183 //____________________________________________________________________________
184 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) {
185
186   // Nov 8, 2007 
187   // EMCAL RTU size is 4modules(phi) x 24modules (eta)
188   // So maximum size of patch is 4modules x 4modules (EMCAL L0 trigger). 
189   // Calculate if the maximum patch found is isolated, find amplitude around maximum (2x2 or nxn) patch, 
190   // inside isolation patch . iPatchType = 0 means calculation for 2x2 patch, 
191   // iPatchType = 1 means calculation for nxn patch.
192   // In the next table there is an example of the different options of patch size and isolation patch size:
193   //                                                           Patch Size (fPatchSize)
194   //                                           0                          1              
195   //          fIsolPatchSize    0             2x2 (not overlap)   4x4 (overlapped)       
196   //                            1             4x4                      8x8               
197                           
198   Bool_t b = kFALSE;
199   if(!ampmatrixes) return kFALSE;
200   
201   // Get matrix of TRU or Module with maximum amplitude patch.
202   Int_t itru = mtru + iSM * fGeom->GetNTRU(); //number of tru, min 0 max 3*12=36.
203   TMatrixD * ampmatrix   = 0x0;
204   Int_t colborder = 0;
205   Int_t rowborder = 0;
206   static int keyPrint = 0;
207   if(keyPrint) AliDebug(2,Form(" IsPatchIsolated : iSM %i mtru %i itru %i maxphi %i maxeta %i \n", iSM, mtru, itru, maxphi, maxeta));
208   
209   if(fIsolateInSuperModule){ // ?
210     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(iSM)) ;
211     rowborder = fGeom->GetNPhi();
212     colborder = fGeom->GetNZ();
213     AliDebug(2,"Isolate trigger in Module");
214   } else{
215     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(itru)) ;
216     rowborder = fGeom->GetNModulesInTRUPhi();
217     colborder = fGeom->GetNModulesInTRUEta();
218     AliDebug(2,"Isolate trigger in TRU");
219   }
220   if(iSM>9) rowborder /= 2; // half size in phi
221   
222   if(!ampmatrixes || !ampmatrix){
223     AliError("Could not recover the matrix with the amplitudes");
224     return kFALSE;
225   }
226   
227   //Define patch modules - what is this ??
228   Int_t isolmodules   = fIsolPatchSize*(1+iPatchType);
229   Int_t ipatchmodules = 2*(1+fPatchSize*iPatchType);
230   Int_t minrow      = maxphi - isolmodules;
231   Int_t mincol      = maxeta - isolmodules;
232   Int_t maxrow      = maxphi + isolmodules + ipatchmodules;
233   Int_t maxcol      = maxeta + isolmodules + ipatchmodules;
234
235   minrow =  minrow<0?0 :minrow;
236   mincol =  mincol<0?0 :mincol;
237
238   maxrow =  maxrow>rowborder?rowborder :maxrow;
239   maxcol =  maxcol>colborder?colborder :maxcol;
240   
241   //printf("%s\n",Form("Number of added Isol Modules %d, Patch Size %d",isolmodules, ipatchmodules));
242   //printf("%s\n",Form("Patch: minrow %d, maxrow %d, mincol %d, maxcol %d",minrow,maxrow,mincol,maxcol));
243   //  AliDebug(2,Form("Number of added Isol Modules %d, Patch Size %d",isolmodules, ipatchmodules));
244   //AliDebug(2,Form("Patch: minrow %d, maxrow %d, mincol %d, maxcol %d",minrow,maxrow,mincol,maxcol));
245   
246   //Add amplitudes in all isolation patch
247   Float_t amp = 0.;
248   for(Int_t irow = minrow ; irow <  maxrow; irow ++)
249     for(Int_t icol = mincol ; icol < maxcol ; icol ++)
250       amp += (*ampmatrix)(irow,icol);
251   
252   AliDebug(2,Form("Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
253
254   if(amp < maxamp){
255     //    AliError(Form("Bad sum: Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
256     //    ampmatrix->Print();
257     return kFALSE;
258   } else {
259     amp-=maxamp; //Calculate energy in isolation patch that do not comes from maximum patch.
260   }
261   
262   AliDebug(2, Form("Maximum amplitude %f, Out of patch %f",maxamp, amp));
263
264   //Fill isolation amplitude data member and say if patch is isolated.
265   if(iPatchType == 0){ //2x2 case
266     f2x2AmpOutOfPatch = amp;   
267     if(amp < f2x2AmpOutOfPatchThres) b=kTRUE;
268   } else  if(iPatchType == 1){ //nxn case
269     fnxnAmpOutOfPatch = amp;   
270     if(amp < fnxnAmpOutOfPatchThres) b=kTRUE;
271   }
272
273   if(keyPrint) AliDebug(2,Form(" IsPatchIsolated - OUT \n"));
274
275   return b;
276
277 }
278
279 /*
280 //____________________________________________________________________________
281 void AliEMCALTrigger::MakeSlidingCell(const TClonesArray * amptrus, const TClonesArray * timeRtrus, const Int_t isupermod,TMatrixD &ampmax2, TMatrixD &ampmaxn){
282   
283   //Sums energy of all possible 2x2 (L0) and nxn (L1) modules per each TRU. 
284   //Fast signal in the experiment is given by 2x2 modules, 
285   //for this reason we loop inside the TRU modules by 2. 
286
287   //Declare and initialize variables
288   Int_t nCellsPhi  = fGeom->GetNCellsInTRUPhi();
289   if(isupermod > 9)
290     nCellsPhi =  nCellsPhi / 2 ; //Half size SM. Not Final.
291   // 12(tow)*2(cell)/1 TRU, cells in Phi in one TRU
292   Int_t nCellsEta  = fGeom->GetNCellsInTRUEta();
293   Int_t nTRU  = fGeom->GetNTRU();
294   // 24(mod)*2(tower)/3 TRU, cells in Eta in one TRU
295   //Int_t nTRU          = geom->GeNTRU();//3 TRU per super module
296
297   Float_t amp2 = 0 ;
298   Float_t ampn = 0 ; 
299   for(Int_t i = 0; i < 4; i++){
300     for(Int_t j = 0; j < nTRU; j++){
301       ampmax2(i,j) = -1;
302       ampmaxn(i,j) = -1;
303     }
304   }
305
306   //Create matrix that will contain 2x2 amplitude sums
307   //used to calculate the nxn sums
308   TMatrixD tru2x2(nCellsPhi/2,nCellsEta/2) ;
309   for(Int_t i = 0; i < nCellsPhi/2; i++)
310     for(Int_t j = 0; j < nCellsEta/2; j++)
311       tru2x2(i,j) = -1;
312   
313   //Loop over all TRUS in a supermodule
314   for(Int_t itru = 0 + isupermod * nTRU ; itru < (isupermod+1)*nTRU ; itru++) {
315     TMatrixD * amptru   = dynamic_cast<TMatrixD *>(amptrus->At(itru)) ;
316     TMatrixD * timeRtru = dynamic_cast<TMatrixD *>(timeRtrus->At(itru)) ;
317     Int_t mtru = itru-isupermod*nTRU ; //Number of TRU in Supermodule
318    
319     //Sliding 2x2, add 2x2 amplitudes (NOT OVERLAP)
320     for(Int_t irow = 0 ; irow <  nCellsPhi; irow += 2){ 
321       for(Int_t icol = 0 ; icol < nCellsEta ; icol += 2){
322         amp2 = (*amptru)(irow,icol)+(*amptru)(irow+1,icol)+
323           (*amptru)(irow,icol+1)+(*amptru)(irow+1,icol+1);
324
325         //Fill matrix with added 2x2 cells for use in nxn sums
326         tru2x2(irow/2,icol/2) = amp2 ;
327         //Select 2x2 maximum sums to select L0 
328         if(amp2 > ampmax2(0,mtru)){
329           ampmax2(0,mtru) = amp2 ; 
330           ampmax2(1,mtru) = irow;
331           ampmax2(2,mtru) = icol;
332         }
333       }
334     }
335     
336     //Find most recent time in the selected 2x2 cell
337     ampmax2(3,mtru) = 1 ;
338     Int_t row2 =  static_cast <Int_t> (ampmax2(1,mtru));
339     Int_t col2 =  static_cast <Int_t> (ampmax2(2,mtru));
340     for(Int_t i = 0; i<2; i++){
341       for(Int_t j = 0; j<2; j++){
342         if((*amptru)(row2+i,col2+j) > 0 &&  (*timeRtru)(row2+i,col2+j)> 0){       
343           if((*timeRtru)(row2+i,col2+j) <  ampmax2(3,mtru)  )
344             ampmax2(3,mtru) =  (*timeRtru)(row2+i,col2+j);
345         }
346       }
347     }
348
349     //Sliding nxn, add nxn amplitudes  (OVERLAP)
350     if(fPatchSize > 0){
351       for(Int_t irow = 0 ; irow <  nCellsPhi/2; irow++){ 
352         for(Int_t icol = 0 ; icol < nCellsEta/2 ; icol++){
353           ampn = 0;
354           if( (irow+fPatchSize) < nCellsPhi/2 && (icol+fPatchSize) < nCellsEta/2){//Avoid exit the TRU
355             for(Int_t i = 0 ; i <= fPatchSize ; i++)
356               for(Int_t j = 0 ; j <= fPatchSize ; j++)
357                 ampn += tru2x2(irow+i,icol+j);
358             //Select nxn maximum sums to select L1 
359             if(ampn > ampmaxn(0,mtru)){
360               ampmaxn(0,mtru) = ampn ; 
361               ampmaxn(1,mtru) = irow*2;
362               ampmaxn(2,mtru) = icol*2;
363             }
364           }
365         }
366       }
367       
368       //Find most recent time in selected nxn cell
369       ampmaxn(3,mtru) = 1 ;
370       Int_t rown =  static_cast <Int_t> (ampmaxn(1,mtru));
371       Int_t coln =  static_cast <Int_t> (ampmaxn(2,mtru));
372       for(Int_t i = 0; i<4*fPatchSize; i++){
373         for(Int_t j = 0; j<4*fPatchSize; j++){
374           if( (rown+i) < nCellsPhi && (coln+j) < nCellsEta){//Avoid exit the TRU
375             if((*amptru)(rown+i,coln+j) > 0 &&  (*timeRtru)(rown+i,coln+j)> 0){
376               if((*timeRtru)(rown+i,coln+j) <  ampmaxn(3,mtru)  )
377                 ampmaxn(3,mtru) =  (*timeRtru)(rown+i,coln+j);
378             }
379           }
380         }
381       }
382     }
383     else {  
384         ampmaxn(0,mtru) =  ampmax2(0,mtru); 
385         ampmaxn(1,mtru) =  ampmax2(1,mtru);
386         ampmaxn(2,mtru) =  ampmax2(2,mtru);
387         ampmaxn(3,mtru) =  ampmax2(3,mtru);
388       }
389   }
390 }
391 */
392 //____________________________________________________________________________
393 void AliEMCALTrigger::MakeSlidingTowers(const TClonesArray * amptrus, const TClonesArray * timeRtrus, 
394                                         const Int_t isupermod,TMatrixD &ampmax2, TMatrixD &ampmaxn){
395   
396   // Output from module (2x2 cells from one module)
397   Int_t nModulesPhi  = fGeom->GetNModulesInTRUPhi(); // now 4 modules (3 div in phi)
398   if(isupermod > 9)
399     nModulesPhi =  nModulesPhi / 2 ; // Half size SM. Not Final.
400   // 
401   Int_t nModulesEta  = fGeom->GetNModulesInTRUEta(); // now 24 modules (no division in eta)
402   Int_t nTRU         = fGeom->GetNTRU();
403   static int keyPrint = 0;
404   if(keyPrint) AliDebug(2,Form("MakeSlidingTowers : nTRU %i nModulesPhi %i nModulesEta %i ", 
405                                nTRU, nModulesPhi, nModulesEta ));
406   
407   Float_t amp2 = 0 ;
408   Float_t ampn = 0 ; 
409   for(Int_t i = 0; i < 4; i++){
410     for(Int_t j = 0; j < nTRU; j++){
411       ampmax2(i,j) = ampmaxn(i,j) = -1;
412     }
413   }
414   
415   // Create matrix that will contain 2x2 amplitude sums
416   // used to calculate the nxn sums
417   TMatrixD tru2x2(nModulesPhi/2,nModulesEta/2);
418   
419   // Loop over all TRUS in a supermodule
420   for(Int_t itru = 0 + isupermod * nTRU ; itru < (isupermod+1)*nTRU ; itru++) {
421     TMatrixD * amptru   = dynamic_cast<TMatrixD *>(amptrus->At(itru)) ;
422     TMatrixD * timeRtru = dynamic_cast<TMatrixD *>(timeRtrus->At(itru)) ;
423     Int_t mtru = itru - isupermod*nTRU ; // Number of TRU in Supermodule !!
424     
425     if(!amptru || !timeRtru){
426       AliError("Amplitude or Time TRU matrix not available");
427       return;
428     }
429     
430     // Sliding 2x2, add 2x2 amplitudes (NOT OVERLAP)
431     for(Int_t irow = 0 ; irow <  nModulesPhi; irow +=2){ 
432       for(Int_t icol = 0 ; icol < nModulesEta ; icol +=2){
433         amp2 = (*amptru)(irow,icol) +(*amptru)(irow+1,icol)+
434         (*amptru)(irow,icol+1)+(*amptru)(irow+1,icol+1);
435         
436         //Fill matrix with added 2x2 towers for use in nxn sums
437         tru2x2(irow/2,icol/2) = amp2 ;
438         //Select 2x2 maximum sums to select L0 
439         if(amp2 > ampmax2(0,mtru)){
440           ampmax2(0,mtru) = amp2 ; 
441           ampmax2(1,mtru) = irow;
442           ampmax2(2,mtru) = icol;
443         }
444       }
445     }
446     
447     ampmax2(3,mtru) = 0.;
448     if(GetTimeKey()) {
449       // Find most recent time in the selected 2x2 towers
450       Int_t row2 =  static_cast <Int_t> (ampmax2(1,mtru));
451       Int_t col2 =  static_cast <Int_t> (ampmax2(2,mtru));
452       for(Int_t i = 0; i<2; i++){
453         for(Int_t j = 0; j<2; j++){
454           if((*amptru)(row2+i,col2+j) > 0 &&  (*timeRtru)(row2+i,col2+j)> 0){     
455             if((*timeRtru)(row2+i,col2+j) >  ampmax2(3,mtru)  )
456               ampmax2(3,mtru) =  (*timeRtru)(row2+i,col2+j); // max time
457           }
458         }
459       }
460     }
461     
462     //Sliding nxn, add nxn amplitudes  (OVERLAP)
463     if(fPatchSize > 0){
464       for(Int_t irow = 0 ; irow <  nModulesPhi/2; irow++){ 
465         for(Int_t icol = 0 ; icol < nModulesEta/2; icol++){
466           ampn = 0;
467           if( (irow+fPatchSize) < nModulesPhi/2 && (icol+fPatchSize) < nModulesEta/2){ //Avoid exit the TRU
468             for(Int_t i = 0 ; i <= fPatchSize ; i++)
469               for(Int_t j = 0 ; j <= fPatchSize ; j++)
470                 ampn += tru2x2(irow+i,icol+j);
471             //Select nxn maximum sums to select L1 
472             if(ampn > ampmaxn(0,mtru)){
473               ampmaxn(0,mtru) = ampn ; 
474               ampmaxn(1,mtru) = irow;
475               ampmaxn(2,mtru) = icol;
476             }
477           }
478         }
479       }
480       
481       ampmaxn(3,mtru) = 0.; // Was 1 , I don't know why
482       if(GetTimeKey()) {
483         //Find most recent time in selected nxn cell
484         Int_t rown =  static_cast <Int_t> (ampmaxn(1,mtru));
485         Int_t coln =  static_cast <Int_t> (ampmaxn(2,mtru));
486         for(Int_t i = 0; i<4*fPatchSize; i++){
487           for(Int_t j = 0; j<4*fPatchSize; j++){
488             if( (rown+i) < nModulesPhi && (coln+j) < nModulesEta){//Avoid exit the TRU
489               if((*amptru)(rown+i,coln+j) > 0 &&  (*timeRtru)(rown+i,coln+j)> 0){
490                 if((*timeRtru)(rown+i,coln+j) >  ampmaxn(3,mtru)  )
491                   ampmaxn(3,mtru) =  (*timeRtru)(rown+i,coln+j); // max time
492               }
493             }
494           }
495         }
496       }
497     } else { // copy 2x2 to nxn  
498       ampmaxn(0,mtru) =  ampmax2(0,mtru); 
499       ampmaxn(1,mtru) =  ampmax2(1,mtru);
500       ampmaxn(2,mtru) =  ampmax2(2,mtru);
501       ampmaxn(3,mtru) =  ampmax2(3,mtru);
502     }
503   }
504   if(keyPrint) AliDebug(2,Form(" : MakeSlidingTowers -OUt \n"));
505 }
506
507 //____________________________________________________________________________
508 void AliEMCALTrigger::Print(const Option_t * opt) const 
509 {
510   
511   //Prints main parameters
512   
513   if(! opt)
514     return;
515   AliTriggerInput* in = 0x0 ;
516   AliInfo(Form(" fSimulation %i (input option) : #digits %i\n", fSimulation, fDigitsList->GetEntries()));
517   AliInfo(Form(" fTimeKey    %i  \n ", fTimeKey));
518
519   AliInfo(Form("\t Maximum Amplitude after Sliding Cell, \n")) ; 
520   AliInfo(Form("\t -2x2 cells sum (not overlapped): %10.2f, in Super Module %d\n",
521           f2x2MaxAmp,f2x2SM)) ; 
522   AliInfo(Form("\t -2x2 from row %d to row %d and from column %d to column %d\n", f2x2ModulePhi, f2x2ModulePhi+2, f2x2ModuleEta, f2x2ModuleEta+2)); 
523   AliInfo(Form("\t -2x2 Isolation Patch %d x %d, Amplitude out of 2x2 patch is %f, threshold %f, Isolated? %d \n", 2*fIsolPatchSize+2, 2*fIsolPatchSize+2,  f2x2AmpOutOfPatch,  f2x2AmpOutOfPatchThres,static_cast<Int_t> (fIs2x2Isol))); 
524   if(fPatchSize > 0){
525     AliInfo(Form("\t Patch Size, n x n: %d x %d cells\n",2*(fPatchSize+1), 2*(fPatchSize+1)));
526     AliInfo(Form("\t -nxn cells sum (overlapped)    : %10.2f, in Super Module %d\n", fnxnMaxAmp,fnxnSM)); 
527     AliInfo(Form("\t -nxn from row %d to row %d and from column %d to column %d\n", fnxnModulePhi, fnxnModulePhi+4*fPatchSize, fnxnModuleEta, fnxnModuleEta+4*fPatchSize)) ; 
528     AliInfo(Form("\t -nxn Isolation Patch %d x %d, Amplitude out of nxn patch is %f, threshold %f, Isolated? %d \n", 4*fIsolPatchSize+2*(fPatchSize+1),4*fIsolPatchSize+2*(fPatchSize+1) ,  fnxnAmpOutOfPatch,  fnxnAmpOutOfPatchThres,static_cast<Int_t> (fIsnxnIsol) )); 
529   }
530   
531   AliInfo(Form("\t Isolate in SuperModule? %d\n", fIsolateInSuperModule)) ;  
532   AliInfo(Form("\t Threshold for LO %10.2f\n", fL0Threshold));  
533
534   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_L0" );
535   if(in->GetValue())
536     AliInfo(Form("\t *** EMCAL LO is set ***\n")); 
537   
538   AliInfo(Form("\t Gamma Low Pt Threshold for L1 %10.2f\n", fL1GammaLowPtThreshold));
539   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_GammaLPt_L1" );
540   if(in->GetValue())
541     AliInfo(Form("\t *** EMCAL Gamma Low Pt for L1 is set ***\n"));
542
543   AliInfo(Form("\t Gamma Medium Pt Threshold for L1 %10.2f\n", fL1GammaMediumPtThreshold));  
544   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_GammaMPt_L1" );
545   if(in->GetValue())
546     AliInfo(Form("\t *** EMCAL Gamma Medium Pt for L1 is set ***\n"));
547
548   AliInfo(Form("\t Gamma High Pt Threshold for L1 %10.2f\n", fL1GammaHighPtThreshold));  
549   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_GammaHPt_L1" );
550   if(in->GetValue())
551     AliInfo(Form("\t *** EMCAL Gamma High Pt for L1 is set ***\n")) ;
552
553 }
554
555 //____________________________________________________________________________
556 void AliEMCALTrigger::SetTriggers(const TClonesArray * ampmatrix,const Int_t iSM, 
557                                   const TMatrixD &ampmax2, 
558                                   const TMatrixD &ampmaxn)  
559 {
560   //Checks the 2x2 and nxn maximum amplitude per each TRU and 
561   //compares with the different L0 and L1 triggers thresholds
562   Float_t max2[] = {-1,-1,-1,-1} ;
563   Float_t maxn[] = {-1,-1,-1,-1} ;
564   Int_t   mtru2  = -1 ;
565   Int_t   mtrun  = -1 ;
566
567   Int_t nTRU = fGeom->GetNTRU();
568
569   //Find maximum summed amplitude of all the TRU 
570   //in a Super Module
571     for(Int_t i = 0 ; i < nTRU ; i++){
572       if(max2[0] < ampmax2(0,i) ){
573         max2[0] =  ampmax2(0,i) ; // 2x2 summed max amplitude
574         max2[1] =  ampmax2(1,i) ; // corresponding phi position in TRU
575         max2[2] =  ampmax2(2,i) ; // corresponding eta position in TRU
576         max2[3] =  ampmax2(3,i) ; // corresponding most recent time
577         mtru2   = i ;
578       }
579       if(maxn[0] < ampmaxn(0,i) ){
580         maxn[0] =  ampmaxn(0,i) ; // nxn summed max amplitude
581         maxn[1] =  ampmaxn(1,i) ; // corresponding phi position in TRU
582         maxn[2] =  ampmaxn(2,i) ; // corresponding eta position in TRU
583         maxn[3] =  ampmaxn(3,i) ; // corresponding most recent time
584         mtrun   = i ;
585       }
586     }
587
588   //--------Set max amplitude if larger than in other Super Modules------------
589   Float_t maxtimeR2 = -1 ;
590   Float_t maxtimeRn = -1 ;
591   static AliEMCALRawUtils rawUtil;
592   // Int_t nTimeBins = rawUtil.GetRawFormatTimeBins() ;
593   Int_t nTimeBins = TIMEBINS;  //changed by PTH
594
595   //Set max of 2x2 amplitudes and select L0 trigger
596   if(max2[0] > f2x2MaxAmp ){
597     //    if(max2[0] > 5) printf(" L0 : iSM %i: max2[0] %5.0f :  max2[3] %5.0f  (maxtimeR2) \n", 
598     //                     iSM, max2[0], max2[3]);
599     f2x2MaxAmp  = max2[0] ;
600     f2x2SM      = iSM ;
601     maxtimeR2   = max2[3] ;
602     fGeom->GetModulePhiEtaIndexInSModuleFromTRUIndex(mtru2, 
603                                                   static_cast<Int_t>(max2[1]),
604                                                   static_cast<Int_t>(max2[2]),
605                                                    f2x2ModulePhi,f2x2ModuleEta);
606     
607     //Isolated patch?
608     if(fIsolateInSuperModule)
609       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iSM, mtru2,  f2x2MaxAmp, f2x2ModulePhi,f2x2ModuleEta) ;
610     else
611       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iSM, mtru2,  f2x2MaxAmp,  static_cast<Int_t>(max2[1]), static_cast<Int_t>(max2[2])) ;
612
613     if(GetTimeKey()) {
614     //Transform digit amplitude in Raw Samples
615       if (fADCValuesLow2x2 == 0) {
616         fADCValuesLow2x2  = new Int_t[nTimeBins];
617         fADCValuesHigh2x2 = new Int_t[nTimeBins];
618       }
619       //printf(" maxtimeR2 %12.5e (1)\n", maxtimeR2);
620       //  rawUtil.RawSampledResponse(maxtimeR2 * AliEMCALRawUtils::GetRawFormatTimeBin(), 
621       //                                 f2x2MaxAmp, fADCValuesHigh2x2, fADCValuesLow2x2) ; 
622        
623       //  rawUtil.RawSampledResponse(maxtimeR2*TIMEBINMAX/TIMEBINS, 
624       //                                 f2x2MaxAmp, fADCValuesHigh2x2, fADCValuesLow2x2) ; 
625       
626       AliEMCALRawResponse::RawSampledResponse( maxtimeR2*TIMEBINMAX/TIMEBINS, 
627                                                f2x2MaxAmp, fADCValuesHigh2x2, fADCValuesLow2x2) ; 
628
629     // Set Trigger Inputs, compare ADC time bins until threshold is attained
630     // Set L0
631       for(Int_t i = 0 ; i < nTimeBins ; i++){
632       //      printf(" fADCValuesHigh2x2[%i] %i : %i \n", i, fADCValuesHigh2x2[i], fADCValuesLow2x2[i]); 
633         if(fADCValuesHigh2x2[i] >= fL0Threshold          || fADCValuesLow2x2[i] >= fL0Threshold){
634           SetInput("EMCAL_L0") ;
635           break;
636         }
637       }
638     } else {
639       // Nov 5 - no analysis of time information
640       if(f2x2MaxAmp >= fL0Threshold) { // should add the low amp too
641         SetInput("EMCAL_L0");
642       }
643     }
644   }
645   
646   //------------Set max of nxn amplitudes and select L1 trigger---------
647   if(maxn[0] > fnxnMaxAmp ){
648     fnxnMaxAmp  = maxn[0] ;
649     fnxnSM      = iSM ;
650     maxtimeRn   = maxn[3] ;
651     fGeom->GetModulePhiEtaIndexInSModuleFromTRUIndex(mtrun, 
652                                                   static_cast<Int_t>(maxn[1]),
653                                                   static_cast<Int_t>(maxn[2]),
654                                                   fnxnModulePhi,fnxnModuleEta) ; 
655     
656     //Isolated patch?
657     if(fIsolateInSuperModule)
658       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iSM, mtrun,  fnxnMaxAmp, fnxnModulePhi, fnxnModuleEta) ;
659     else
660       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iSM, mtrun,  fnxnMaxAmp,  static_cast<Int_t>(maxn[1]), static_cast<Int_t>(maxn[2])) ;
661     
662     if(GetTimeKey()) {
663     //Transform digit amplitude in Raw Samples
664       if (fADCValuesLownxn == 0) {
665         fADCValuesHighnxn = new Int_t[nTimeBins];
666         fADCValuesLownxn  = new Int_t[nTimeBins];
667       }
668       //  rawUtil.RawSampledResponse(maxtimeRn * AliEMCALRawUtils::GetRawFormatTimeBin(), 
669       //   fnxnMaxAmp, fADCValuesHighnxn, fADCValuesLownxn) ;
670
671       //rawUtil.RawSampledResponse(maxtimeRn*TIMEBINMAX/TIMEBINS, 
672       //                                 fnxnMaxAmp, fADCValuesHighnxn, fADCValuesLownxn) ;
673
674       AliEMCALRawResponse::RawSampledResponse (maxtimeRn*TIMEBINMAX/TIMEBINS, 
675                                                fnxnMaxAmp, fADCValuesHighnxn, fADCValuesLownxn) ;
676       
677     //Set Trigger Inputs, compare ADC time bins until threshold is attained
678     //SetL1 Low
679       for(Int_t i = 0 ; i < nTimeBins ; i++){
680         if(fADCValuesHighnxn[i] >= fL1GammaLowPtThreshold  || fADCValuesLownxn[i] >= fL1GammaLowPtThreshold){
681           SetInput("EMCAL_GammaLPt_L1") ;
682           break; 
683         }
684       }
685     
686     //SetL1 Medium
687       for(Int_t i = 0 ; i < nTimeBins ; i++){
688         if(fADCValuesHighnxn[i] >= fL1GammaMediumPtThreshold || fADCValuesLownxn[i] >= fL1GammaMediumPtThreshold){
689           SetInput("EMCAL_GammaMPt_L1") ;
690           break;
691         }
692       }
693     
694     //SetL1 High
695       for(Int_t i = 0 ; i < nTimeBins ; i++){
696         if(fADCValuesHighnxn[i] >= fL1GammaHighPtThreshold || fADCValuesLownxn[i] >= fL1GammaHighPtThreshold){
697           SetInput("EMCAL_GammaHPt_L1") ;
698           break;
699         }
700       }
701     } else {
702       // Nov 5 - no analysis of time information
703       if(fnxnMaxAmp >= fL1GammaLowPtThreshold) { // should add the low amp too
704         SetInput("EMCAL_GammaLPt_L1") ; //SetL1 Low
705       }
706       if(fnxnMaxAmp >= fL1GammaMediumPtThreshold) { // should add the low amp too
707         SetInput("EMCAL_GammaMPt_L1") ; //SetL1 Medium
708       }
709       if(fnxnMaxAmp >= fL1GammaHighPtThreshold) { // should add the low amp too
710         SetInput("EMCAL_GammaHPt_L1") ; //SetL1 High
711       }
712     }
713   }
714 }
715
716 //____________________________________________________________________________
717 void AliEMCALTrigger::FillTRU(const TClonesArray * digits, TClonesArray * ampmatrix, TClonesArray * ampmatrixsmod, TClonesArray * timeRmatrix) {
718
719 //  Orders digits ampitudes list in fNTRU TRUs (384 cells) per supermodule. 
720 //  Each TRU is a TMatrixD, and they are kept in TClonesArrays. The number of 
721 //  TRU in phi is fNTRUPhi, and the number of TRU in eta is fNTRUEta.
722 //  Last 2 modules are half size in Phi, I considered that the number of TRU
723 //  is maintained for the last modules but decision not taken. If different, 
724 //  then this must be changed. Also fill a matrix with all amplitudes in supermodule for isolation studies. 
725  
726 //  Initilize and declare variables
727 //  List of TRU matrices initialized to 0.
728 //  printf("<I> AliEMCALTrigger::FillTRU() started : # digits %i\n", digits->GetEntriesFast());
729
730 // Nov 2, 2007.
731 // One input per EMCAL module so size of matrix is reduced by 4 (2x2 division case) 
732
733   Int_t nPhi        = fGeom->GetNPhi();
734   Int_t nZ          = fGeom->GetNZ();
735   Int_t nTRU        = fGeom->GetNTRU();
736   //  Int_t nTRUPhi     = fGeom->GetNTRUPhi();
737   Int_t nModulesPhi  = fGeom->GetNModulesInTRUPhi();
738   Int_t nModulesPhi2 = fGeom->GetNModulesInTRUPhi();
739   Int_t nModulesEta  = fGeom->GetNModulesInTRUEta();
740   //  printf("<I> AliEMCALTrigger::FillTRU() nTRU %i  nTRUPhi %i : nModulesPhi %i nModulesEta %i \n", 
741   //     nTRU, nTRUPhi, nModulesPhi, nModulesEta);
742
743   Int_t id       = -1; 
744   Float_t amp    = -1;
745   Float_t timeR  = -1;
746   Int_t iSupMod  = -1;
747   Int_t nModule  = -1;
748   Int_t nIphi    = -1;
749   Int_t nIeta    = -1;
750   Int_t iphi     = -1;
751   Int_t ieta     = -1;
752   // iphim, ietam - module indexes in SM 
753   Int_t iphim    = -1;
754   Int_t ietam    = -1;
755
756   //List of TRU matrices initialized to 0.
757   Int_t nSup = fGeom->GetNumberOfSuperModules();
758   for(Int_t k = 0; k < nTRU*nSup; k++){
759     TMatrixD amptrus(nModulesPhi,nModulesEta) ;
760     TMatrixD timeRtrus(nModulesPhi,nModulesEta) ;
761     // Do we need to initialise? I think TMatrixD does it by itself...
762     for(Int_t i = 0; i < nModulesPhi; i++){
763       for(Int_t j = 0; j < nModulesEta; j++){
764         amptrus(i,j) = 0.0;
765         timeRtrus(i,j) = 0.0;
766       }
767     }
768     new((*ampmatrix)[k])   TMatrixD(amptrus) ;
769     new((*timeRmatrix)[k]) TMatrixD(timeRtrus) ; 
770   }
771   
772   // List of Modules matrices initialized to 0.
773   for(Int_t k = 0; k < nSup ; k++){
774     int mphi = nPhi;
775     //    if(nSup>9) mphi = nPhi/2; // the same size
776     TMatrixD  ampsmods( mphi, nZ);
777     for(Int_t i = 0; i <  mphi; i++){
778       for(Int_t j = 0; j < nZ; j++){
779         ampsmods(i,j)   = 0.0;
780       }
781     }
782     new((*ampmatrixsmod)[k]) TMatrixD(ampsmods) ;
783   }
784
785   AliEMCALDigit * dig ;
786   
787   //Digits loop to fill TRU matrices with amplitudes.
788   for(Int_t idig = 0 ; idig < digits->GetEntriesFast() ; idig++){
789     
790     dig = dynamic_cast<AliEMCALDigit *>(digits->At(idig)) ;
791     if(dig){
792       amp    = Float_t(dig->GetAmplitude()); // Energy of the digit (arbitrary units)
793       id     = dig->GetId() ;          // Id label of the cell
794       timeR  = dig->GetTimeR() ;       // Earliest time of the digit
795       if(amp<=0.0) AliDebug(1,Form(" id %i amp %f \n", id, amp));
796       // printf(" FILLTRU : timeR %10.5e time %10.5e : amp %10.5e \n", timeR, dig->GetTime(), amp);
797       // Get eta and phi cell position in supermodule
798       Bool_t bCell = fGeom->GetCellIndex(id, iSupMod, nModule, nIphi, nIeta) ;
799       if(!bCell)
800         AliError(Form("%i Wrong cell id number %i ", idig, id)) ;
801       
802       fGeom->GetCellPhiEtaIndexInSModule(iSupMod,nModule,nIphi, nIeta,iphi,ieta);
803       // iphim, ietam - module indexes in SM
804       fGeom->GetModuleIndexesFromCellIndexesInSModule(iSupMod,iphi,ieta, iphim, ietam, nModule); 
805       //if(iSupMod >9) 
806       //printf("iSupMod %i nModule %i iphi %i  ieta %i  iphim %i  ietam %i \n",
807       //iSupMod,nModule, iphi, ieta, iphim, ietam); 
808       
809       // Check to which TRU in the supermodule belongs the cell. 
810       // Supermodules are divided in a TRU matrix of dimension 
811       // (fNTRUPhi,fNTRUEta).
812       // Each TRU is a cell matrix of dimension (nModulesPhi,nModulesEta)
813       
814       // First calculate the row and column in the supermodule 
815       // of the TRU to which the cell belongs.
816       Int_t row   = iphim / nModulesPhi;
817       Int_t col   = ietam / nModulesEta;
818       //Calculate label number of the TRU
819       Int_t itru  = fGeom->GetAbsTRUNumberFromNumberInSm(row, col, iSupMod);
820       
821       //Fill TRU matrix with cell values
822       TMatrixD * amptrus   = dynamic_cast<TMatrixD *>(ampmatrix->At(itru)) ;
823       TMatrixD * timeRtrus = dynamic_cast<TMatrixD *>(timeRmatrix->At(itru)) ;
824       
825       if(!amptrus || !timeRtrus){
826         AliError("Could not recover the TRU matrix with amplitudes or times");
827       }
828       else{
829         //Calculate row and column of the module inside the TRU with number itru
830         Int_t irow = iphim - row * nModulesPhi;
831         if(iSupMod > 9)
832           irow = iphim - row *  nModulesPhi2; // size of matrix the same
833         Int_t icol = ietam - col * nModulesEta;
834       
835         (*amptrus)(irow,icol)  += amp ;
836         if((*timeRtrus)(irow,icol) <0.0 || (*timeRtrus)(irow,icol) <= timeR){ // ??
837           (*timeRtrus)(irow,icol) = timeR ;
838         }
839       }
840       //printf(" ieta %i iphi %i iSM %i || col %i row %i : itru %i -> amp %f\n", 
841       //           ieta, iphi, iSupMod, col, row, itru, amp);     
842       //####################SUPERMODULE MATRIX ##################
843       TMatrixD * ampsmods   = dynamic_cast<TMatrixD *>(ampmatrixsmod->At(iSupMod)) ;
844       if(!ampsmods){
845         AliError("Could not recover the matrix per SM");
846         continue;
847       }
848       (*ampsmods)(iphim,ietam)  += amp ;
849       //    printf(" id %i iphim %i ietam %i SM %i : irow %i icol %i itru %i : amp %6.0f\n", 
850       //id, iphim, ietam, iSupMod, irow, icol, itru, amp); 
851     }
852     else AliError("Could not recover the digit");
853   }
854   //assert(0);
855   //printf("<I> AliEMCALTrigger::FillTRU() is ended \n");
856 }
857
858 //____________________________________________________________________________
859 void AliEMCALTrigger::Trigger() 
860 {
861   //Main Method to select triggers.
862   TH1::AddDirectory(0);
863
864   AliRunLoader *runLoader = AliRunLoader::Instance();
865   AliEMCALLoader *emcalLoader = 0;
866   if(runLoader) {
867     emcalLoader = dynamic_cast<AliEMCALLoader*>(runLoader->GetDetectorLoader("EMCAL"));
868   }
869  
870   //Load EMCAL Geometry
871   if (runLoader && runLoader->GetAliRun()){
872     AliEMCAL* emcal = dynamic_cast<AliEMCAL*>(runLoader->GetAliRun()->GetDetector("EMCAL"));
873     if(emcal)fGeom = emcal->GetGeometry();
874   }
875   
876   if (!fGeom)    
877     fGeom = AliEMCALGeometry::GetInstance(AliEMCALGeometry::GetDefaultGeometryName());   
878
879   if (!fGeom)
880     AliFatal("Did not get geometry from EMCALLoader");
881   
882   //Define parameters
883   Int_t nSuperModules = fGeom->GetNumberOfSuperModules() ; //12 SM in EMCAL
884   Int_t nTRU       = fGeom->GetNTRU();    // 3 TRU per super module
885
886   //Intialize data members each time the trigger is called in event loop
887   f2x2MaxAmp = -1; f2x2ModulePhi = -1;  f2x2ModuleEta = -1;
888   fnxnMaxAmp = -1; fnxnModulePhi = -1;  fnxnModuleEta = -1;
889
890   // Take the digits list if simulation
891   if(fSimulation && runLoader && emcalLoader){ // works than run seperate macros
892     runLoader->LoadDigits("EMCAL");
893     fDigitsList = emcalLoader->Digits() ;
894     runLoader->LoadSDigits("EMCAL");
895   }
896   // Digits list should be set by method SetDigitsList(TClonesArray * digits)
897   if(!fDigitsList)
898     AliFatal("Digits not found !") ;
899   
900   //Take the digits list 
901   
902   // Delete old if unzero
903   if(fAmpTrus)     {fAmpTrus->Delete();   delete fAmpTrus;}
904   if(fTimeRtrus)   {fTimeRtrus->Delete(); delete fTimeRtrus;}
905   if(fAmpSMods)    {fAmpSMods->Delete();  delete fAmpSMods;}
906   // Fill TRU and SM matrix    
907   fAmpTrus   = new TClonesArray("TMatrixD",nTRU);
908   fAmpTrus->SetName("AmpTrus");
909   fTimeRtrus = new TClonesArray("TMatrixD",nTRU);
910   fTimeRtrus->SetName("TimeRtrus");
911   fAmpSMods  = new TClonesArray("TMatrixD",nSuperModules);
912   fAmpSMods->SetName("AmpSMods");
913   
914   FillTRU(fDigitsList, fAmpTrus, fAmpSMods, fTimeRtrus);
915
916   // Jet stuff - only one case, no freedom here
917   if(fGeom->GetNEtaSubOfTRU() == 6) {
918     if(fAmpJetMatrix) {delete fAmpJetMatrix; fAmpJetMatrix=0;}
919     if(fJetMatrixE)   {delete fJetMatrixE; fJetMatrixE=0;}
920
921     fAmpJetMatrix = new TMatrixD(17,12); // 17-phi(row), 12-eta(col)
922     fJetMatrixE = new TH2F("fJetMatrixE"," E of max patch in (#phi,#eta)", 
923     17, 80.*TMath::DegToRad(), (180.+20.*2/3.)*TMath::DegToRad(), 12, -0.7, 0.7);
924     for(Int_t row=0; row<fAmpJetMatrix->GetNrows(); row++) {
925       for(Int_t col=0; col<fAmpJetMatrix->GetNcols(); col++) {
926         (*fAmpJetMatrix)(row,col) = 0.;
927       }
928     }
929     FillJetMatrixFromSMs(fAmpSMods, fAmpJetMatrix, fGeom);
930   }
931   if(!CheckConsistentOfMatrixes()) assert(0);
932
933   // Do Tower Sliding and select Trigger
934   // Initialize varible that will contain maximum amplitudes and 
935   // its corresponding tower position in eta and phi, and time.
936   TMatrixD ampmax2(4,nTRU) ; // 0-max amp, 1-irow, 2-icol, 3-timeR
937   TMatrixD ampmaxn(4,nTRU) ;
938   
939   for(Int_t iSM = 0 ; iSM < nSuperModules ; iSM++) {
940     //Do 2x2 and nxn sums, select maximums. 
941
942     MakeSlidingTowers(fAmpTrus, fTimeRtrus, iSM, ampmax2, ampmaxn);
943     
944     // Set the trigger
945     if(fIsolateInSuperModule) // here some discripency between tru and SM
946       SetTriggers(fAmpSMods,iSM,ampmax2,ampmaxn) ;
947     if(!fIsolateInSuperModule)
948       SetTriggers(fAmpTrus,iSM,ampmax2,ampmaxn) ;
949   }
950   
951   // Do patch sliding and select Jet Trigger
952   // 0-max amp-meanFromVZERO(if), 1-irow, 2-icol, 3-timeR, 
953   // 4-max amp , 5-meanFromVZERO (Nov 25, 2007)
954   // fAmpJetMax(6,1)
955   MakeSlidingPatch((*fAmpJetMatrix), fNJetPatchPhi, fAmpJetMax); // no timing information here
956
957   //Print();
958   // fDigitsList = 0;
959 }
960
961 //____________________________________________________________________________
962 void AliEMCALTrigger::GetTriggerInfo(TArrayF &triggerPosition, TArrayF &triggerAmplitudes) const
963 {
964   // Template - should be defined; Nov 5, 2007
965   triggerPosition[0]   = 0.; 
966   triggerAmplitudes[0] = 0.;
967 }
968
969 //____________________________________________________________________________
970 void AliEMCALTrigger::FillJetMatrixFromSMs(TClonesArray *ampmatrixsmod, TMatrixD* jetMat, AliEMCALGeometry *g)
971 {
972   // Nov 5, 2007
973   // Fill matrix for jet trigger from SM matrixes of modules
974   //
975   static int keyPrint = 0;
976
977   if(ampmatrixsmod==0 || jetMat==0 || g==0) return;
978   Double_t amp = 0.0, ampSum=0.0;
979
980   Int_t nEtaModSum = g->GetNZ()   / g->GetNEtaSubOfTRU(); // should be 4 
981   Int_t nPhiModSum = g->GetNPhi() / g->GetNTRUPhi();      // should be 4 
982
983   if(keyPrint) AliDebug(2,Form("%s",Form(" AliEMCALTrigger::FillJetMatrixFromSMs | nEtaModSum %i : nPhiModSum %i \n", nEtaModSum, nPhiModSum)));
984   Int_t jrow=0, jcol=0;  // indexes of jet matrix
985   Int_t nEtaSM=0, nPhiSM=0;
986   for(Int_t iSM=0; iSM<ampmatrixsmod->GetEntries(); iSM++) {
987     TMatrixD * ampsmods   = dynamic_cast<TMatrixD *>(ampmatrixsmod->At(iSM));
988     
989     if(!ampsmods) return;
990     
991     Int_t nrow = ampsmods->GetNrows();
992     Int_t ncol = ampsmods->GetNcols();
993     //printf("%s",Form(" ######## SM %i : nrow %i : ncol %i ##### \n", iSM, nrow, ncol));
994     for(Int_t row=0; row<nrow; row++) {
995       for(Int_t col=0; col<ncol; col++) {
996         amp  = (*ampsmods)(row,col);
997         nPhiSM = iSM / 2; 
998         nEtaSM = iSM % 2;
999         if       (amp>0.0) {
1000            if(keyPrint) AliDebug(2,Form("%s",Form(" ** nPhiSm %i : nEtaSM %i : row %2.2i : col %2.2i -> ", nPhiSM, nEtaSM, row, col))); 
1001           if(nEtaSM == 0) { // positive Z
1002             jrow = 3*nPhiSM + row/nPhiModSum;
1003             jcol = 6 + col / nEtaModSum;
1004           } else {         // negative Z
1005             if(iSM<=9) jrow = 3*nPhiSM + 2 - row/nPhiModSum;
1006             else       jrow = 3*nPhiSM + 1 - row/nPhiModSum; // half size 
1007             jcol = 5 - col / nEtaModSum;
1008           }
1009           if(keyPrint) AliDebug(2,Form("%s",Form(" jrow %2.2i : jcol %2.2i : amp %f (jetMat) \n", jrow, jcol, amp))); 
1010
1011           (*jetMat)(jrow,jcol) += amp;
1012           ampSum += amp; // For controling
1013         } else if(amp<0.0) {
1014           AliDebug(1,Form(" jrow %2.2i : jcol %2.2i : amp %f (jetMat: amp<0) \n", jrow, jcol, amp)); 
1015           assert(0);
1016         }
1017       }
1018     }
1019   } // cycle on SM
1020   if(ampSum <= 0.0) AliDebug(1,Form("ampSum %f (<=0.0) ", ampSum));
1021 }
1022
1023 //____________________________________________________________________________
1024 void AliEMCALTrigger::MakeSlidingPatch(const TMatrixD &jm, const Int_t nPatchSize, TMatrixD &ampJetMax)
1025 {
1026   // Sliding patch : nPatchSize x nPatchSize (OVERLAP)
1027   static int keyPrint = 0;
1028   if(keyPrint) AliDebug(2,Form(" AliEMCALTrigger::MakeSlidingPatch() was started \n"));
1029   Double_t ampCur = 0.0, e=0.0;
1030   ampJetMax(0,0)  = 0.0;
1031   ampJetMax(3,0)  = 0.0; // unused now
1032   ampJetMax(4,0)  = ampJetMax(5,0)  = 0.0;
1033   for(Int_t row=0; row<fAmpJetMatrix->GetNrows(); row ++) {
1034     for(Int_t col=0; col<fAmpJetMatrix->GetNcols(); col++) {
1035       ampCur = 0.;
1036       // check on patch size
1037       if( (row+nPatchSize-1) <  fAmpJetMatrix->GetNrows() && (col+nPatchSize-1) < fAmpJetMatrix->GetNcols()){
1038         for(Int_t i = 0 ; i < nPatchSize ; i++) {
1039           for(Int_t j = 0 ; j < nPatchSize ; j++) {
1040             ampCur += jm(row+i, col+j);
1041           }
1042         } // end cycle on patch
1043         if(ampCur > ampJetMax(0,0)){
1044           ampJetMax(0,0) = ampCur; 
1045           ampJetMax(1,0) = row;
1046           ampJetMax(2,0) = col;
1047         }
1048       } // check on patch size
1049     }
1050   }
1051   if(keyPrint) AliDebug(2,Form(" ampJetMax %i row %2i->%2i col %2i->%2i \n", Int_t(ampJetMax(0,0)), Int_t(ampJetMax(1,0)), Int_t(ampJetMax(1,0))+nPatchSize-1, Int_t(ampJetMax(2,0)), Int_t(ampJetMax(2,0))+nPatchSize-1));
1052
1053   Double_t eCorrJetMatrix=0.0;
1054   if(fVZER0Mult > 0.0) {
1055   // Correct patch energy (adc) and jet patch matrix energy
1056     Double_t meanAmpBG = GetMeanEmcalPatchEnergy(Int_t(fVZER0Mult), nPatchSize)/0.0153;
1057     ampJetMax(4,0) = ampJetMax(0,0);
1058     ampJetMax(5,0) = meanAmpBG;
1059
1060     Double_t eCorr     = ampJetMax(0,0) - meanAmpBG; 
1061     AliDebug(2,Form(" ampJetMax(0,0) %f meanAmpBG %f  eCorr %f : ampJetMax(4,0) %f \n",
1062            ampJetMax(0,0), meanAmpBG, eCorr, ampJetMax(5,0)));
1063     ampJetMax(0,0)     = eCorr;
1064     // --
1065     eCorrJetMatrix = GetMeanEmcalEnergy(Int_t(fVZER0Mult)) / 208.;
1066   }
1067   // Fill patch energy matrix
1068   for(int row=Int_t(ampJetMax(1,0)); row<Int_t(ampJetMax(1,0))+nPatchSize; row++) {
1069     for(int col=Int_t(ampJetMax(2,0)); col<Int_t(ampJetMax(2,0))+nPatchSize; col++) {
1070       e = Double_t(jm(row,col)*0.0153);  // 0.0153 - hard coded now
1071       if(eCorrJetMatrix > 0.0) { // BG subtraction case
1072         e -= eCorrJetMatrix;
1073         fJetMatrixE->SetBinContent(row+1, col+1, e);
1074       } else if(e > 0.0) {
1075         fJetMatrixE->SetBinContent(row+1, col+1, e);
1076       }
1077     }
1078   }
1079   // PrintJetMatrix();
1080   // Set the jet trigger(s), multiple threshold now, Nov 19,2007
1081   for(Int_t i=0; i<fNJetThreshold; i++ ) {
1082     if(ampJetMax(0,0) >= fL1JetThreshold[i]) {
1083       SetInput(GetNameOfJetTrigger(i)); 
1084     }
1085   }
1086 }
1087
1088 //____________________________________________________________________________
1089 Double_t AliEMCALTrigger::GetEmcalSumAmp() const 
1090
1091   // Return sum of amplidutes from EMCal
1092   // Used calibration coefficeint for transition to energy
1093   return fAmpJetMatrix >0 ?fAmpJetMatrix->Sum() :0.0;
1094 }
1095
1096 //____________________________________________________________________________
1097 void AliEMCALTrigger::PrintJetMatrix() const
1098 {
1099   //  fAmpJetMatrix : (17,12); // 17-phi(row), 12-eta(col)
1100   if(fAmpJetMatrix == 0) return;
1101
1102   AliInfo(Form("\n ####  jetMatrix : (%i,%i) ##### \n ", 
1103                fAmpJetMatrix->GetNrows(), fAmpJetMatrix->GetNcols()));
1104   PrintMatrix(*fAmpJetMatrix);
1105 }
1106
1107 //____________________________________________________________________________
1108 void AliEMCALTrigger::PrintAmpTruMatrix(Int_t ind) const
1109 {
1110  // Print matrix with TRU patches
1111   TMatrixD * tru = dynamic_cast<TMatrixD *>(fAmpTrus->At(ind));
1112   if(tru == 0) return;
1113   AliInfo(Form("\n ####  Amp TRU matrix(%i) : (%i,%i) ##### \n ", 
1114          ind, tru->GetNrows(), tru->GetNcols()));
1115   PrintMatrix(*tru);
1116 }
1117
1118 //____________________________________________________________________________
1119 void AliEMCALTrigger::PrintAmpSmMatrix(Int_t ind) const
1120 {
1121         // Print matrix with SM amplitudes
1122         TMatrixD * sm = dynamic_cast<TMatrixD *>(fAmpSMods->At(ind));
1123   if(sm == 0) return;
1124   AliInfo(Form("\n ####  Amp SM matrix(%i) : (%i,%i) ##### \n ", 
1125          ind, sm->GetNrows(), sm->GetNcols()));
1126   PrintMatrix(*sm);
1127 }
1128
1129 //____________________________________________________________________________
1130 void AliEMCALTrigger::PrintMatrix(const TMatrixD &mat) const
1131 {
1132   //Print matrix object
1133   for(Int_t col=0; col<mat.GetNcols(); col++) AliInfo(Form(" %3i ", col));
1134   AliInfo(Form("\n -- \n"));
1135   for(Int_t row=0; row<mat.GetNrows(); row++) {
1136     AliInfo(Form(" row:%2i ", row));
1137     for(Int_t col=0; col<mat.GetNcols(); col++) {
1138       AliInfo(Form(" %4i", (Int_t)mat(row,col)));
1139     }
1140     AliInfo("\n");
1141   }
1142 }
1143
1144 //____________________________________________________________________________
1145 Bool_t AliEMCALTrigger::CheckConsistentOfMatrixes(const Int_t pri)
1146 {
1147   // Check consitency of matrices
1148   Double_t sumSM = 0.0, smCur=0.0;
1149   Double_t sumTru=0.0,  sumTruInSM = 0.0, truSum=0.0;
1150   //  Bool_t key = kTRUE;
1151  
1152   for(Int_t i=0; i<fAmpSMods->GetEntries(); i++) {
1153     TMatrixD * sm = dynamic_cast<TMatrixD *>(fAmpSMods->At(i));
1154     if(sm) {
1155       smCur  = sm->Sum();
1156       sumSM += smCur;
1157
1158       sumTruInSM = 0.0;
1159       for(Int_t itru=0; itru<3; itru++) { // Cycle on tru inside SM
1160         Int_t ind = 3*i + itru;
1161         TMatrixD *tru = dynamic_cast<TMatrixD *>(fAmpTrus->At(ind)); 
1162         if(tru) {
1163           truSum = tru->Sum();
1164           sumTruInSM += truSum;
1165         }
1166       }
1167       sumTru += sumTruInSM;
1168
1169       if(sumTruInSM != smCur) {
1170         AliDebug(1,Form(" sm %i : smCur %f -> sumTruInSM %f \n", i, smCur, sumTruInSM));
1171         return kFALSE;
1172       }
1173     }
1174   }
1175   Double_t sumJetMat = fAmpJetMatrix->Sum();
1176         if(pri || TMath::Abs(sumSM-sumTru)>0.0001 || TMath::Abs(sumSM-sumJetMat) > 0.0001) 
1177    AliDebug(1,Form(" sumSM %f : sumTru %f : sumJetMat %f \n", sumSM, sumTru, sumJetMat)); 
1178         if(TMath::Abs(sumSM - sumTru)>0.0001 || TMath::Abs(sumSM-sumJetMat) > 0.0001) return kFALSE; 
1179   else                                       return kTRUE; 
1180 }
1181
1182 //____________________________________________________________________________
1183 void AliEMCALTrigger::Browse(TBrowser* b)
1184 {
1185   //Browse.
1186   if(&fInputs)      b->Add(&fInputs);
1187   if(fAmpTrus)      b->Add(fAmpTrus);
1188   if(fTimeRtrus)    b->Add(fTimeRtrus);
1189   if(fAmpSMods)     b->Add(fAmpSMods);
1190   if(fAmpJetMatrix) b->Add(fAmpJetMatrix);
1191   if(fJetMatrixE)   b->Add(fJetMatrixE);
1192   //  if(c) b->Add(c);
1193 }