]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALTrigger.cxx
Inserting TMath.h where required by the new version of ROOT
[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 /* $Log $ */
17
18 //_________________________________________________________________________  
19 //
20 //  Class for trigger analysis.
21 //  Digits are grouped in TRU's (384 cells ordered fNTRUPhi x fNTRUEta). 
22 //  The algorithm searches all possible 2x2 and nxn (n is a multiple of 4) cell 
23 //  combinations per each TRU,  adding the digits amplitude and finding the 
24 //  maximum. Maxima are compared to triggers threshold and they are set. 
25 //  Thresholds need to be fixed. Last 2 modules are half size in Phi, I considered 
26 //  that the number of TRU is maintained for the last modules but decision not taken. 
27 //  If different, then this must be changed. 
28 //  Usage:
29 //
30 //  //Inside the event loop
31 //  AliEMCALTrigger *tr = new AliEMCALTrigger();//Init Trigger
32 //  tr->SetL0Threshold(100); //Arbitrary threshold values
33 //  tr->SetL1JetLowPtThreshold(1000);
34 //  tr->SetL1JetMediumPtThreshold(10000);
35 //  tr->SetL1JetHighPtThreshold(20000);
36 //  tr->Trigger(); //Execute Trigger
37 //  tr->Print(""); //Print results
38 //
39 //*-- Author: Gustavo Conesa & Yves Schutz (IFIC, CERN) 
40 //////////////////////////////////////////////////////////////////////////////
41
42
43 // --- ROOT system ---
44 //#include "TMatrixD.h"
45
46 // --- ALIROOT system ---
47
48 #include "AliRun.h" 
49 #include "AliRunLoader.h" 
50 #include "AliTriggerInput.h"
51 #include "AliEMCAL.h" 
52 #include "AliEMCALLoader.h" 
53 #include "AliEMCALDigit.h"
54 #include "AliEMCALTrigger.h" 
55 #include "AliEMCALGeometry.h"
56
57 ClassImp(AliEMCALTrigger)
58
59 //______________________________________________________________________
60 AliEMCALTrigger::AliEMCALTrigger()
61   : AliTriggerDetector(),
62     f2x2MaxAmp(-1), f2x2CellPhi(-1),  f2x2CellEta(-1),
63     f2x2SM(0),
64     fnxnMaxAmp(-1), fnxnCellPhi(-1),  fnxnCellEta(-1),
65     fnxnSM(0),
66     fADCValuesHighnxn(0x0),fADCValuesLownxn(0x0),
67     fADCValuesHigh2x2(0x0),fADCValuesLow2x2(0x0),
68     fDigitsList(0x0),
69     fL0Threshold(100),fL1JetLowPtThreshold(200),
70     fL1JetMediumPtThreshold(500), fL1JetHighPtThreshold(1000),
71     fPatchSize(1), fSimulation(kTRUE)
72 {
73   //ctor 
74
75   SetName("EMCAL");
76   CreateInputs();
77    
78    //Print("") ; 
79 }
80
81
82
83 //____________________________________________________________________________
84 AliEMCALTrigger::AliEMCALTrigger(const AliEMCALTrigger & trig) 
85   : AliTriggerDetector(trig),
86     f2x2MaxAmp(trig.f2x2MaxAmp), 
87     f2x2CellPhi(trig.f2x2CellPhi),  
88     f2x2CellEta(trig.f2x2CellEta),
89     f2x2SM(trig.f2x2SM),
90     fnxnMaxAmp(trig.fnxnMaxAmp), 
91     fnxnCellPhi(trig.fnxnCellPhi),  
92     fnxnCellEta(trig.fnxnCellEta),
93     fnxnSM(trig.fnxnSM),
94     fADCValuesHighnxn(trig.fADCValuesHighnxn),
95     fADCValuesLownxn(trig.fADCValuesLownxn),
96     fADCValuesHigh2x2(trig.fADCValuesHigh2x2),
97     fADCValuesLow2x2(trig.fADCValuesLow2x2),
98     fDigitsList(trig.fDigitsList),
99     fL0Threshold(trig.fL0Threshold),
100     fL1JetLowPtThreshold(trig.fL1JetLowPtThreshold),
101     fL1JetMediumPtThreshold(trig.fL1JetMediumPtThreshold), 
102     fL1JetHighPtThreshold(trig.fL1JetHighPtThreshold),
103     fPatchSize(trig.fPatchSize), fSimulation(trig.fSimulation)
104 {
105   // cpy ctor
106 }
107
108 //----------------------------------------------------------------------
109 void AliEMCALTrigger::CreateInputs()
110 {
111    // inputs 
112    
113    // Do not create inputs again!!
114    if( fInputs.GetEntriesFast() > 0 ) return;
115    
116    fInputs.AddLast( new AliTriggerInput( "EMCAL_L0",       "EMCAL L0", 0x02 ) );
117    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetHPt_L1","EMCAL Jet High Pt L1",   0x04 ) );
118    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetMPt_L1","EMCAL Jet Medium Pt L1", 0x08 ) );
119    fInputs.AddLast( new AliTriggerInput( "EMCAL_JetLPt_L1","EMCAL Jet Low Pt L1",    0x016 ) );
120  
121 }
122
123 //____________________________________________________________________________
124 void AliEMCALTrigger::MakeSlidingCell(const TClonesArray * amptrus, const TClonesArray * timeRtrus, const Int_t isupermod,TMatrixD *ampmax2, TMatrixD *ampmaxn, AliEMCALGeometry *geom){
125   
126   //Sums energy of all possible 2x2 (L0) and nxn (L1) cells per each TRU. 
127   //Fast signal in the experiment is given by 2x2 cells, 
128   //for this reason we loop inside the TRU cells by 2. 
129
130   //Declare and initialize variables
131   Int_t nCellsPhi  = geom->GetNPhi()*2/geom->GetNTRUPhi() ;
132   if(isupermod > 9)
133     nCellsPhi =  nCellsPhi / 2 ; //Half size SM. Not Final.
134   // 12(tow)*2(cell)/1 TRU, cells in Phi in one TRU
135   Int_t nCellsEta  = geom->GetNEta()*2/geom->GetNTRUEta() ;
136   // 24(mod)*2(tower)/3 TRU, cells in Eta in one TRU
137   Int_t nTRU          = geom->GetNTRU();//3 TRU per super module
138
139   Float_t amp2 = 0 ;
140   Float_t ampn = 0 ; 
141   for(Int_t i = 0; i < 4; i++){
142     for(Int_t j = 0; j < nTRU; j++){
143       (*ampmax2)(i,j) = -1;
144       (*ampmaxn)(i,j) = -1;
145     }
146   }
147
148   //Create matrix that will contain 2x2 amplitude sums
149   //used to calculate the nxn sums
150   TMatrixD  * tru2x2 = new TMatrixD(nCellsPhi/2,nCellsEta/2) ;
151   for(Int_t i = 0; i < nCellsPhi/2; i++)
152     for(Int_t j = 0; j < nCellsEta/2; j++)
153       (*tru2x2)(i,j) = -1;
154   
155   //Loop over all TRUS in a supermodule
156   for(Int_t itru = 0 + isupermod * nTRU ; itru < (isupermod+1)*nTRU ; itru++) {
157     TMatrixD * amptru   = dynamic_cast<TMatrixD *>(amptrus->At(itru)) ;
158     TMatrixD * timeRtru = dynamic_cast<TMatrixD *>(timeRtrus->At(itru)) ;
159     Int_t mtru = itru-isupermod*nTRU ; //Number of TRU in Supermodule
160    
161     //Sliding 2x2, add 2x2 amplitudes (NOT OVERLAP)
162     for(Int_t irow = 0 ; irow <  nCellsPhi; irow += 2){ 
163       for(Int_t icol = 0 ; icol < nCellsEta ; icol += 2){
164         amp2 = (*amptru)(irow,icol)+(*amptru)(irow+1,icol)+
165           (*amptru)(irow,icol+1)+(*amptru)(irow+1,icol+1);
166
167         //Fill matrix with added 2x2 crystals for use in nxn sums
168         (*tru2x2)(irow/2,icol/2) = amp2 ;
169         //Select 2x2 maximum sums to select L0 
170         if(amp2 > (*ampmax2)(0,mtru)){
171           (*ampmax2)(0,mtru) = amp2 ; 
172           (*ampmax2)(1,mtru) = irow;
173           (*ampmax2)(2,mtru) = icol;
174         }
175       }
176     }
177     
178     //Find most recent time in the selected 2x2 cell
179     (*ampmax2)(3,mtru) = 1 ;
180     Int_t row2 =  static_cast <Int_t> ((*ampmax2)(1,mtru));
181     Int_t col2 =  static_cast <Int_t> ((*ampmax2)(2,mtru));
182     for(Int_t i = 0; i<2; i++){
183       for(Int_t j = 0; j<2; j++){
184         if((*amptru)(row2+i,col2+j) > 0 &&  (*timeRtru)(row2+i,col2+j)> 0){       
185           if((*timeRtru)(row2+i,col2+j) <  (*ampmax2)(3,mtru)  )
186             (*ampmax2)(3,mtru) =  (*timeRtru)(row2+i,col2+j);
187         }
188       }
189     }
190
191     //Sliding nxn, add nxn amplitudes  (OVERLAP)
192     if(fPatchSize > 0){
193       for(Int_t irow = 0 ; irow <  nCellsPhi/2; irow++){ 
194         for(Int_t icol = 0 ; icol < nCellsEta/2 ; icol++){
195           ampn = 0;
196           if( (irow+fPatchSize) < nCellsPhi/2 && (icol+fPatchSize) < nCellsEta/2){//Avoid exit the TRU
197             for(Int_t i = 0 ; i <= fPatchSize ; i++)
198               for(Int_t j = 0 ; j <= fPatchSize ; j++)
199                 ampn += (*tru2x2)(irow+i,icol+j);
200             //Select nxn maximum sums to select L1 
201             if(ampn > (*ampmaxn)(0,mtru)){
202               (*ampmaxn)(0,mtru) = ampn ; 
203               (*ampmaxn)(1,mtru) = irow*2;
204               (*ampmaxn)(2,mtru) = icol*2;
205             }
206           }
207         }
208       }
209       
210       //Find most recent time in selected nxn cell
211       (*ampmaxn)(3,mtru) = 1 ;
212       Int_t rown =  static_cast <Int_t> ((*ampmaxn)(1,mtru));
213       Int_t coln =  static_cast <Int_t> ((*ampmaxn)(2,mtru));
214       for(Int_t i = 0; i<4*fPatchSize; i++){
215         for(Int_t j = 0; j<4*fPatchSize; j++){
216           if( (rown+i) < nCellsPhi && (coln+j) < nCellsEta/2){//Avoid exit the TRU
217             if((*amptru)(rown+i,coln+j) > 0 &&  (*timeRtru)(rown+i,coln+j)> 0){
218               if((*timeRtru)(rown+i,coln+j) <  (*ampmaxn)(3,mtru)  )
219                 (*ampmaxn)(3,mtru) =  (*timeRtru)(rown+i,coln+j);
220             }
221           }
222         }
223       }
224     }
225     else {  
226         (*ampmaxn)(0,mtru) =  (*ampmax2)(0,mtru); 
227         (*ampmaxn)(1,mtru) =  (*ampmax2)(1,mtru);
228         (*ampmaxn)(2,mtru) =  (*ampmax2)(2,mtru);
229         (*ampmaxn)(3,mtru) =  (*ampmax2)(3,mtru);
230       }
231   }
232 }
233
234 //____________________________________________________________________________
235 void AliEMCALTrigger::Print(const Option_t * opt) const 
236 {
237   
238   //Prints main parameters
239   
240   if(! opt)
241     return;
242   AliTriggerInput* in = 0x0 ;
243
244   printf( "             Maximum Amplitude after Sliding Cell, \n") ; 
245   printf( "               -2x2 cells sum (not overlapped): %10.2f, in Super Module %d\n",
246           f2x2MaxAmp,f2x2SM) ; 
247   printf( "               -2x2 from row %d to row %d and from column %d to column %d\n", f2x2CellPhi, f2x2CellPhi+2, f2x2CellEta, f2x2CellEta+2) ; 
248  
249   if(fPatchSize > 0){
250     printf( "             Patch Size, n x n: %d x %d cells\n",4*fPatchSize, 4*fPatchSize);
251     printf( "               -nxn cells sum (overlapped)    : %10.2f, in Super Module %d\n",
252             fnxnMaxAmp,fnxnSM) ; 
253     printf( "               -nxn from row %d to row %d and from column %d to column %d\n", fnxnCellPhi, fnxnCellPhi+4*fPatchSize, fnxnCellEta, fnxnCellEta+4*fPatchSize) ; 
254   }
255  
256   printf( "             Threshold for LO %10.2f\n", 
257           fL0Threshold) ;  
258   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_L0" );
259   if(in->GetValue())
260     printf( "             *** EMCAL LO is set ***\n") ; 
261   
262   printf( "             Jet Low Pt Threshold for L1 %10.2f\n", 
263           fL1JetLowPtThreshold) ;
264   in = (AliTriggerInput*)fInputs.FindObject( "EMCAL_JetLPt_L1" );
265   if(in->GetValue())
266     printf( "             *** EMCAL Jet Low Pt for L1 is set ***\n") ;
267
268   printf( "             Jet Medium Pt Threshold for L1 %10.2f\n", 
269           fL1JetMediumPtThreshold) ;  
270   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_JetMPt_L1" );
271   if(in->GetValue())
272     printf( "             *** EMCAL Jet Medium Pt for L1 is set ***\n") ;
273
274   printf( "             Jet High Pt Threshold for L1 %10.2f\n", 
275           fL1JetHighPtThreshold) ;  
276   in = (AliTriggerInput*) fInputs.FindObject( "EMCAL_JetHPt_L1" );
277   if(in->GetValue())
278     printf( "             *** EMCAL Jet High Pt for L1 is set ***\n") ;
279
280 }
281
282 //____________________________________________________________________________
283 void AliEMCALTrigger::SetTriggers(const Int_t iSM, const TMatrixD *ampmax2, 
284                                   const TMatrixD *ampmaxn, AliEMCALGeometry *geom)  
285 {
286
287   //Checks the 2x2 and nxn maximum amplitude per each TRU and 
288   //compares with the different L0 and L1 triggers thresholds
289   Float_t max2[] = {-1,-1,-1,-1} ;
290   Float_t maxn[] = {-1,-1,-1,-1} ;
291   Int_t   itru2  = -1 ;
292   Int_t   itrun  = -1 ;
293
294   //Find maximum summed amplitude of all the TRU 
295   //in a Super Module
296     for(Int_t i = 0 ; i < geom->GetNTRU() ; i++){
297       if(max2[0] < (*ampmax2)(0,i) ){
298         max2[0] =  (*ampmax2)(0,i) ; // 2x2 summed max amplitude
299         max2[1] =  (*ampmax2)(1,i) ; // corresponding phi position in TRU
300         max2[2] =  (*ampmax2)(2,i) ; // corresponding eta position in TRU
301         max2[3] =  (*ampmax2)(3,i) ; // corresponding most recent time
302         itru2   = i ;
303       }
304       if(maxn[0] < (*ampmaxn)(0,i) ){
305         maxn[0] =  (*ampmaxn)(0,i) ; // nxn summed max amplitude
306         maxn[1] =  (*ampmaxn)(1,i) ; // corresponding phi position in TRU
307         maxn[2] =  (*ampmaxn)(2,i) ; // corresponding eta position in TRU
308         maxn[3] =  (*ampmaxn)(3,i) ; // corresponding most recent time
309         itrun   = i ;
310       }
311     }
312
313   //--------Set max amplitude if larger than in other Super Modules------------
314   Float_t maxtimeR2 = -1 ;
315   Float_t maxtimeRn = -1 ;
316   AliRunLoader *rl  = AliRunLoader::GetRunLoader();
317   AliRun * gAlice   = rl->GetAliRun(); 
318   AliEMCAL * emcal  = (AliEMCAL*)gAlice->GetDetector("EMCAL");
319   Int_t nTimeBins = emcal->GetRawFormatTimeBins() ;
320
321   //Set max of 2x2 amplitudes and select L0 trigger
322   if(max2[0] > f2x2MaxAmp ){
323     f2x2MaxAmp  = max2[0] ;
324     f2x2SM      = iSM ;
325     maxtimeR2   = max2[3] ;
326     geom->GetCellPhiEtaIndexInSModuleFromTRUIndex(itru2, 
327                                                   static_cast<Int_t>(max2[1]),
328                                                   static_cast<Int_t>(max2[2]),
329                                                   f2x2CellPhi,f2x2CellEta) ;
330     
331     //Transform digit amplitude in Raw Samples
332     fADCValuesLow2x2  = new Int_t[nTimeBins];
333     fADCValuesHigh2x2 = new Int_t[nTimeBins];
334     emcal->RawSampledResponse(maxtimeR2, f2x2MaxAmp, fADCValuesHigh2x2, fADCValuesLow2x2) ; 
335     
336     //Set Trigger Inputs, compare ADC time bins until threshold is attained
337     //Set L0
338     for(Int_t i = 0 ; i < nTimeBins ; i++){
339       if(fADCValuesHigh2x2[i] >= fL0Threshold          || fADCValuesLow2x2[i] >= fL0Threshold){
340         SetInput("EMCAL_L0") ;
341         break;
342       }
343     }
344   }
345   
346   //------------Set max of nxn amplitudes and select L1 trigger---------
347   if(maxn[0] > fnxnMaxAmp ){
348     fnxnMaxAmp  = maxn[0] ;
349     fnxnSM      = iSM ;
350     maxtimeRn   = maxn[3] ;
351     geom->GetCellPhiEtaIndexInSModuleFromTRUIndex(itrun, 
352                                                   static_cast<Int_t>(maxn[1]),
353                                                   static_cast<Int_t>(maxn[2]),
354                                                   fnxnCellPhi,fnxnCellEta) ; 
355     //Transform digit amplitude in Raw Samples
356     fADCValuesHighnxn = new Int_t[nTimeBins];
357     fADCValuesLownxn  = new Int_t[nTimeBins];
358     emcal->RawSampledResponse(maxtimeRn, fnxnMaxAmp, fADCValuesHighnxn, fADCValuesLownxn) ;
359     
360     //Set Trigger Inputs, compare ADC time bins until threshold is attained
361     //SetL1 Low
362     for(Int_t i = 0 ; i < nTimeBins ; i++){
363       if(fADCValuesHighnxn[i] >= fL1JetLowPtThreshold  || fADCValuesLownxn[i] >= fL1JetLowPtThreshold){
364         SetInput("EMCAL_JetLPt_L1") ;
365         break; 
366       }
367     }
368     
369     //SetL1 Medium
370     for(Int_t i = 0 ; i < nTimeBins ; i++){
371       if(fADCValuesHighnxn[i] >= fL1JetMediumPtThreshold || fADCValuesLownxn[i] >= fL1JetMediumPtThreshold){
372         SetInput("EMCAL_JetMPt_L1") ;
373         break;
374       }
375     }
376     
377     //SetL1 High
378     for(Int_t i = 0 ; i < nTimeBins ; i++){
379       if(fADCValuesHighnxn[i] >= fL1JetHighPtThreshold || fADCValuesLownxn[i] >= fL1JetHighPtThreshold){
380         SetInput("EMCAL_JetHPt_L1") ;
381         break;
382       }
383     }
384   } 
385 }
386
387 //____________________________________________________________________________
388 void AliEMCALTrigger::Trigger() 
389 {
390   //Main Method to select triggers.
391   AliRunLoader *rl = AliRunLoader::GetRunLoader();
392   AliEMCALLoader *emcalLoader = dynamic_cast<AliEMCALLoader*>
393     (rl->GetDetectorLoader("EMCAL"));
394
395   //Load EMCAL Geometry
396   rl->LoadgAlice(); 
397   AliRun * gAlice = rl->GetAliRun(); 
398   AliEMCAL * emcal  = (AliEMCAL*)gAlice->GetDetector("EMCAL");
399   AliEMCALGeometry * geom = emcal->GetGeometry();
400   
401   if (geom==0)
402     AliFatal("Did not get geometry from EMCALLoader");
403
404
405   //Define parameters
406   Int_t nSuperModules = geom->GetNumberOfSuperModules() ; //12 SM in EMCAL
407   Int_t nTRU          = geom->GetNTRU();//3 TRU per super module
408
409   //Intialize data members each time the trigger is called in event loop
410   f2x2MaxAmp = -1; f2x2CellPhi = -1;  f2x2CellEta = -1;
411   fnxnMaxAmp = -1; fnxnCellPhi = -1;  fnxnCellEta = -1;
412
413   //Take the digits list if simulation
414   if(fSimulation){
415     rl->LoadDigits("EMCAL");
416     fDigitsList = emcalLoader->Digits() ;
417   }
418   if(!fDigitsList)
419     AliFatal("Digits not found !") ;
420   
421   //Take the digits list 
422   
423   //Fill TRU Matrix  
424   TClonesArray * amptrus   = new TClonesArray("TMatrixD",1000);
425   TClonesArray * timeRtrus = new TClonesArray("TMatrixD",1000);
426
427   geom->FillTRU(fDigitsList, amptrus, timeRtrus) ;
428
429   //Do Cell Sliding and select Trigger
430   //Initialize varible that will contain maximum amplitudes and 
431   //its corresponding cell position in eta and phi, and time.
432   TMatrixD  * ampmax2 = new TMatrixD(4,nTRU) ;
433   TMatrixD  * ampmaxn = new TMatrixD(4,nTRU) ;
434
435   for(Int_t iSM = 0 ; iSM < nSuperModules ; iSM++) {
436     //Do 2x2 and nxn sums, select maximums. 
437     MakeSlidingCell(amptrus, timeRtrus, iSM, ampmax2, ampmaxn, geom);
438   
439     //Set the trigger
440     SetTriggers(iSM, ampmax2, ampmaxn, geom) ;
441   }
442 }