]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONLocalTriggerBoard.cxx
tofLabel copying
[u/mrichter/AliRoot.git] / MUON / AliMUONLocalTriggerBoard.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
16 /* $Id$ */
17
18 //-----------------------------------------------------------------------------
19 /// \class AliMUONLocalTriggerBoard
20 /// A local trigger board has as input a bit pattern and returns 
21 /// the local trigger response after comparison w/ a LUT
22 /// \todo Change member functions comments in capital letters to normal text
23 ///
24 /// \author Rachid Guernane (LPCCFd)
25 //-----------------------------------------------------------------------------
26
27 #include <cstdlib>
28 #include "AliMUONLocalTriggerBoard.h"
29 #include "AliMUONTriggerLut.h"
30
31 #include "AliLog.h"
32
33 #include <TBits.h>
34 #include <Riostream.h>
35
36 using std::endl;
37 using std::cout;
38 /// \cond CLASSIMP
39 ClassImp(AliMUONLocalTriggerBoard)
40 /// \endcond
41
42 const Int_t AliMUONLocalTriggerBoard::fgkCircuitId[234] = 
43 {
44   111,  121,  131,  141,  151,  161,  171,
45   211,  212,  221,  222,  231,  232,  241,  242,  251,  252,  261,  262,  271,
46   311,  312,  321,  322,  331,  332,  341,  342,  351,  352,  361,  362,  371,
47   411,  412,  413,  421,  422,  423,  424,  431,  432,  433,  434,  441,  442,  451,  452,  461,  462,  471,
48   521,  522,  523,  524,  531,  532,  533,  534,  541,  542,  551,  552,  561,  562,  571, 
49   611,  612,  613,  621,  622,  623,  624,  631,  632,  633,  634,  641,  642,  651,  652,  661,  662,  671,
50   711,  712,  721,  722,  731,  732,  741,  742,  751,  752,  761,  762,  771,
51   811,  812,  821,  822,  831,  832,  841,  842,  851,  852,  861,  862,  871,
52   911,  921,  931,  941,  951,  961,  971,
53   -111, -121, -131, -141, -151, -161, -171,
54   -211, -212, -221, -222, -231, -232, -241, -242, -251, -252, -261, -262, -271,
55   -311, -312, -321, -322, -331, -332, -341, -342, -351, -352, -361, -362, -371,
56   -411, -412, -413, -421, -422, -423, -424, -431, -432, -433, -434, -441, -442, -451, -452, -461, -462, -471,
57   -521, -522, -523, -524, -531, -532, -533, -534, -541, -542, -551, -552, -561, -562, -571, 
58   -611, -612, -613, -621, -622, -623, -624, -631, -632, -633, -634, -641, -642, -651, -652, -661, -662, -671,
59   -711, -712, -721, -722, -731, -732, -741, -742, -751, -752, -761, -762, -771,
60   -811, -812, -821, -822, -831, -832, -841, -842, -851, -852, -861, -862, -871,
61   -911, -921, -931, -941, -951, -961, -971 
62 };
63
64 //___________________________________________
65 AliMUONLocalTriggerBoard::AliMUONLocalTriggerBoard()
66     : AliMUONTriggerBoard(),
67       fMpLocalBoard(0x0),
68       fStripX11(0),
69       fStripY11(15),
70       fDev(0),
71       fTrigY(1),
72       fOutput(0),
73       fLUT(0x0),
74       fCoinc44(0)
75 {
76 /// default constructor
77 ///
78    
79    for (Int_t i=0; i<2; i++) 
80       for (Int_t j=0; j<4; j++) 
81       {
82          fXY[i][j] = fXYU[i][j] = fXYD[i][j] = 0;
83
84          fMask[i][j] = 0xFFFF;
85       }
86
87    for (Int_t i=0; i<5; i++) fMinDevStrip[i] = fMinDev[i] = fCoordY[i] = 0;
88
89    for (Int_t i=0; i<2; i++) fLutLpt[i] = fLutHpt[i] = 0;
90 }
91
92 //___________________________________________
93 AliMUONLocalTriggerBoard::AliMUONLocalTriggerBoard(AliMpLocalBoard* mpLocalBoard)
94     : AliMUONTriggerBoard(mpLocalBoard->GetName(), mpLocalBoard->GetSlot()),
95       fMpLocalBoard(mpLocalBoard),
96       fStripX11(0),
97       fStripY11(15),
98       fDev(0),
99       fTrigY(1),
100       fOutput(0),
101       fLUT(0x0),
102       fCoinc44(0)
103 {
104 /// Standard constructor
105 ///
106    
107    for (Int_t i=0; i<2; i++) 
108       for (Int_t j=0; j<4; j++) 
109       {
110          fXY[i][j] = fXYU[i][j] = fXYD[i][j] = 0;
111
112          fMask[i][j] = 0xFFFF;
113       }
114
115    for (Int_t i=0; i<5; i++) fMinDevStrip[i] = fMinDev[i] = fCoordY[i] = 0;
116
117    for (Int_t i=0; i<2; i++) fLutLpt[i] = fLutHpt[i] = 0;
118 }
119
120 //___________________________________________
121 AliMUONLocalTriggerBoard::AliMUONLocalTriggerBoard(const AliMUONLocalTriggerBoard& right) :
122   AliMUONTriggerBoard(right),
123   fMpLocalBoard(right.fMpLocalBoard),
124   fStripX11(right.fStripX11),
125   fStripY11(right.fStripY11),
126   fDev(right.fDev),
127   fTrigY(right.fTrigY),
128   fOutput(right.fOutput),
129   fLUT(right.fLUT),
130   fCoinc44(right.fCoinc44)
131 {
132   //
133   /// Copy constructor
134   //
135   for (Int_t i=0; i<2; i++) {
136     for (Int_t j=0; j<4; j++) {
137       fXY[i][j]  = right.fXY[i][j];
138       fXYU[i][j] = right.fXYU[i][j];
139       fXYD[i][j] = right.fXYD[i][j];
140
141       fMask[i][j] = right.fMask[i][j];
142     }
143   }
144
145   for (Int_t i=0; i<2; i++) {
146     fLutLpt[i] = right.fLutLpt[i];
147     fLutHpt[i] = right.fLutHpt[i];
148   }
149
150   for (Int_t i=0; i<5; i++) {
151     fMinDevStrip[i] = right.fMinDevStrip[i];
152     fMinDev[i] = right.fMinDev[i];
153     fCoordY[i] = right.fCoordY[i];
154   }
155 }
156
157 //___________________________________________
158 AliMUONLocalTriggerBoard& AliMUONLocalTriggerBoard::operator=(const AliMUONLocalTriggerBoard& right)
159 {
160 /// Assigment operator;
161 /// equal operator
162
163   if (this == &right)
164     return *this;
165
166   // base class assignement
167   AliMUONTriggerBoard::operator=(right);
168
169   fMpLocalBoard = right.fMpLocalBoard;
170   fStripX11 = right.fStripX11;
171   fStripY11 = right.fStripY11;
172   fDev      = right.fDev;
173   fTrigY    = right.fTrigY;
174   fOutput   = right.fOutput;
175   fLUT      = right.fLUT;
176   fCoinc44  = right.fCoinc44;
177
178   for (Int_t i=0; i<2; i++) {
179     for (Int_t j=0; j<4; j++) {
180       fXY[i][j]  = right.fXY[i][j];
181       fXYU[i][j] = right.fXYU[i][j];
182       fXYD[i][j] = right.fXYD[i][j];
183
184       fMask[i][j] = right.fMask[i][j];
185     }
186   }
187
188   for (Int_t i=0; i<2; i++) {
189     fLutLpt[i] = right.fLutLpt[i];
190     fLutHpt[i] = right.fLutHpt[i];
191   }
192
193   for (Int_t i=0; i<5; i++) {
194     fMinDevStrip[i] = right.fMinDevStrip[i];
195     fMinDev[i] = right.fMinDev[i];
196     fCoordY[i] = right.fCoordY[i];
197   }
198
199   return *this;  
200 }
201
202 //___________________________________________
203 AliMUONLocalTriggerBoard::~AliMUONLocalTriggerBoard()
204 {
205 /// Destructor
206 }
207
208
209 //___________________________________________
210 Int_t AliMUONLocalTriggerBoard::GetNumber() const 
211 {
212 /// return board number for notified boards
213
214     if (fMpLocalBoard->IsNotified())
215         return fMpLocalBoard->GetId();
216     else 
217         return 0;
218 }
219
220 //___________________________________________
221 void AliMUONLocalTriggerBoard::Reset()
222 {
223 /// reset board
224 ///
225    for (Int_t i=0; i<2; i++) 
226       for (Int_t j=0; j<4; j++) 
227          fXY[i][j] = fXYU[i][j] = fXYD[i][j] = 0;
228
229    ResetResponse();
230 }
231
232 //___________________________________________
233 void AliMUONLocalTriggerBoard::ResetResponse()
234 {
235 /// reset board response
236 //
237   fResponse = 0;
238
239   for (Int_t i=0; i<5; i++) fMinDevStrip[i] = fMinDev[i] = fCoordY[i] = 0;
240
241   fOutput = 0;
242    
243   fStripX11 = 0;
244   fStripY11 = 15;
245   fDev = 0;
246   fTrigY = 1;
247
248   for (Int_t i=0; i<2; i++) fLutLpt[i] = fLutHpt[i] = 0;
249 }
250
251
252 //___________________________________________
253 void AliMUONLocalTriggerBoard::Setbit(Int_t strip, Int_t cathode, Int_t chamber)
254 {
255 /// 0 .. LBS   :   N-1 .. MSB
256    TBits w, m;
257
258    UShort_t xy = fXY[cathode][chamber], mask = fMask[cathode][chamber];
259
260    w.Set(16,&xy);
261    m.Set(16,&mask);
262
263    Int_t s = strip - int(strip / 16) * 16;
264
265    w.SetBitNumber(s);
266    
267    w &= m;
268
269    UShort_t value;
270
271    w.Get(&value);
272
273    fXY[cathode][chamber] = value;
274 }
275
276 //___________________________________________
277 void AliMUONLocalTriggerBoard::SetbitM(Int_t strip, Int_t cathode, Int_t chamber)
278 {
279 /// 0 .. LBS   :   N-1 .. MSB
280    TBits w, m;
281
282    UShort_t xy = fXY[cathode][chamber], mask = fMask[cathode][chamber];
283
284    w.Set(16,&xy);
285    m.Set(16,&mask);
286
287    w.SetBitNumber(strip);
288    
289    w &= m;
290
291    UShort_t value;
292
293    w.Get(&value);
294
295    fXY[cathode][chamber] = value;
296 }
297
298
299 //___________________________________________
300 void AliMUONLocalTriggerBoard::Pattern(const Option_t *option) const
301 {
302 /// print bit pattern
303 ///
304    TString op = option;
305    
306    if (op.Contains("X")) BP("X");
307
308    if (op.Contains("Y")) BP("Y");
309 }
310
311
312 //___________________________________________
313 void AliMUONLocalTriggerBoard::BP(const Option_t *option) const
314 {
315 /// Respect the old printout format
316   
317   const Int_t kModuleId[126] = 
318   {11,12,13,14,15,16,17,         // right side of the chamber
319   21,22,23,24,25,26,27,
320   31,32,33,34,35,36,37,
321   41,42,43,44,45,46,47,
322   51,52,53,54,55,56,57,
323   61,62,63,64,65,66,67,
324   71,72,73,74,75,76,77,
325   81,82,83,84,85,86,87,
326   91,92,93,94,95,96,97,   
327   -11,-12,-13,-14,-15,-16,-17,  // right side of chamber
328   -21,-22,-23,-24,-25,-26,-27,
329   -31,-32,-33,-34,-35,-36,-37,
330   -41,-42,-43,-44,-45,-46,-47,
331   -51,-52,-53,-54,-55,-56,-57,
332   -61,-62,-63,-64,-65,-66,-67,
333   -71,-72,-73,-74,-75,-76,-77,
334   -81,-82,-83,-84,-85,-86,-87,
335   -91,-92,-93,-94,-95,-96,-97};
336
337   const Int_t kNstripY[126]=
338   { 8, 8, 8, 8, 8, 8,16,  // right side of the chamber
339   8, 8, 8, 8, 8, 8,16,
340   16,16,16,16,16, 8,16,
341   16,16,16,16,16, 8,16,
342   0, 8,16,16,16, 8,16,
343   16,16,16,16,16, 8,16,
344   16,16,16,16,16, 8,16,
345   8, 8, 8, 8, 8, 8,16,
346   8, 8, 8, 8, 8, 8,16,  
347   8, 8, 8, 8, 8, 8,16,  // left side of the chamber
348   8, 8, 8, 8, 8, 8,16,
349   16,16,16,16,16, 8,16,
350   16,16,16,16,16, 8,16,
351   0, 8,16,16,16, 8,16,
352   16,16,16,16,16, 8,16,
353   16,16,16,16,16, 8,16,
354   8, 8, 8, 8, 8, 8,16,
355   8, 8, 8, 8, 8, 8,16};
356
357    TString op = option;
358         
359    TString nn = GetName();
360
361    if (op.Contains("X"))
362    {
363       printf("-------- TRIGGER INPUT ---------\n");
364       printf("--- warning: switchs not activated at this level ---\n");
365       printf("===============================================================\n");
366       printf("                            5432109876543210");
367
368       const char *x[4] = {"XMC11","XMC12","XMC21","XMC22"};
369       const char *s[4] = {"                      ",
370                     "                      ",
371                     "              ",
372                     "              "};
373       
374       for (Int_t ch=0; ch<4; ch++)
375       { 
376          printf("\n %s%s", x[ch], s[ch]);
377
378          UShort_t xy = fXY[0][ch];
379
380          TBits w(16); w.Set(16,&xy);
381
382          if (ch<2) cout << w;
383          else
384          {
385             UShort_t xyd = fXYD[0][ch], xyu = fXYU[0][ch];
386             TBits dw(16), uw(16); dw.Set(16,&xyd); uw.Set(16,&xyu); 
387
388             TBits ew(32);
389          
390             for (Int_t i=0;i<16;i++) ew[i+8] = w[i];
391
392             for (Int_t i=0;i<8;i++) 
393             {
394                ew[i]    = dw[i+8]; // 8 MSB
395                ew[i+24] = uw[i];   // 
396             }
397
398             cout << ew;
399          }
400       }
401    
402       printf("\n                    ");
403       printf("10987654321098765432109876543210\n");
404    }
405
406    if (op.Contains("Y"))
407    {
408       printf("---------------------------------------------------------------\n");
409       printf("                            ");
410
411 /*    OLD NUMBERING STYLE    */
412 /**/
413       Int_t idCircuit = 0, absidModule = 0;
414
415       if (!(nn.Contains("Int"))) 
416       { 
417         idCircuit   = fgkCircuitId[GetI()];
418         absidModule = TMath::Abs(Int_t(idCircuit/10));
419       }
420                 
421       Int_t iModule=0;
422
423       for (Int_t i=0; i<63; i++) 
424       {
425         if (kModuleId[i]==absidModule) 
426          { 
427             iModule=i;
428             break;
429          }
430       }
431
432       Int_t nStrip = kNstripY[iModule];
433       for (Int_t istrip=nStrip-1; istrip>=0; istrip--) {
434          if (istrip>9)  printf("%i",istrip-10*Int_t(istrip/10));
435          if (istrip<10) printf("%i",istrip);
436       }
437 /**/
438 /*                           */
439
440       UShort_t xyval = 0;
441
442       if (GetSwitch(1))
443       {
444          xyval = fXY[1][0];
445          TBits v11(8); v11.Set(8,&xyval);
446          printf("\n YMC11                      ");
447          cout << v11;         
448
449          xyval = fXY[1][1];
450          TBits v12(8); v12.Set(8,&xyval);
451          printf("\n YMC12                      ");
452          cout << v12;
453
454          xyval = fXY[1][2];
455          TBits v21(8); v21.Set(8,&xyval);
456          printf("\n YMC21                      ");         
457          cout << v21;
458
459          xyval = fXY[1][3];
460          TBits v22(8); v22.Set(8,&xyval);
461          printf("\n YMC22                      ");
462          cout << v22 << endl;
463       }
464       else
465       {
466          xyval = fXY[1][0];
467          TBits v11(16); v11.Set(16,&xyval);
468          printf("\n YMC11                      ");
469          cout << v11;         
470
471          xyval = fXY[1][1];
472          TBits v12(16); v12.Set(16,&xyval);
473          printf("\n YMC12                      ");
474          cout << v12;
475
476          xyval = fXY[1][2];
477          TBits v21(16); v21.Set(16,&xyval);
478          printf("\n YMC21                      ");         
479          cout << v21;
480
481          xyval = fXY[1][3];
482          TBits v22(16); v22.Set(16,&xyval);
483          printf("\n YMC22                      ");
484          cout << v22 << endl;
485       }
486
487 //    tmp
488       printf("---------------------------------------------------------------");
489       printf("\n upper part of circuit %i",idCircuit);
490       printf("\n UMC21                      ");
491       xyval = fXYU[1][2];
492       TBits wu21(16); wu21.Set(16,&xyval);
493       cout << wu21;
494       printf("\n UMC22                      ");
495       xyval = fXYU[1][3];
496       TBits wu22(16); wu22.Set(16,&xyval);
497       cout << wu22;
498       printf("\n lower part of circuit %i",idCircuit);
499       printf("\n LMC21                      ");
500       xyval = fXYD[1][2];
501       TBits wl21(16); wl21.Set(16,&xyval);
502       cout << wl21;
503       printf("\n LMC22                      ");
504       xyval = fXYD[1][3];
505       TBits wl22(16); wl22.Set(16,&xyval);
506       cout << wl22;
507       printf("\n");
508       printf("===============================================================\n");
509    }
510 }
511
512 //___________________________________________
513 void AliMUONLocalTriggerBoard::TrigX(Int_t ch1q[16], Int_t ch2q[16], Int_t ch3q[32], Int_t ch4q[32])
514 {
515 /// note : coinc44 = flag 0 or 1 (0 coincidence -> 3/4, 1 coincidence -> 4/4) \n
516 ///---------------------------------------------------------                  \n
517 /// step # 1 : declustering, reduction DS, calculate sgle & dble              \n 
518 ///---------------------------------------------------------
519    Int_t ch1e[19], ch2e[20], ch3e[35], ch4e[36]; 
520    Int_t sgleHit1[31], sgleHit2[63];
521    Int_t dbleHit1[31], dbleHit2[63];
522
523    Int_t i;
524    Int_t j;
525    Int_t istrip;
526
527    for (i=0; i<31; i++) {
528       sgleHit1[i]=0;
529       dbleHit1[i]=0;
530    }
531    for (i=0; i<63; i++) {
532       sgleHit2[i]=0;
533       dbleHit2[i]=0;
534    }
535
536 //--- inititialize che using chq 
537 //--- switch zero_down, zero_middle & zero_up added 30/05/07
538 //--- fSwitch[7/8/9] = zero_down/zero_middle/zero_up
539    for (i=0; i<19; i++) {
540       if (i<1||i>16)  ch1e[i]=0; 
541       else            ch1e[i]=ch1q[i-1]; 
542    }
543    for (i=0; i<20; i++) {
544       if (i<2||i>17) ch2e[i]=0; 
545       else           ch2e[i]=ch2q[i-2]; 
546    }
547    for (i=0; i<35; i++) {
548       if (i<1||i>32) ch3e[i]=0; 
549       else if (i>=1 && i<=8)   ch3e[i]=ch3q[i-1]&!GetSwitch(7);
550       else if (i>=9 && i<=24)  ch3e[i]=ch3q[i-1]&!GetSwitch(8);
551       else if (i>=25 && i<=32) ch3e[i]=ch3q[i-1]&!GetSwitch(9);
552    }
553    for (i=0; i<36; i++) {
554       if (i<2||i>33) ch4e[i]=0; 
555       else if (i>=2 && i<=9)   ch4e[i]=ch4q[i-2]&!GetSwitch(7);
556       else if (i>=10 && i<=25) ch4e[i]=ch4q[i-2]&!GetSwitch(8);
557       else if (i>=26 && i<=33) ch4e[i]=ch4q[i-2]&!GetSwitch(9);
558    }
559
560 //--- calculate dble & sgle first station
561    for (i=0; i<=15; i++) {                   
562       sgleHit1[2*i] = (!ch1e[i+1]|(ch1e[i]^ch1e[i+2])) & 
563          (!ch2e[i+2] | (ch2e[i+1]^ch2e[i+3]));
564
565       dbleHit1[2*i] = ch1e[i+1]&!(ch1e[i+2]^ch1e[i]) & 
566          (ch2e[i+2] | (!ch2e[i]&ch2e[i+1]) | (ch2e[i+3]&!ch2e[i+4]));
567    }
568
569    for (i=0; i<=14; i++) {               
570       sgleHit1[2*i+1] = (!ch1e[i+1]|!ch1e[i+2]|(ch1e[i]^ch1e[i+3])) & 
571          (!ch2e[i+2] | !ch2e[i+3] | (ch2e[i+1]^ch2e[i+4]));
572       dbleHit1[2*i+1] = ch1e[i+1]&ch1e[i+2]&!(ch1e[i]^ch1e[i+3]) & 
573         ((ch2e[i+2]&(!ch2e[i+1]|!ch2e[i])) | 
574           (ch2e[i+3]&(ch2e[i+2]|!ch2e[i+4]|!ch2e[i+5])));
575    }
576
577 //--- calculate dble & sgle second station
578    for (i=0; i<=31; i++) {               
579       sgleHit2[2*i] = (!ch3e[i+1]|(ch3e[i]^ch3e[i+2])) & 
580          (!ch4e[i+2] | (ch4e[i+1]^ch4e[i+3]));
581       dbleHit2[2*i] = ch3e[i+1]&!(ch3e[i+2]^ch3e[i]) & 
582          (ch4e[i+2] | (!ch4e[i]&ch4e[i+1]) | (ch4e[i+3]&!ch4e[i+4]));
583    }
584   
585    for (i=0; i<=30; i++) {               
586       sgleHit2[2*i+1] = (!ch3e[i+1]|!ch3e[i+2]|(ch3e[i]^ch3e[i+3])) & 
587          (!ch4e[i+2] | !ch4e[i+3] | (ch4e[i+1]^ch4e[i+4]));
588       dbleHit2[2*i+1] = ch3e[i+1]&ch3e[i+2]&!(ch3e[i]^ch3e[i+3]) & 
589         ((ch4e[i+2]&(!ch4e[i+1]|!ch4e[i])) | 
590           (ch4e[i+3]&((ch4e[i+2]|!ch4e[i+4])|!ch4e[i+5])));
591    }
592
593 //--- 
594    if(AliDebugLevel()==3||AliDebugLevel()==5) {
595       printf("===============================================================\n");
596       printf(" X plane after sgle and dble \n");
597       printf("                       0987654321098765432109876543210");
598       printf("\n SGLE1                 ");
599       for (istrip=30; istrip>=0; istrip--) printf("%i",(!sgleHit1[istrip]));
600       printf("\n DBLE1                 ");
601       for (istrip=30; istrip>=0; istrip--) printf("%i",dbleHit1[istrip]);
602       printf("\n SGLE2 ");
603       for (istrip=62; istrip>=0; istrip--) printf("%i",(!sgleHit2[istrip]));
604       printf("\n DBLE2 ");
605       for (istrip=62; istrip>=0; istrip--) printf("%i",dbleHit2[istrip]);
606       printf("\n       210987654321098765432109876543210987654321098765432109876543210\n");
607    }
608   
609 //---------------------------------------------------------
610 // step # 2 : coincidence 3/4
611 //---------------------------------------------------------
612    Int_t rearImage[31][31];
613    for (i=0; i<31; i++) {
614       for (j=0; j<31; j++) {
615          rearImage[i][j]=0;
616       }
617    }
618
619    Int_t notOr1=!dbleHit1[30] & !dbleHit1[29] & !dbleHit1[28] & !dbleHit1[27] & 
620       !dbleHit1[26] & !dbleHit1[25] & !dbleHit1[24] & !dbleHit1[23] &
621       !dbleHit1[22] & !dbleHit1[21] & !dbleHit1[20] & !dbleHit1[19] & 
622       !dbleHit1[18] & !dbleHit1[17] & !dbleHit1[16] & !dbleHit1[15] & 
623       !dbleHit1[14] & !dbleHit1[13] & !dbleHit1[12] & !dbleHit1[11] & 
624       !dbleHit1[10] & !dbleHit1[9]  & !dbleHit1[8]  & !dbleHit1[7]  & 
625       !dbleHit1[6]  & !dbleHit1[5]  & !dbleHit1[4]  & !dbleHit1[3]  & 
626       !dbleHit1[2]  & !dbleHit1[1]  & !dbleHit1[0]  & !fCoinc44;
627
628    Int_t notOr2= !dbleHit2[62] & !dbleHit2[61] & !dbleHit2[60] & !dbleHit2[59] & 
629       !dbleHit2[58] & !dbleHit2[57] & !dbleHit2[56] & !dbleHit2[55] & 
630       !dbleHit2[54] & !dbleHit2[53] & !dbleHit2[52] & !dbleHit2[51] & 
631       !dbleHit2[50] & !dbleHit2[49] & !dbleHit2[48] & !dbleHit2[47] & 
632       !dbleHit2[46] & !dbleHit2[45] & !dbleHit2[44] & !dbleHit2[43] & 
633       !dbleHit2[42] & !dbleHit2[41] & !dbleHit2[40] & !dbleHit2[39] & 
634       !dbleHit2[38] & !dbleHit2[37] & !dbleHit2[36] & !dbleHit2[35] & 
635       !dbleHit2[34] & !dbleHit2[33] & !dbleHit2[32] & !dbleHit2[31] &
636       !dbleHit2[30] & !dbleHit2[29] & !dbleHit2[28] & !dbleHit2[27] & 
637       !dbleHit2[26] & !dbleHit2[25] & !dbleHit2[24] & !dbleHit2[23] & 
638       !dbleHit2[22] & !dbleHit2[21] & !dbleHit2[20] & !dbleHit2[19] & 
639       !dbleHit2[18] & !dbleHit2[17] & !dbleHit2[16] & !dbleHit2[15] & 
640       !dbleHit2[14] & !dbleHit2[13] & !dbleHit2[12] & !dbleHit2[11] & 
641       !dbleHit2[10] & !dbleHit2[9]  & !dbleHit2[8]  & !dbleHit2[7]  & 
642       !dbleHit2[6]  & !dbleHit2[5]  & !dbleHit2[4]  & !dbleHit2[3]  & 
643       !dbleHit2[2]  & !dbleHit2[1]  & !dbleHit2[0]  & !fCoinc44;        
644
645 // DS reduction
646    for (i=0; i<31; i++) {
647       sgleHit1[i] = !sgleHit1[i]&notOr1;
648    }
649    for (i=0; i<63; i++) {
650       sgleHit2[i] = !sgleHit2[i]&notOr2;
651    }
652
653 // extract rearImage
654    for (i=0; i<31; i++){
655       Int_t tmpSgleHit2[31];
656       Int_t tmpDbleHit2[31];
657       for (j=0; j<31; j++){
658          tmpSgleHit2[j] = sgleHit2[i+j+1];
659          tmpDbleHit2[j] = dbleHit2[i+j+1];
660       }
661
662       for (Int_t k=0; k<31; k++) {
663          rearImage[i][k]=(sgleHit1[i]&tmpDbleHit2[k])|
664             (dbleHit1[i]&(tmpSgleHit2[k]|tmpDbleHit2[k]));
665       }
666    }
667
668    //-----------
669    if(AliDebugLevel()==3||AliDebugLevel()==5) {
670       printf("===============================================================\n");
671       for (i=30; i>=0; i--) {
672          printf("%i \t",i);
673          for (istrip=31; istrip>=0; istrip--) printf("%i",rearImage[i][istrip]);
674          printf("\n");   
675       }
676    }
677
678 //---------------------------------------------------------
679 // step # 3 : calculate deviation
680 //--------------------------------------------------------- 
681    Int_t dev[31][6];
682    for (i=0; i<31; i++) {
683       for (j=0; j<6; j++) {
684          dev[i][j]=0;
685       }
686    }
687
688    for (i=0; i<31; i++){
689       Int_t leftDev[5], rightDev[5]; 
690       Int_t orL1, andL1, andL2, orR1, orR2, andR1, andR2, andR3;
691
692 // calculate Left deviation
693       orL1=rearImage[i][16]|rearImage[i][18]|rearImage[i][20]|rearImage[i][22];
694       andL1=!rearImage[i][17]&!rearImage[i][19]&!rearImage[i][21] & !orL1; 
695       andL2=!rearImage[i][23]&!rearImage[i][24]&!rearImage[i][25]&!rearImage[i][26];
696  
697       leftDev[0] = (rearImage[i][16]|!rearImage[i][17]) & 
698          (rearImage[i][16]|rearImage[i][18]|(!rearImage[i][19]&
699          (rearImage[i][20]|!rearImage[i][21]))) &
700          (orL1|(!rearImage[i][23]&(rearImage[i][24]|!rearImage[i][25]))) & 
701          (orL1|rearImage[i][24]|rearImage[i][26]|(!rearImage[i][27]&
702          (rearImage[i][28]|!rearImage[i][29])));
703                                 
704       leftDev[1] = !rearImage[i][16] & 
705          !(!rearImage[i][17]&!rearImage[i][18]&!rearImage[i][21]&!rearImage[i][22] & 
706          (!rearImage[i][25]&!rearImage[i][26]&(rearImage[i][27]|rearImage[i][28]))) &
707          (rearImage[i][17]|rearImage[i][18] | (!rearImage[i][19]&!rearImage[i][20])) &
708          (rearImage[i][17]|rearImage[i][18]|rearImage[i][21]|rearImage[i][22] | 
709          (!rearImage[i][23]&!rearImage[i][24]));
710                                 
711       leftDev[2] = (!rearImage[i][16]&!rearImage[i][17]&!rearImage[i][18]) & 
712          (rearImage[i][19]|rearImage[i][20]|rearImage[i][21]|rearImage[i][22] | andL2);
713                 
714       leftDev[3] = andL1;
715                 
716       leftDev[4] = 
717          !rearImage[i][27]&!rearImage[i][28]&!rearImage[i][29]&!rearImage[i][30] & 
718          andL1 & andL2;
719
720       // calculate Right deviation
721       orR1=rearImage[i][8]|rearImage[i][10]|rearImage[i][12]|rearImage[i][14];
722       orR2=rearImage[i][8]|rearImage[i][9]|rearImage[i][10]|rearImage[i][11];
723       andR1=!rearImage[i][12]&!rearImage[i][13]&!rearImage[i][14]&!rearImage[i][15];
724       andR2=
725          !rearImage[i][8]&!rearImage[i][9]&!rearImage[i][10]&!rearImage[i][11] & andR1;
726       andR3=!rearImage[i][4]&!rearImage[i][5]&!rearImage[i][6]&!rearImage[i][7]; 
727                 
728       rightDev[0] = !rearImage[i][15]&(rearImage[i][14]|!rearImage[i][13]) & 
729           ((rearImage[i][12]|rearImage[i][14]|(!rearImage[i][11]&
730           (rearImage[i][10]|!rearImage[i][9]))) &
731           ((orR1|(!rearImage[i][7]&(rearImage[i][6]|!rearImage[i][5]))) & 
732            (orR1|rearImage[i][4]|rearImage[i][6]|(!rearImage[i][3]&(rearImage[i][2]|
733                                                                    !rearImage[i][1])))));
734                                 
735       rightDev[1] = !rearImage[i][15]&!rearImage[i][14] & 
736          !(!rearImage[i][4]&!rearImage[i][5]&!rearImage[i][8]&!rearImage[i][9] &
737            (!rearImage[i][12]&!rearImage[i][13]&(rearImage[i][2]|rearImage[i][3]))) &
738          (rearImage[i][12]|rearImage[i][13] | (!rearImage[i][10]&!rearImage[i][11])) & 
739          (rearImage[i][8]|rearImage[i][9]|rearImage[i][12]|rearImage[i][13] | 
740           (!rearImage[i][6]&!rearImage[i][7]));
741                 
742       rightDev[2] = andR1 & (orR2 | andR3); 
743       rightDev[3] = andR2;              
744       rightDev[4] = 
745          !rearImage[i][0]&!rearImage[i][1]&!rearImage[i][2]&!rearImage[i][3] & 
746          andR2 & andR3 ;
747
748       // compare Left & Right deviations
749       Int_t tmpLeftDev=0, tmpRightDev=0;
750       for (j=0; j<5; j++){
751          tmpLeftDev  = tmpLeftDev + Int_t(leftDev[j]<<j); 
752          tmpRightDev = tmpRightDev + Int_t(rightDev[j]<<j); 
753       }
754
755       // assign mimimum deviation do dev[][]
756       if (tmpLeftDev < tmpRightDev ){
757          for (j=0; j<5; j++){ dev[i][j]=leftDev[j];}
758          dev[i][5]=1;
759       } else {
760          for (j=0; j<5; j++){ dev[i][j]=rightDev[j];}
761          dev[i][5]=0;
762       }
763    }
764   
765 //---
766    if(AliDebugLevel()==3||AliDebugLevel()==5) {
767       printf("===============================================================\n");
768       for (i=30; i>=0; i--) {
769          printf("%i \t",i);
770          for (istrip=5; istrip>=0; istrip--) printf("%i",dev[i][istrip]);
771          printf(" \n");
772       }
773    }
774
775 //---------------------------------------------------------
776 // step # 4 : sort deviation
777 //--------------------------------------------------------- 
778    Int_t bga1[16], bga2[8], bga3[4], bga4[2], bga5;
779    Int_t tmpbga1[16][6], tmpbga2[8][6], tmpbga3[4][6], tmpbga4[2][6], tmpbga5[6];
780    Int_t tmpMax[6]={1,1,1,1,1,0};
781
782    for (i=0; i<15; i++) {
783       Sort2x5(dev[2*i],dev[2*i+1],tmpbga1[i],bga1[i]);
784    }  
785    Sort2x5(dev[30],tmpMax,tmpbga1[15],bga1[15]);
786
787 //--    
788    if(AliDebugLevel()==3||AliDebugLevel()==5) {
789       printf("===============================================================\n");
790       printf(" sorting : 1st level \n");
791       for (i=15; i>=0; i--) {
792          printf("\t %i \t",bga1[i]);    
793          for (j=5; j>=0; j--) printf("%i",tmpbga1[i][j]); 
794          printf(" \n");
795       }
796    }
797
798    for (i=0; i<8; i++) {  
799       Sort2x5(tmpbga1[2*i],tmpbga1[2*i+1],tmpbga2[i],bga2[i]);
800    }
801
802 //--    
803    if(AliDebugLevel()==3||AliDebugLevel()==5) {
804       printf("===============================================================\n");
805       printf(" sorting : 2nd level \n");
806       for (i=7; i>=0; i--) {
807          printf("\t %i \t",bga2[i]);    
808          for (j=5; j>=0; j--) printf("%i",tmpbga1[i][j]);       
809          printf(" \n");
810       }
811    }
812   
813    for (i=0; i<4; i++) {  
814       Sort2x5(tmpbga2[2*i],tmpbga2[2*i+1],tmpbga3[i],bga3[i]);
815    }
816
817 //--    
818    if(AliDebugLevel()==3||AliDebugLevel()==5) {
819       printf("===============================================================\n");
820       printf(" sorting : 3rd level \n");
821       for (i=3; i>=0; i--) {
822          printf("\t %i \t",bga3[i]);    
823          for (j=5; j>=0; j--) printf("%i",tmpbga3[i][j]); 
824          printf(" \n");
825       }
826    }
827
828    for (i=0; i<2; i++) {  
829       Sort2x5(tmpbga3[2*i],tmpbga3[2*i+1],tmpbga4[i],bga4[i]);
830    }
831
832 //--    
833    if(AliDebugLevel()==3||AliDebugLevel()==5) {
834       printf("===============================================================\n");
835       printf(" sorting : 4th level \n");
836       for (i=1; i>=0; i--) {
837          printf("\t %i \t",bga4[i]);    
838          for (j=5; j>=0; j--) printf("%i",tmpbga4[i][j]);
839          printf(" \n");
840       }
841    }
842   
843    Sort2x5(tmpbga4[0],tmpbga4[1],tmpbga5,bga5);
844
845    // coding from 6 to 5 bits 
846    fMinDev[4] = tmpbga5[5] | tmpbga5[4];
847    for (i=0; i<4; i++) { 
848       fMinDev[i]=tmpbga5[i] & !tmpbga5[4];
849    }
850
851    // find address of strip with minimum deviation 
852    fMinDevStrip[4]=bga5;
853    if (bga5<=1) fMinDevStrip[3]=bga4[bga5];
854
855    Int_t tmpAd=fMinDevStrip[3]+fMinDevStrip[4]*2;
856    if (tmpAd<=3) fMinDevStrip[2]=bga3[tmpAd];
857
858    tmpAd=fMinDevStrip[2]+fMinDevStrip[3]*2+fMinDevStrip[4]*4;
859    if (tmpAd<=7) fMinDevStrip[1]=bga2[tmpAd];
860
861    tmpAd=fMinDevStrip[1]+fMinDevStrip[2]*2+fMinDevStrip[3]*4+fMinDevStrip[4]*8;
862    if (tmpAd<=15) fMinDevStrip[0]=bga1[tmpAd];
863
864    if(AliDebugLevel()==3||AliDebugLevel()==5) {
865       printf("===============================================================\n");
866       printf("minDevStrip = ");
867       for  (i=4; i>=0; i--) printf("%i",fMinDevStrip[i]);
868       printf(" minDev = ");
869       for  (i=4; i>=0; i--) printf("%i",fMinDev[i]); 
870       printf(" \n");
871       printf("===============================================================\n");
872    }
873
874 }
875
876 //___________________________________________
877 void AliMUONLocalTriggerBoard::Sort2x5(Int_t dev1[6], Int_t dev2[6],
878                                        Int_t minDev[6], Int_t &dev1GTdev2)
879
880 /// returns minimun between dev1 and dev2
881    Int_t tmpDev1=0, tmpDev2=0;
882
883    for (Int_t j=0; j<5; j++)
884    {
885       tmpDev1 += Int_t(dev1[j]<<j); 
886       tmpDev2 += Int_t(dev2[j]<<j); 
887    }
888
889    if (tmpDev1<=tmpDev2)
890    {
891       for (Int_t j=0; j<=5; j++) minDev[j]=dev1[j];
892       dev1GTdev2=0;
893    } 
894    else 
895    {
896       for (Int_t j=0; j<=5; j++) minDev[j]=dev2[j];
897       dev1GTdev2=1;   
898    }
899 }
900
901 //___________________________________________
902 void AliMUONLocalTriggerBoard::TrigY(Int_t y1[16], Int_t y2[16], Int_t y3[16], Int_t y4[16],
903                                      Int_t y3u[16], Int_t y3d[16], Int_t y4u[16], Int_t y4d[16])
904 {
905 /// note : resMid = 1 -> cancel                             \n
906 ///---------------------------------------------------------\n
907 /// step # 1 : prehandling Y                                \n
908 ///--------------------------------------------------------- 
909    Int_t i;
910    Int_t istrip;
911
912    for (i=0; i<16; i++)
913    {
914      y3[i]=y3[i]&!GetSwitch(8);
915      y4[i]=y4[i]&!GetSwitch(8);
916    }
917
918 // 10/29/04 fZeroAllYLSB added
919 //    for (i=0; i<8; i++)
920 //    {
921 //       y1[i] = y1[i]&!fSwitch[6];     
922 //       y2[i] = y2[i]&!fSwitch[6];      
923 //       y3[i] = y3[i]&!fSwitch[6];      
924 //       y4[i] = y4[i]&!fSwitch[6];
925 //    }
926
927    Int_t ch1[16], ch2[16], ch3[16], ch4[16];
928
929    Int_t tmpy3to16[16], tmpy4to16[16];
930    Int_t tmpy3uto16[16], tmpy3dto16[16], tmpy4uto16[16], tmpy4dto16[16];
931    for (i=0; i<8; i++){
932       ch1[2*i]   = (y1[i]&GetSwitch(1)) | (y1[2*i]&!GetSwitch(1));              
933       ch1[2*i+1] = (y1[i]&GetSwitch(1)) | (y1[2*i+1]&!GetSwitch(1));
934
935       ch2[2*i]   = (y2[i]&GetSwitch(1)) | (y2[2*i]&!GetSwitch(1));              
936       ch2[2*i+1] = (y2[i]&GetSwitch(1)) | (y2[2*i+1]&!GetSwitch(1));
937
938       tmpy3to16[2*i  ] = (y3[i]&GetSwitch(1)) | (y3[2*i  ]&!GetSwitch(1));              
939       tmpy3to16[2*i+1] = (y3[i]&GetSwitch(1)) | (y3[2*i+1]&!GetSwitch(1));
940
941       tmpy4to16[2*i  ] = (y4[i]&GetSwitch(1)) | (y4[2*i  ]&!GetSwitch(1));
942       tmpy4to16[2*i+1] = (y4[i]&GetSwitch(1)) | (y4[2*i+1]&!GetSwitch(1));
943
944       tmpy3uto16[2*i  ] = (y3u[i]&GetSwitch(2)) | (y3u[2*i  ]&!GetSwitch(2)); 
945       tmpy3uto16[2*i+1] = (y3u[i]&GetSwitch(2)) | (y3u[2*i+1]&!GetSwitch(2));
946
947       tmpy4uto16[2*i  ] = (y4u[i]&GetSwitch(2)) | (y4u[2*i  ]&!GetSwitch(2)); 
948       tmpy4uto16[2*i+1] = (y4u[i]&GetSwitch(2)) | (y4u[2*i+1]&!GetSwitch(2));
949
950       tmpy3dto16[2*i  ] = (y3d[i]&GetSwitch(0)) | (y3d[2*i  ]&!GetSwitch(0)); 
951       tmpy3dto16[2*i+1] = (y3d[i]&GetSwitch(0)) | (y3d[2*i+1]&!GetSwitch(0));
952     
953       tmpy4dto16[2*i  ] = (y4d[i]&GetSwitch(0)) | (y4d[2*i  ]&!GetSwitch(0)); 
954       tmpy4dto16[2*i+1] = (y4d[i]&GetSwitch(0)) | (y4d[2*i+1]&!GetSwitch(0));
955    }
956   
957    for (i=0; i<16; i++) ch3[i] = ch4[i] = 0;
958
959    if (GetSwitch(3)==0&&GetSwitch(4)==0){
960       for (i=0; i<16; i++){
961          ch3[i] = tmpy3to16[i];
962          ch4[i] = tmpy4to16[i];
963       }
964    }
965    if (GetSwitch(3)==0&&GetSwitch(4)==1){
966       for (i=0; i<16; i++){
967          ch3[i] = tmpy3dto16[i]|tmpy3to16[i];
968          ch4[i] = tmpy4dto16[i]|tmpy4to16[i];
969       }
970    }
971    if (GetSwitch(3)==1&&GetSwitch(4)==0){
972       for (i=0; i<16; i++){
973          ch3[i] = tmpy3uto16[i]|tmpy3to16[i];
974          ch4[i] = tmpy4uto16[i]|tmpy4to16[i];
975       }
976    }
977    if (GetSwitch(3)==1&&GetSwitch(4)==1){
978       for (i=0; i<16; i++){
979          ch3[i] = tmpy3dto16[i]|tmpy3to16[i]|tmpy3uto16[i];
980          ch4[i] = tmpy4dto16[i]|tmpy4to16[i]|tmpy4uto16[i];
981       }
982    }
983
984 // debug
985    if(AliDebugLevel()==4||AliDebugLevel()==5) {
986       printf("===============================================================\n");  
987       printf(" Y plane after PreHandling x2m x2u x2d orMud %i %i %i %i %i \n",
988              GetSwitch(1),GetSwitch(2), GetSwitch(0),GetSwitch(3),GetSwitch(4));
989       printf("                            ");
990       for (istrip=15; istrip>=0; istrip--) {
991          if (istrip>9)  printf("%i",istrip-10*Int_t(istrip/10));
992          if (istrip<10) printf("%i",istrip);
993       }  
994       printf("\n YMC11                      ");
995       for (istrip=15; istrip>=0; istrip--) printf("%i",ch1[istrip]); 
996       printf("\n YMC12                      ");
997       for (istrip=15; istrip>=0; istrip--) printf("%i",ch2[istrip]); 
998       printf("\n YMC21                      ");
999       for (istrip=15; istrip>=0; istrip--) printf("%i",ch3[istrip]); 
1000       printf("\n YMC22                      ");
1001       for (istrip=15; istrip>=0; istrip--) printf("%i",ch4[istrip]); 
1002       printf(" \n"); 
1003    }
1004 //debug
1005   
1006 //---------------------------------------------------------
1007 // step # 2 : calculate sgle and dble, apply DS reduction
1008 //--------------------------------------------------------- 
1009    Int_t sgle1[16], dble1[16];
1010    Int_t sgle2[16], dble2[16];
1011
1012    // Calculate simple and double hits
1013    for (i=0; i<16; i++) {
1014       dble1[i] = ch1[i] & ch2[i];
1015       dble2[i] = ch3[i] & ch4[i];
1016     
1017       sgle1[i] = (ch1[i]|ch2[i]);
1018       sgle2[i] = (ch3[i]|ch4[i]);
1019    }
1020
1021    //debug
1022    if(AliDebugLevel()==4||AliDebugLevel()==5) {
1023       printf("===============================================================\n");
1024       printf(" Y plane after sgle dble \n"); 
1025       printf("                            ");
1026       for (istrip=15; istrip>=0; istrip--) {
1027          if (istrip>9)  printf("%i",istrip-10*Int_t(istrip/10));
1028          if (istrip<10) printf("%i",istrip);
1029       }  
1030       printf("\n SGLE1                      ");
1031       for (istrip=15; istrip>=0; istrip--) printf("%i",sgle1[istrip]); 
1032       printf("\n DBLE1                      ");
1033       for (istrip=15; istrip>=0; istrip--) printf("%i",dble1[istrip]); 
1034       printf("\n SGLE2                      ");
1035       for (istrip=15; istrip>=0; istrip--) printf("%i",sgle2[istrip]); 
1036       printf("\n DBLE2                      ");
1037       for (istrip=15; istrip>=0; istrip--) printf("%i",dble2[istrip]); 
1038       printf(" \n"); 
1039    }
1040    //debug
1041
1042    // DS Reduction 
1043    Int_t notOr1, notOr2;
1044
1045    notOr1=!dble1[15] & !dble1[14] & !dble1[13] & !dble1[12] & 
1046       !dble1[11] & !dble1[10] & !dble1[9]  & !dble1[8]  & 
1047       !dble1[7]  & !dble1[6]  & !dble1[5]  & !dble1[4]  & 
1048       !dble1[3]  & !dble1[2]  & !dble1[1]  & !dble1[0];
1049
1050    notOr2=!dble2[15] & !dble2[14] & !dble2[13] & !dble2[12] & 
1051       !dble2[11] & !dble2[10] & !dble2[9]  & !dble2[8]  & 
1052       !dble2[7]  & !dble2[6]  & !dble2[5]  & !dble2[4]  & 
1053       !dble2[3]  & !dble2[2]  & !dble2[1]  & !dble2[0];
1054
1055    for (i=0; i<16; i++) {
1056       sgle1[i] = sgle1[i] & notOr1 & !fCoinc44;
1057       sgle2[i] = sgle2[i] & notOr2 & !fCoinc44;
1058    }
1059
1060 //---------------------------------------------------------
1061 // step # 3 : 3/4 coincidence 
1062 //--------------------------------------------------------- 
1063    Int_t frontImage[16];
1064
1065    for (i=1; i<15; i++) {
1066      frontImage[i] = ((dble1[i] | sgle1[i]) & 
1067                       (dble2[i+1] | dble2[i] | dble2[i-1])) |
1068         (dble1[i] & (sgle2[i+1] | sgle2[i] | sgle2[i-1]));
1069    }
1070    frontImage[0] = ((dble1[0] | sgle1[0]) & 
1071                     (dble2[1] | dble2[0])) | (dble1[0] & (sgle2[1] | sgle2[0]));
1072
1073    frontImage[15] = ((dble1[15] | sgle1[15]) & 
1074                      (dble2[15] | dble2[14])) | (dble1[15] & (sgle2[15] | sgle2[14]));
1075
1076
1077 //debug
1078    if(AliDebugLevel()==4||AliDebugLevel()==5) {
1079       printf("===============================================================\n");
1080       printf(" Y plane frontImage\n");
1081       printf("                            ");
1082       for (istrip=15; istrip>=0; istrip--) {
1083          if (istrip>9)  printf("%i",istrip-10*Int_t(istrip/10));
1084          if (istrip<10) printf("%i",istrip);
1085       }
1086       printf("\n                            ");
1087       for (istrip=15; istrip>=0; istrip--) printf("%i",frontImage[istrip]); 
1088       printf("\n");
1089    }
1090 //debug
1091
1092 //---------------------------------------------------------
1093 // step # 4 : Y position 
1094 //--------------------------------------------------------- 
1095    Int_t or1, or2, and1, and2, and3;
1096
1097    or1  = frontImage[7]|frontImage[5]|frontImage[3]|frontImage[1];
1098    or2  = frontImage[7]|frontImage[6]|frontImage[5]|frontImage[4];
1099    and1 = !frontImage[3]&!frontImage[2]&!frontImage[1]&!frontImage[0];
1100    and2 = !frontImage[7]&!frontImage[6]&!frontImage[5]&!frontImage[4] & and1;
1101    and3 = !frontImage[11]&!frontImage[10]&!frontImage[9]&!frontImage[8]; 
1102  
1103    fCoordY[0] = !frontImage[0]&(frontImage[1]|!frontImage[2]) & 
1104      (frontImage[3]|frontImage[1]|(!frontImage[4]&(frontImage[5]|!frontImage[6]))) &
1105      (or1|(!frontImage[8]&(frontImage[9]|!frontImage[10]))) & 
1106      (or1|frontImage[11]|frontImage[9]|(!frontImage[12]&(frontImage[13]|!frontImage[14])));
1107  
1108    fCoordY[1] = !frontImage[0]&!frontImage[1] & 
1109       !(!frontImage[11]&!frontImage[10]&!frontImage[7]&!frontImage[6] & 
1110         !frontImage[3]&!frontImage[2]&(frontImage[13]|frontImage[12])) &
1111      (frontImage[3]|frontImage[2] | (!frontImage[5]&!frontImage[4])) & 
1112       (frontImage[7]|frontImage[6]|frontImage[3]|frontImage[2] | 
1113        (!frontImage[9]&!frontImage[8]));
1114                 
1115    fCoordY[2] = and1 & (or2 | and3);
1116                 
1117    fCoordY[3] = and2;
1118                 
1119    fCoordY[4] = !frontImage[15]&!frontImage[14]&!frontImage[13]&!frontImage[12] &
1120       and2 & and3 ;
1121 }
1122
1123 //___________________________________________
1124 void AliMUONLocalTriggerBoard::LocalTrigger()
1125 {
1126 /// L0 trigger after LUT
1127 ///
1128     Int_t deviation=0;
1129     Int_t iStripY=0;
1130     Int_t iStripX=0;
1131     Bool_t xOutput = IsTrigX();
1132     Bool_t yOutput = IsTrigY();
1133
1134    if (xOutput) {
1135       for (Int_t i=0; i<5; i++) iStripX += static_cast<int>( fMinDevStrip[i] << i );
1136       for (Int_t i=0; i<4; i++) deviation += static_cast<int>( fMinDev[i] << i );
1137       fDev      = deviation;
1138       fStripX11 = iStripX;
1139    }
1140    if (yOutput) {
1141      for (Int_t i=0; i<4; i++) iStripY   += static_cast<int>( fCoordY[i] << i );
1142       fStripY11 = iStripY;
1143       fTrigY    = fCoordY[4];
1144    }
1145   
1146 //   cout << " Local Trigger " << " " << fNumber << " " << 
1147 //       xOutput << " " << yOutput << " " << fDev << " " << fStripX11 << " " <<
1148 //       fTrigY << " " << fStripY11 << "\n";
1149
1150    if (xOutput && yOutput){ // trigger in X and Y
1151        fOutput =1;
1152
1153        Int_t sign = 0;
1154        if ( !fMinDev[4] &&  deviation ) sign=-1;
1155        if ( !fMinDev[4] && !deviation ) sign= 0;
1156        if (  fMinDev[4] == 1 )          sign=+1;    
1157        
1158        deviation *= sign; 
1159        
1160 //    calculate deviation in [0;+30]
1161        deviation += 15;
1162        
1163 //    GET LUT OUTPUT FOR icirc/istripX1/deviation/istripY
1164        fLUT->GetLutOutput(GetNumber(), fStripX11, deviation, fStripY11, fLutLpt, fLutHpt);
1165    }
1166
1167    fResponse = fLutLpt[0]              + 
1168        static_cast<int>(fLutLpt[1]<<1) + 
1169        static_cast<int>(fLutHpt[0]<<2) + 
1170        static_cast<int>(fLutHpt[1]<<3);  
1171
1172 }
1173
1174 //___________________________________________
1175 Int_t AliMUONLocalTriggerBoard::GetI() const
1176 {
1177 /// old numbering
1178 ///
1179
1180    const Int_t kMaxfields = 2; char **fields = new char*[kMaxfields];
1181
1182    for (Int_t i = 0; i < kMaxfields; i++) 
1183      fields[i] = new char[1];
1184
1185    char s[100]; strncpy(s, GetName(), 99);
1186
1187    Int_t numlines = 0;
1188
1189    for (char *token = strtok(s, "B");
1190         token != NULL;
1191         token = strtok(NULL, " "))
1192    {
1193       delete [] fields[numlines];
1194       fields[numlines] = new char[strlen(token)+1];
1195       strcpy(fields[numlines++], token);
1196    }
1197
1198    TString l(fields[0]);
1199
1200    char copy = l[0];
1201
1202    Int_t lL = atoi(&l[4]), cC = atoi(&l[2]), sS = (copy=='R') ? +1 : -1;
1203
1204    const char *b[4] = {"12", "34", "56", "78"};
1205
1206    Int_t ib = 0;
1207
1208    for (Int_t i=0; i<4; i++) if (!strcmp(fields[1],b[i])) {ib = i; break;} ib++;
1209
1210 // lL=1 ON TOP
1211    lL -= 9; lL = abs(lL); lL++;
1212
1213    Int_t code = 100 * lL + 10 * cC + ib;
1214
1215    code *= sS;
1216
1217    Int_t ic = 0;
1218
1219    for (Int_t i=0; i<234; i++) if (fgkCircuitId[i] == code) {ic = i; break;}
1220    
1221    delete [] fields;
1222
1223    return ic;
1224 }
1225
1226 //___________________________________________
1227 void AliMUONLocalTriggerBoard::Mask(Int_t index, UShort_t mask)
1228 {
1229 /// set mask
1230 ///
1231   if ( index >= 0 && index < 2*4 )
1232   {
1233     Int_t i = index/4;
1234     Int_t j = index%4;
1235     fMask[i][j]=mask;
1236   }
1237   else
1238   {
1239     AliError(Form("Index %d out of bounds (max %d)",index,8));
1240   }
1241 }
1242
1243 //___________________________________________
1244 void AliMUONLocalTriggerBoard::Scan(Option_t *option) const
1245 {
1246 /// full dump
1247 ///
1248     TString op = option;
1249
1250    if (op.Contains("CONF")) Conf();
1251
1252    if (op.Contains("BITP")) Pattern();
1253
1254    if (op.Contains("RESPI")) Resp("I");
1255
1256    if (op.Contains("RESPF")) Resp("F"); 
1257
1258    if (op.Contains("RESPO")) Resp("O");
1259
1260    if (op.Contains("ALL"))
1261    {
1262       Conf();
1263       Pattern();
1264       Resp("I");
1265       Resp("F");
1266    }
1267 }
1268
1269 //___________________________________________
1270 void AliMUONLocalTriggerBoard::Conf() const
1271 {
1272 /// board switches
1273 ///
1274    cout << "Switch(" << GetName() << ")" 
1275         << " x2d = "           << GetSwitch(0) 
1276         << " x2m = "           << GetSwitch(1) 
1277         << " x2u = "           << GetSwitch(2) 
1278         << " OR[0] = "         << GetSwitch(3) 
1279         << " OR[1] = "         << GetSwitch(4) 
1280         << " EN-Y = "          << GetSwitch(5) 
1281         << " ZERO-ALLY-LSB = " << GetSwitch(6) 
1282         << " ZERO-down = "     << GetSwitch(7) 
1283         << " ZERO-middle = "   << GetSwitch(8) 
1284         << " ZERO-up = "       << GetSwitch(9) 
1285         << " trans. conn. "    << GetTC() 
1286         << " Slot = "          << fSlot 
1287         << endl;
1288 }
1289
1290 //___________________________________________
1291 void AliMUONLocalTriggerBoard::Resp(Option_t *option) const
1292 {
1293 /// board I/O
1294 ///
1295    TString op = option;
1296
1297    if (op.Contains("I"))
1298    {
1299 //    print Local trigger output before the LuT step
1300       printf("===============================================================\n");
1301       printf("-------- TRIGGER OUTPUT --------\n");
1302       printf("minDevStrip = ");
1303       for  (Int_t i=4; i>=0; i--) printf("%i",fMinDevStrip[i]);
1304       printf(" minDev = ");
1305       for  (Int_t i=4; i>=0; i--) printf("%i",fMinDev[i]);
1306       printf(" coordY = ");
1307       for  (Int_t i=4; i>=0; i--) printf("%i",fCoordY[i]); 
1308       printf(" \n");
1309    }
1310    if (op.Contains("O")){
1311      printf("-- Output --\n");
1312      printf("Coinc44 %i\n", fCoinc44);
1313      printf("StripX11 %i  StripY11 %i  Dev %i  TrigY %i\n", fStripX11, fStripY11, fDev, fTrigY);
1314      printf("LutLpt %i %i  LutHpt %i %i\n", fLutLpt[0], fLutLpt[1], fLutHpt[0], fLutHpt[1]);
1315      printf("Response %i\n", fOutput);
1316    }
1317
1318 }
1319
1320 //___________________________________________
1321 void AliMUONLocalTriggerBoard::Response()
1322 {
1323 /// algo
1324 ///
1325   if ( IsNull() ) {
1326     fMinDev[4] = 1;
1327     for  (Int_t i=4; i>=0; i--) fCoordY[i] = 1;
1328     return; // Do nothing if strip pattern is null
1329   }
1330
1331    Int_t xX1[16], xX2[16], xXX3[32], xXX4[32];
1332
1333    TBits x1(16), x2(16), x3(16), x4(16);
1334
1335    UShort_t xyv = 0;
1336
1337    xyv = fXY[0][0]; x1.Set(16,&xyv);
1338    xyv = fXY[0][1]; x2.Set(16,&xyv);
1339    xyv = fXY[0][2]; x3.Set(16,&xyv);
1340    xyv = fXY[0][3]; x4.Set(16,&xyv);
1341
1342    TBits x3u(16), x4u(16), x3d(16), x4d(16);
1343
1344    xyv = fXYU[0][2]; x3u.Set(16,&xyv);
1345    xyv = fXYU[0][3]; x4u.Set(16,&xyv);
1346
1347    xyv = fXYD[0][2]; x3d.Set(16,&xyv);
1348    xyv = fXYD[0][3]; x4d.Set(16,&xyv);
1349
1350    for (Int_t i=0;i<16;i++)
1351    {
1352       xX1[i] = x1[i];
1353       xX2[i] = x2[i];
1354       
1355       xXX3[i+8] = x3[i];
1356       xXX4[i+8] = x4[i];  
1357    }
1358
1359    for (Int_t i=0;i<8;i++)
1360    {
1361       xXX3[i] = x3d[i+8];
1362       xXX4[i] = x4d[i+8];
1363
1364       xXX3[i+24] = x3u[i];
1365       xXX4[i+24] = x4u[i];
1366    }
1367    
1368 //   Int_t coinc44 = 0;
1369    
1370    TrigX(xX1, xX2, xXX3, xXX4);   
1371
1372    Int_t yY1[16], yY2[16], yY3[16], yY4[16];
1373    
1374    Int_t yY3U[16], yY3D[16], yY4U[16], yY4D[16];
1375
1376    TBits y1(16), y2(16), y3(16), y4(16);
1377
1378    TBits y3u(16), y3d(16), y4u(16), y4d(16);
1379
1380    xyv = fXY[1][0]; y1.Set(16,&xyv);
1381    xyv = fXY[1][1]; y2.Set(16,&xyv);
1382    xyv = fXY[1][2]; y3.Set(16,&xyv);
1383    xyv = fXY[1][3]; y4.Set(16,&xyv);
1384
1385    xyv = fXYU[1][2]; y3u.Set(16,&xyv);
1386    xyv = fXYD[1][2]; y3d.Set(16,&xyv);
1387    xyv = fXYU[1][3]; y4u.Set(16,&xyv);
1388    xyv = fXYD[1][3]; y4d.Set(16,&xyv);
1389
1390    for (Int_t i=0;i<16;i++)
1391    {
1392       yY1[i] = y1[i];
1393       yY2[i] = y2[i];
1394       yY3[i] = y3[i];
1395       yY4[i] = y4[i];
1396       
1397       yY3U[i] = y3u[i];
1398       yY3D[i] = y3d[i];
1399       
1400       yY4U[i] = y4u[i];
1401       yY4D[i] = y4d[i];
1402    }
1403
1404    TrigY(yY1, yY2, yY3, yY4, yY3U, yY3D, yY4U, yY4D);
1405    
1406 // ASIGN fLutLpt, fLutHpt
1407    LocalTrigger();
1408 }
1409
1410 //___________________________________________
1411 Bool_t AliMUONLocalTriggerBoard::IsTrigY() const
1412 {
1413   /// Return the response of non-bending plane
1414   Int_t iStripY = 0;
1415   Bool_t output = kFALSE;
1416
1417   for (Int_t i=0; i<4; i++) iStripY   += static_cast<int>( fCoordY[i] << i );
1418
1419   if (fCoordY[4]==1 && iStripY==15) output=kFALSE; // no trigger in Y
1420   else output=kTRUE;                               // trigger in Y
1421    
1422   return output;
1423 }
1424
1425 //___________________________________________
1426 Bool_t AliMUONLocalTriggerBoard::IsTrigX() const
1427 {
1428   /// Return the response of bending plane
1429
1430   Int_t deviation = 0;
1431   Bool_t output = kFALSE;
1432
1433   for (Int_t i=0; i<4; i++) deviation += static_cast<int>( fMinDev[i] << i );
1434
1435   if (fMinDev[4]==1 && !deviation) output=kFALSE;  // no trigger in X
1436   else output=kTRUE;                               // trigger in X
1437
1438   return output;
1439 }
1440
1441 //___________________________________________
1442 Bool_t AliMUONLocalTriggerBoard::IsNull() const
1443 {
1444   /// Check if board has fired strips in the first station
1445   for (Int_t icath=0; icath<2; icath++){
1446     for(Int_t ich=0; ich<2; ich++){
1447       if ( fXY[icath][ich] ) return kFALSE;
1448     }
1449   }
1450   return kTRUE;
1451 }