]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSTrigger.cxx
Medium Jet pT L1 trigger input added
[u/mrichter/AliRoot.git] / PHOS / AliPHOSTrigger.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 /* $Log$ */
17
18 //_________________________________________________________________________  
19 //  Class for trigger analysis.
20 //  Digits are grouped in TRU's (Trigger Units). A TRU consist of 16x28 
21 //  crystals ordered fNTRUPhi x fNTRUZ. The algorithm searches all possible 
22 //  2x2 and nxn (n multiple of 2) crystal 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 //  Usage:
27 //
28 //  //Inside the event loop
29 //  AliPHOSTrigger *tr = new AliPHOSTrigger();//Init Trigger
30 //  tr->SetL0Threshold(100);
31 //  tr->SetL1JetLowPtThreshold(1000);
32 //  tr->SetL1JetMediumPtThreshold(10000);
33 //  tr->SetL1JetHighPtThreshold(20000);
34 //  ....
35 //  tr->Trigger(); //Execute Trigger
36 //  tr->Print(""); //Print data members after calculation.
37 //  
38 //
39 //*-- Author: Gustavo Conesa & Yves Schutz (IFIC, CERN) 
40 //////////////////////////////////////////////////////////////////////////////
41
42
43 // --- ROOT system ---
44
45 // --- ALIROOT system ---
46 #include "AliPHOS.h"
47 #include "AliPHOSTrigger.h" 
48 #include "AliPHOSGeometry.h"
49 #include "AliPHOSGetter.h" 
50 #include "AliPHOSPulseGenerator.h" 
51 #include "AliTriggerInput.h"
52
53
54 ClassImp(AliPHOSTrigger)
55
56 //______________________________________________________________________
57 AliPHOSTrigger::AliPHOSTrigger()
58   : AliTriggerDetector(),
59     f2x2MaxAmp(-1), f2x2CrystalPhi(-1),  f2x2CrystalEta(-1), f2x2SM(0),
60     fnxnMaxAmp(-1), fnxnCrystalPhi(-1),  fnxnCrystalEta(-1), fnxnSM(0),
61     fADCValuesHighnxn(0), fADCValuesLownxn(0),
62     fADCValuesHigh2x2(0), fADCValuesLow2x2(0), fDigitsList(0),
63     fL0Threshold(50), fL1JetLowPtThreshold(200),   fL1JetMediumPtThreshold(500),  
64     fL1JetHighPtThreshold(1000),
65     fNTRU(8), fNTRUZ(2), fNTRUPhi(4), 
66     fNCrystalsPhi(16),
67     fNCrystalsZ(28),
68     fPatchSize(1), fIsolPatchSize(1), 
69     f2x2AmpOutOfPatch(-1), fnxnAmpOutOfPatch(-1), 
70     f2x2AmpOutOfPatchThres(2),  fnxnAmpOutOfPatchThres(2), //2 GeV out of patch 
71     fIs2x2Isol(kFALSE), fIsnxnIsol(kFALSE),  
72     fSimulation(kTRUE), fIsolateInModule(kTRUE)
73 {
74   //ctor
75   fADCValuesHighnxn = 0x0; //new Int_t[fTimeBins];
76   fADCValuesLownxn  = 0x0; //new Int_t[fTimeBins];
77   fADCValuesHigh2x2 = 0x0; //new Int_t[fTimeBins];
78   fADCValuesLow2x2  = 0x0; //new Int_t[fTimeBins];
79
80   SetName("PHOS");
81   CreateInputs();
82   
83   //Print("") ; 
84 }
85
86 //____________________________________________________________________________
87 AliPHOSTrigger::AliPHOSTrigger(const AliPHOSTrigger & trig) : 
88   AliTriggerDetector(trig),
89   f2x2MaxAmp(trig.f2x2MaxAmp),
90   f2x2CrystalPhi(trig.f2x2CrystalPhi),
91   f2x2CrystalEta(trig.f2x2CrystalEta),
92   f2x2SM(trig.f2x2SM),
93   fnxnMaxAmp(trig.fnxnMaxAmp),
94   fnxnCrystalPhi(trig.fnxnCrystalPhi),
95   fnxnCrystalEta(trig.fnxnCrystalEta),
96   fnxnSM(trig.fnxnSM),
97   fADCValuesHighnxn(trig.fADCValuesHighnxn),
98   fADCValuesLownxn(trig.fADCValuesLownxn),
99   fADCValuesHigh2x2(trig.fADCValuesHigh2x2),
100   fADCValuesLow2x2(trig.fADCValuesLow2x2),
101   fDigitsList(trig.fDigitsList),
102   fL0Threshold(trig.fL0Threshold),
103   fL1JetLowPtThreshold(trig.fL1JetLowPtThreshold),
104   fL1JetMediumPtThreshold(trig.fL1JetMediumPtThreshold), 
105   fL1JetHighPtThreshold(trig.fL1JetHighPtThreshold),
106   fNTRU(trig.fNTRU),
107   fNTRUZ(trig.fNTRUZ),
108   fNTRUPhi(trig.fNTRUPhi),
109   fNCrystalsPhi(trig.fNCrystalsPhi),
110   fNCrystalsZ(trig. fNCrystalsZ),
111   fPatchSize(trig.fPatchSize),
112   fIsolPatchSize(trig.fIsolPatchSize), 
113   f2x2AmpOutOfPatch(trig.f2x2AmpOutOfPatch), 
114   fnxnAmpOutOfPatch(trig.fnxnAmpOutOfPatch), 
115   f2x2AmpOutOfPatchThres(trig.f2x2AmpOutOfPatchThres),  
116   fnxnAmpOutOfPatchThres(trig.fnxnAmpOutOfPatchThres), 
117   fIs2x2Isol(trig.fIs2x2Isol),
118   fIsnxnIsol(trig.fIsnxnIsol),  
119   fSimulation(trig.fSimulation), 
120   fIsolateInModule(trig.fIsolateInModule)
121 {
122   // cpy ctor
123 }
124
125 //_________________________________________________________________________
126 AliPHOSTrigger & AliPHOSTrigger::operator = (const AliPHOSTrigger &)
127 {
128   Fatal("operator =", "no implemented");
129   return *this;
130 }
131
132 void AliPHOSTrigger::CreateInputs()
133 {
134    // inputs 
135    
136    // Do not create inputs again!!
137    if( fInputs.GetEntriesFast() > 0 ) return;
138    
139    fInputs.AddLast( new AliTriggerInput( "PHOS_L0",       "PHOS L0", 0x02 ) );
140    fInputs.AddLast( new AliTriggerInput( "PHOS_JetHPt_L1","PHOS Jet High Pt L1", 0x04 ) );
141    fInputs.AddLast( new AliTriggerInput( "PHOS_JetMPt_L1","PHOS Jet Medium Pt L1", 0x08 ) );
142    fInputs.AddLast( new AliTriggerInput( "PHOS_JetLPt_L1","PHOS Jet Low Pt L1", 0x016 ) );
143  
144 }
145
146 //____________________________________________________________________________
147 void AliPHOSTrigger::FillTRU(const TClonesArray * digits, const AliPHOSGeometry * geom, TClonesArray * ampmatrixtru, TClonesArray * ampmatrixmod, TClonesArray * timeRmatrixtru) const {
148
149   //Orders digits ampitudes list and times in fNTRU TRUs (28x16 crystals) 
150   //per module. Each TRU is a TMatrixD, and they are kept in TClonesArrays. 
151   //In a module, the number of TRU in phi is fNTRUPhi, and the number of 
152   //TRU in eta is fNTRUZ. Also fill a matrix with all amplitudes in module for isolation studies. 
153
154   //Check data members
155   
156   if(fNTRUZ*fNTRUPhi != fNTRU)
157     Error("FillTRU"," Wrong number of TRUS per Z or Phi");
158
159   //Initilize and declare variables
160   Int_t nModules     = geom->GetNModules();
161   Int_t relid[4] ; 
162   Float_t amp   = -1;
163   Float_t timeR = -1;
164   Int_t id      = -1;
165
166   //List of TRU matrices initialized to 0.
167   for(Int_t k = 0; k < fNTRU*nModules ; k++){
168     TMatrixD  * amptrus   = new TMatrixD(fNCrystalsPhi,fNCrystalsZ) ;
169     TMatrixD  * timeRtrus = new TMatrixD(fNCrystalsPhi,fNCrystalsZ) ;
170     for(Int_t i = 0; i < fNCrystalsPhi; i++){
171       for(Int_t j = 0; j < fNCrystalsZ; j++){
172         (*amptrus)(i,j)   = 0.0;
173         (*timeRtrus)(i,j) = 0.0;
174       }
175     }
176     new((*ampmatrixtru)[k])   TMatrixD(*amptrus) ;
177     new((*timeRmatrixtru)[k]) TMatrixD(*timeRtrus) ; 
178   }
179
180   //List of Modules matrices initialized to 0.
181   Int_t nmodphi = geom->GetNPhi();
182   Int_t nmodz = geom->GetNZ();
183   
184   for(Int_t k = 0; k < nModules ; k++){
185     TMatrixD  * ampmods   = new TMatrixD(nmodphi,nmodz) ;
186     for(Int_t i = 0; i < nmodphi; i++){
187       for(Int_t j = 0; j < nmodz; j++){
188         (*ampmods)(i,j)   = 0.0;
189       }
190     }
191     new((*ampmatrixmod)[k])   TMatrixD(*ampmods) ;
192   }
193   
194   AliPHOSDigit * dig ;
195  
196   //Digits loop to fill TRU matrices with amplitudes.
197   for(Int_t idig = 0 ; idig < digits->GetEntriesFast() ; idig++){
198     
199     dig    = static_cast<AliPHOSDigit *>(digits->At(idig)) ;
200     amp    = dig->GetEnergy() ;   // Energy of the digit 
201     id     = dig->GetId() ;    // Id label of the cell
202     timeR  = dig->GetTimeR() ; // Earliest time of the digit
203     geom->AbsToRelNumbering(id, relid) ;
204     //Transform digit number into 4 numbers
205     //relid[0] = module
206     //relid[1] = EMC (0) or CPV (-1)
207     //relid[2] = row <= 64 (fNPhi)
208     //relid[3] = column <= 56 (fNZ)
209     
210     if(relid[1] == 0){//Not CPV, Only EMC digits
211       //############# TRU ###################
212       //Check to which TRU in the supermodule belongs the crystal. 
213       //Supermodules are divided in a TRU matrix of dimension 
214       //(fNTRUPhi,fNTRUZ).
215       //Each TRU is a crystal matrix of dimension (fNCrystalsPhi,fNCrystalsZ)
216       
217       //First calculate the row and column in the supermodule 
218       //of the TRU to which the crystal belongs.
219       Int_t col   = (relid[3]-1)/fNCrystalsZ+1; 
220       Int_t row   = (relid[2]-1)/fNCrystalsPhi+1;
221  
222       //Calculate label number of the TRU  
223       Int_t itru  = (row-1) + (col-1)*fNTRUPhi + (relid[0]-1)*fNTRU ;
224
225       //Fill TRU matrix with crystal values
226       TMatrixD * amptrus   = dynamic_cast<TMatrixD *>(ampmatrixtru->At(itru)) ;
227       TMatrixD * timeRtrus = dynamic_cast<TMatrixD *>(timeRmatrixtru->At(itru)) ;
228
229       //Calculate row and column of the crystal inside the TRU with number itru
230       Int_t irow = (relid[2]-1) - (row-1) *  fNCrystalsPhi;     
231       Int_t icol = (relid[3]-1) - (col-1) *  fNCrystalsZ;
232       
233       (*amptrus)(irow,icol)   = amp ;
234       (*timeRtrus)(irow,icol) = timeR ;
235
236       //####################MODULE MATRIX ##################
237       TMatrixD * ampmods   = dynamic_cast<TMatrixD *>(ampmatrixmod->At(relid[0]-1)) ;
238       (*ampmods)(relid[2]-1,relid[3]-1)   = amp ;
239     }
240   }
241 }
242
243 //______________________________________________________________________
244 void AliPHOSTrigger::GetCrystalPhiEtaIndexInModuleFromTRUIndex(const Int_t itru,const Int_t iphitru,const Int_t ietatru,Int_t &iphiMod,Int_t &ietaMod) const 
245 {
246   // This method transforms the (eta,phi) index of a crystals in a 
247   // TRU matrix into Super Module (eta,phi) index.
248   
249   // Calculate in which row and column in which the TRU are 
250   // ordered in the SM
251   Int_t col = itru/ fNTRUPhi + 1;
252   Int_t row = itru - (col-1)*fNTRUPhi + 1;
253   
254   //Calculate the (eta,phi) index in SM
255   
256   iphiMod = fNCrystalsPhi*(row-1) + iphitru + 1 ;
257   ietaMod = fNCrystalsZ*(col-1)   + ietatru + 1 ;
258
259 }
260
261 //____________________________________________________________________________
262 Bool_t AliPHOSTrigger::IsPatchIsolated(Int_t iPatchType, const TClonesArray * ampmatrixes, const Int_t imod, const Int_t mtru, const Float_t maxamp, const Int_t maxphi, const Int_t maxeta) {
263
264   //Calculate if the maximum patch found is isolated, find amplitude around maximum (2x2 or nxn) patch, 
265   //inside isolation patch . iPatchType = 0 means calculation for 2x2 patch, 
266   //iPatchType = 1 means calculation for nxn patch.
267   //In the next table there is an example of the different options of patch size and isolation patch size:
268   //                                                                                 Patch Size (fPatchSize)
269   //                                                             0                          1                                  2
270   //          fIsolPatchSize                 2x2 (not overlap)   4x4 (overlapped)        6x6(overlapped) ...
271   //                   1                                       4x4                      8x8                              10x10
272   //                   2                                       6x6                     12x12                           14x14    
273   //                   3                                       8x8                     16x16                           18x18
274                           
275   Bool_t b = kFALSE;
276   Float_t amp = 0;
277  
278  //Get matrix of TRU or Module with maximum amplitude patch.
279   Int_t itru = mtru+imod*fNTRU ; //number of tru, min 0 max 8*5.
280   TMatrixD * ampmatrix   = 0x0;
281   Int_t colborder = 0;
282   Int_t rowborder = 0;
283
284   if(fIsolateInModule){
285     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(imod)) ;
286     rowborder = fNCrystalsPhi*fNTRUPhi;
287     colborder = fNCrystalsZ*fNTRUZ;
288     AliDebug(2,"Isolate trigger in Module");
289   }
290   else{
291     ampmatrix = dynamic_cast<TMatrixD *>(ampmatrixes->At(itru)) ;
292     rowborder = fNCrystalsPhi;
293     colborder = fNCrystalsZ;
294     AliDebug(2,"Isolate trigger in TRU");
295   }
296
297   //Define patch cells
298   Int_t isolcells = fIsolPatchSize*(1+iPatchType);
299   Int_t ipatchcells = 2*(1+fPatchSize*iPatchType);
300   Int_t minrow =  maxphi - isolcells;
301   Int_t mincol =  maxeta - isolcells;
302   Int_t maxrow =  maxphi + isolcells + ipatchcells;
303   Int_t maxcol = maxeta +  isolcells + ipatchcells;
304
305   AliDebug(2,Form("Number of added Isol Cells %d, Patch Size %d",isolcells, ipatchcells));
306   AliDebug(2,Form("Patch: minrow %d, maxrow %d, mincol %d, maxcol %d",minrow,maxrow,mincol,maxcol));
307   
308   if(minrow < 0 || mincol < 0 || maxrow > rowborder || maxcol > colborder){
309     AliDebug(1,Form("Out of Module/TRU range, cannot isolate patch"));
310     return kFALSE;
311   }
312
313   //Add amplitudes in all isolation patch
314   for(Int_t irow = minrow ; irow <  maxrow; irow ++)
315     for(Int_t icol = mincol ; icol < maxcol ; icol ++)
316       amp += (*ampmatrix)(irow,icol);
317
318   AliDebug(2,Form("Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
319
320   if(amp < maxamp){
321     AliError(Form("Bad sum: Type %d, Maximum amplitude %f, patch+isol square %f",iPatchType, maxamp, amp));
322     return kFALSE;
323   }
324   else
325     amp-=maxamp; //Calculate energy in isolation patch that do not comes from maximum patch.
326   
327   AliDebug(2, Form("Maximum amplitude %f, Out of patch %f",maxamp, amp));
328
329   //Fill isolation amplitude data member and say if patch is isolated.
330   if(iPatchType == 0){ //2x2 case
331     f2x2AmpOutOfPatch = amp;   
332     if(amp < f2x2AmpOutOfPatchThres)
333       b=kTRUE;
334   }
335   else  if(iPatchType == 1){ //nxn case
336     fnxnAmpOutOfPatch = amp;   
337     if(amp < fnxnAmpOutOfPatchThres)
338       b=kTRUE;
339   }
340
341   return b;
342
343 }
344
345
346 //____________________________________________________________________________
347 void AliPHOSTrigger::MakeSlidingCell(const TClonesArray * amptrus, const TClonesArray * timeRtrus, const Int_t imod, TMatrixD *ampmax2, TMatrixD *ampmaxn){
348   //Sums energy of all possible 2x2 (L0) and nxn (L1) crystals per each TRU. 
349   //Fast signal in the experiment is given by 2x2 crystals, 
350   //for this reason we loop inside the TRU crystals by 2. 
351  
352   //Declare and initialize varibles
353   Float_t amp2 = 0 ;
354   Float_t ampn = 0 ; 
355   for(Int_t i = 0; i < 4; i++){
356     for(Int_t j = 0; j < fNTRU; j++){
357       (*ampmax2)(i,j) = -1;
358       (*ampmaxn)(i,j) = -1;
359     }
360   }
361
362   //Create matrix that will contain 2x2 amplitude sums
363   //used to calculate the nxn sums
364   TMatrixD  * tru2x2 = new TMatrixD(fNCrystalsPhi/2,fNCrystalsZ/2) ;
365   for(Int_t i = 0; i < fNCrystalsPhi/2; i++)
366     for(Int_t j = 0; j < fNCrystalsZ/2; j++)
367       (*tru2x2)(i,j) = -1.;
368     
369   //Loop over all TRUS in a module
370   for(Int_t itru = 0 + imod  * fNTRU ; itru < (imod+1)*fNTRU ; itru++){
371     TMatrixD * amptru   = dynamic_cast<TMatrixD *>(amptrus->At(itru)) ;
372     TMatrixD * timeRtru = dynamic_cast<TMatrixD *>(timeRtrus->At(itru)) ;
373     Int_t mtru = itru-imod*fNTRU ; //Number of TRU in Module
374     
375     //Sliding 2x2, add 2x2 amplitudes (NOT OVERLAP)
376     for(Int_t irow = 0 ; irow <  fNCrystalsPhi; irow += 2){ 
377       for(Int_t icol = 0 ; icol < fNCrystalsZ ; icol += 2){
378         amp2 = (*amptru)(irow,icol)+(*amptru)(irow+1,icol)+
379           (*amptru)(irow,icol+1)+(*amptru)(irow+1,icol+1);
380         //Fill new matrix with added 2x2 crystals for use in nxn sums
381         (*tru2x2)(irow/2,icol/2) = amp2 ;
382         //Select 2x2 maximum sums to select L0 
383         if(amp2 > (*ampmax2)(0,mtru)){
384           (*ampmax2)(0,mtru) = amp2 ; 
385           (*ampmax2)(1,mtru) = irow;
386           (*ampmax2)(2,mtru) = icol;
387         }
388       }
389     }
390
391     //Find most recent time in the selected 2x2 cell
392     (*ampmax2)(3,mtru) = 1 ;
393     Int_t row2 =  static_cast <Int_t> ((*ampmax2)(1,mtru));
394     Int_t col2 =  static_cast <Int_t> ((*ampmax2)(2,mtru));
395     for(Int_t i = 0; i<2; i++){
396       for(Int_t j = 0; j<2; j++){
397         if((*amptru)(row2+i,col2+j) > 0 &&  (*timeRtru)(row2+i,col2+j)> 0){       
398           if((*timeRtru)(row2+i,col2+j) <  (*ampmax2)(3,mtru)  )
399             (*ampmax2)(3,mtru) =  (*timeRtru)(row2+i,col2+j);
400         }
401       }
402     }
403
404     //Sliding nxn, add nxn amplitudes (OVERLAP)
405     if(fPatchSize > 0){
406       for(Int_t irow = 0 ; irow <  fNCrystalsPhi/2; irow++){ 
407         for(Int_t icol = 0 ; icol < fNCrystalsZ/2 ; icol++){
408           ampn = 0;
409           if( (irow+fPatchSize) < fNCrystalsPhi/2 && (icol+fPatchSize) < fNCrystalsZ/2){//Avoid exit the TRU
410             for(Int_t i = 0 ; i <= fPatchSize ; i++)
411               for(Int_t j = 0 ; j <= fPatchSize ; j++)
412                 ampn += (*tru2x2)(irow+i,icol+j);
413             //Select nxn maximum sums to select L1 
414             if(ampn > (*ampmaxn)(0,mtru)){
415               (*ampmaxn)(0,mtru) = ampn ; 
416               (*ampmaxn)(1,mtru) = irow*2;
417               (*ampmaxn)(2,mtru) = icol*2;
418             }
419           }
420         }
421       }
422       
423       //Find most recent time in selected nxn cell
424       (*ampmaxn)(3,mtru) = 1 ;
425       Int_t rown =  static_cast <Int_t> ((*ampmaxn)(1,mtru));
426       Int_t coln =  static_cast <Int_t> ((*ampmaxn)(2,mtru));
427       for(Int_t i = 0; i<4*fPatchSize; i++){
428         for(Int_t j = 0; j<4*fPatchSize; j++){
429           if( (rown+i) < fNCrystalsPhi && (coln+j) < fNCrystalsZ/2){//Avoid exit the TRU
430             if((*amptru)(rown+i,coln+j) > 0 &&  (*timeRtru)(rown+i,coln+j)> 0){
431               if((*timeRtru)(rown+i,coln+j) <  (*ampmaxn)(3,mtru)  )
432                 (*ampmaxn)(3,mtru) =  (*timeRtru)(rown+i,coln+j);
433             }
434           }
435         }
436       }
437     }
438     else {  
439         (*ampmaxn)(0,mtru) =  (*ampmax2)(0,mtru); 
440         (*ampmaxn)(1,mtru) =  (*ampmax2)(1,mtru);
441         (*ampmaxn)(2,mtru) =  (*ampmax2)(2,mtru);
442         (*ampmaxn)(3,mtru) =  (*ampmax2)(3,mtru);
443       }
444   }
445 }
446
447
448 //____________________________________________________________________________
449 void AliPHOSTrigger::Print(const Option_t * opt) const 
450 {
451
452   //Prints main parameters
453  
454   if(! opt)
455     return;
456   AliTriggerInput* in = 0x0 ;
457
458   printf( "             Maximum Amplitude after Sliding Crystal, \n") ; 
459   printf( "               -2x2 crystals sum (not overlapped): %10.2f, in Super Module %d\n",
460           f2x2MaxAmp,f2x2SM) ; 
461   printf( "               -2x2 from row %d to row %d and from column %d to column %d\n", f2x2CrystalPhi, f2x2CrystalPhi+2, f2x2CrystalEta, f2x2CrystalEta+2) ; 
462   printf( "               -2x2 Isolation Patch %d x %d, Amplitude out of 2x2 patch is %f, threshold %f, Isolated? %d \n", 
463           2*fIsolPatchSize+2, 2*fIsolPatchSize+2,  f2x2AmpOutOfPatch,  f2x2AmpOutOfPatchThres,static_cast<Int_t> (fIs2x2Isol)) ; 
464   if(fPatchSize > 0){
465     printf( "             Patch Size, n x n: %d x %d cells\n",2*(fPatchSize+1), 2*(fPatchSize+1));
466     printf( "               -nxn crystals sum (overlapped)    : %10.2f, in Super Module %d\n",
467             fnxnMaxAmp,fnxnSM) ; 
468     printf( "               -nxn from row %d to row %d and from column %d to column %d\n", fnxnCrystalPhi, fnxnCrystalPhi+4*fPatchSize, fnxnCrystalEta, fnxnCrystalEta+4*fPatchSize) ; 
469     printf( "               -nxn Isolation Patch %d x %d, Amplitude out of nxn patch is %f, threshold %f, Isolated? %d \n", 
470             4*fIsolPatchSize+2*(fPatchSize+1),4*fIsolPatchSize+2*(fPatchSize+1) ,  fnxnAmpOutOfPatch,  fnxnAmpOutOfPatchThres,static_cast<Int_t> (fIsnxnIsol) ) ; 
471   }
472
473   printf( "             Isolate in Module? %d\n",  
474           fIsolateInModule) ;  
475
476   printf( "             Threshold for LO %10.1f\n", 
477           fL0Threshold) ;  
478   
479   printf( "             Threshold for LO %10.2f\n", fL0Threshold) ;  
480   in = (AliTriggerInput*)fInputs.FindObject( "PHOS_L0" );
481   if(in->GetValue())
482     printf( "             *** PHOS LO is set ***\n") ; 
483   
484   printf( "             Jet Low Pt Threshold for L1 %10.2f\n", fL1JetLowPtThreshold) ;
485   in = (AliTriggerInput*)fInputs.FindObject( "PHOS_JetLPt_L1" );
486   if(in->GetValue())
487     printf( "             *** PHOS Jet Low Pt for L1 is set ***\n") ;
488   
489   printf( "             Jet Medium Pt Threshold for L1 %10.2f\n", fL1JetMediumPtThreshold) ;
490   in = (AliTriggerInput*)fInputs.FindObject( "PHOS_JetMPt_L1" );
491   if(in->GetValue())
492     printf( "             *** PHOS Jet Medium Pt for L1 is set ***\n") ;
493   
494   printf( "             Jet High Pt Threshold for L1 %10.2f\n", fL1JetHighPtThreshold) ;  
495   in = (AliTriggerInput*) fInputs.FindObject( "PHOS_JetHPt_L1" );
496   if(in->GetValue())
497     printf( "              *** PHOS Jet High Pt for L1 is set ***\n") ;
498   
499 }
500
501 //____________________________________________________________________________
502 void AliPHOSTrigger::SetTriggers(const TClonesArray * ampmatrix, const Int_t iMod, const TMatrixD * ampmax2, const TMatrixD * ampmaxn)  
503 {
504   //Checks the 2x2 and nxn maximum amplitude per each TRU and compares 
505   //with the different L0 and L1 triggers thresholds. It finds if maximum amplitudes are isolated.
506
507   //Initialize variables
508   Float_t max2[] = {-1,-1,-1,-1} ;
509   Float_t maxn[] = {-1,-1,-1,-1} ;
510   Int_t   mtru2  = -1 ;
511   Int_t   mtrun  = -1 ;
512
513
514   //Find maximum summed amplitude of all the TRU 
515   //in a Module
516   for(Int_t i = 0 ; i < fNTRU ; i++){
517     if(max2[0] < (*ampmax2)(0,i) ){
518       max2[0] =  (*ampmax2)(0,i) ; // 2x2 summed max amplitude
519       max2[1] =  (*ampmax2)(1,i) ; // corresponding phi position in TRU
520       max2[2] =  (*ampmax2)(2,i) ; // corresponding eta position in TRU
521       max2[3] =  (*ampmax2)(3,i) ; // corresponding most recent time
522       mtru2   = i ; // TRU number in module
523     }
524     if(maxn[0] < (*ampmaxn)(0,i) ){
525       maxn[0] =  (*ampmaxn)(0,i) ; // nxn summed max amplitude
526       maxn[1] =  (*ampmaxn)(1,i) ; // corresponding phi position in TRU
527       maxn[2] =  (*ampmaxn)(2,i) ; // corresponding eta position in TRU
528       maxn[3] =  (*ampmaxn)(3,i) ; // corresponding most recent time
529       mtrun   = i ; // TRU number in module
530     }
531   }
532   
533   //Set max amplitude if larger than in other Modules
534   Float_t maxtimeR2 = -1 ;
535   Float_t maxtimeRn = -1 ;
536   // Create a shaper pulse object
537   AliPHOSPulseGenerator *pulse = new AliPHOSPulseGenerator();
538   Int_t nTimeBins = pulse->GetRawFormatTimeBins() ;
539  
540   //Set max 2x2 amplitude and select L0 trigger
541   if(max2[0] > f2x2MaxAmp ){
542     f2x2MaxAmp  = max2[0] ;
543     f2x2SM      = iMod ;
544     maxtimeR2   = max2[3] ;
545     GetCrystalPhiEtaIndexInModuleFromTRUIndex(mtru2,
546                                               static_cast<Int_t>(max2[1]),
547                                               static_cast<Int_t>(max2[2]),
548                                               f2x2CrystalPhi,f2x2CrystalEta) ;
549     
550     //Isolated patch?
551     if(fIsolateInModule)
552       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iMod, mtru2,  f2x2MaxAmp, f2x2CrystalPhi,f2x2CrystalEta) ;
553     else
554       fIs2x2Isol =  IsPatchIsolated(0, ampmatrix, iMod, mtru2,  f2x2MaxAmp,  static_cast<Int_t>(max2[1]), static_cast<Int_t>(max2[2])) ;
555
556     //Transform digit amplitude in Raw Samples
557     fADCValuesLow2x2  = new Int_t[nTimeBins];
558     fADCValuesHigh2x2 = new Int_t[nTimeBins];
559     
560     pulse->SetAmplitude(f2x2MaxAmp);
561     pulse->SetTZero(maxtimeR2);
562     pulse->MakeSamples();
563     pulse->GetSamples(fADCValuesHigh2x2, fADCValuesLow2x2) ; 
564     
565     //Set Trigger Inputs, compare ADC time bins until threshold is attained
566     //Set L0
567     for(Int_t i = 0 ; i < nTimeBins ; i++){
568       if(fADCValuesHigh2x2[i] >= fL0Threshold || fADCValuesLow2x2[i] >= fL0Threshold) {
569         SetInput("PHOS_L0") ;
570         break;
571       }
572     }
573   }
574
575   //Set max nxn amplitude and select L1 triggers
576   if(maxn[0] > fnxnMaxAmp  && fPatchSize > 0){
577     fnxnMaxAmp  = maxn[0] ;
578     fnxnSM      = iMod ;
579     maxtimeRn   = maxn[3] ;
580     GetCrystalPhiEtaIndexInModuleFromTRUIndex(mtrun,
581                                               static_cast<Int_t>(maxn[1]),
582                                               static_cast<Int_t>(maxn[2]),
583                                               fnxnCrystalPhi,fnxnCrystalEta) ; 
584     
585     //Isolated patch?
586     if(fIsolateInModule)
587       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iMod, mtrun,  fnxnMaxAmp, fnxnCrystalPhi, fnxnCrystalEta) ;
588     else
589       fIsnxnIsol =  IsPatchIsolated(1, ampmatrix, iMod, mtrun,  fnxnMaxAmp,  static_cast<Int_t>(maxn[1]), static_cast<Int_t>(maxn[2])) ;
590
591     //Transform digit amplitude in Raw Samples
592     fADCValuesHighnxn = new Int_t[nTimeBins];
593     fADCValuesLownxn  = new Int_t[nTimeBins];
594
595     pulse->SetAmplitude(maxtimeRn);
596     pulse->SetTZero(fnxnMaxAmp);
597     pulse->MakeSamples();
598     pulse->GetSamples(fADCValuesHighnxn, fADCValuesLownxn) ;
599     
600     //Set Trigger Inputs, compare ADC time bins until threshold is attained
601     //SetL1 Low
602     for(Int_t i = 0 ; i < nTimeBins ; i++){
603       if(fADCValuesHighnxn[i] >= fL1JetLowPtThreshold  || fADCValuesLownxn[i] >= fL1JetLowPtThreshold){
604         SetInput("PHOS_JetLPt_L1") ;
605         break; 
606       }
607     }
608     //SetL1 Medium
609     for(Int_t i = 0 ; i < nTimeBins ; i++){
610       if(fADCValuesHighnxn[i] >= fL1JetMediumPtThreshold  || fADCValuesLownxn[i] >= fL1JetMediumPtThreshold){
611         SetInput("PHOS_JetMPt_L1") ;
612         break; 
613       }
614     }
615     //SetL1 High
616     for(Int_t i = 0 ; i < nTimeBins ; i++){
617       if(fADCValuesHighnxn[i] >= fL1JetHighPtThreshold || fADCValuesLownxn[i] >= fL1JetHighPtThreshold){
618         SetInput("PHOS_JetHPt_L1") ;
619         break;
620       }
621     }
622   }
623 }
624
625 //____________________________________________________________________________
626 void AliPHOSTrigger::Trigger() 
627 {
628
629   //Main Method to select triggers.
630   AliRunLoader *rl = gAlice->GetRunLoader();
631   //Getter
632   AliPHOSGetter * gime = AliPHOSGetter::Instance( rl->GetFileName() ) ;
633   //AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
634
635   //Get Geometry
636   const AliPHOSGeometry * geom = AliPHOSGetter::Instance()->PHOSGeometry() ;
637    
638   //Define parameters
639   Int_t nModules     = geom->GetNModules();
640   fNCrystalsPhi = geom->GetNPhi()/fNTRUPhi ;// 64/4=16
641   fNCrystalsZ   = geom->GetNZ()/fNTRUZ ;// 56/2=28
642
643   //Intialize data members each time the trigger is called in event loop
644   f2x2MaxAmp = -1; f2x2CrystalPhi = -1;  f2x2CrystalEta = -1;
645   fnxnMaxAmp = -1; fnxnCrystalPhi = -1;  fnxnCrystalEta = -1;
646
647   //Take the digits list if simulation
648   if(fSimulation)
649     fDigitsList = gime->Digits() ;
650   
651   if(!fDigitsList)
652     AliFatal("Digits not found !") ;
653   
654   //Fill TRU Matrix  
655   TClonesArray * amptrus   = new TClonesArray("TMatrixD",1000);
656   TClonesArray * ampmods   = new TClonesArray("TMatrixD",1000);
657   TClonesArray * timeRtrus = new TClonesArray("TMatrixD",1000);
658   FillTRU(fDigitsList,geom,amptrus, ampmods,timeRtrus) ;
659
660   //Do Crystal Sliding and select Trigger
661   //Initialize varible that will contain maximum amplitudes and 
662   //its corresponding cell position in eta and phi, and time.
663   TMatrixD  * ampmax2 = new TMatrixD(4,fNTRU) ;
664   TMatrixD  * ampmaxn = new TMatrixD(4,fNTRU) ;
665
666   for(Int_t imod = 0 ; imod < nModules ; imod++) {
667
668     //Do 2x2 and nxn sums, select maximums. 
669     MakeSlidingCell(amptrus, timeRtrus, imod, ampmax2, ampmaxn);
670     //Set the trigger
671     if(fIsolateInModule)
672       SetTriggers(ampmods,imod,ampmax2,ampmaxn) ;
673     if(!fIsolateInModule)
674       SetTriggers(amptrus,imod,ampmax2,ampmaxn) ;
675   }
676
677   //Print();
678
679 }