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