]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliTPCAltroEmulator.cxx
Effective C++ + add RMS and MEAN to the analysis
[u/mrichter/AliRoot.git] / TPC / AliTPCAltroEmulator.cxx
1 /**             @file Altro.C
2  *      @brief The Altro class implements the Altro digital Chain in C++
3  *
4  *      This Class represents a C++ version of the ALTRO. For a complete Documentation of the Altro
5  *      Look at : http://ep-ed-alice-tpc.web.cern.ch/ep-ed-alice-tpc/altro_chip.htm\n
6  *      Due to the fact that the real ALTRO constantly samples in between the recorded events,
7  *      it has the knowledge on what happened in the period. This affects the BSL1, TCF and BSL2 module.
8  *      In the BSL1 the ALTRO follows slow baseline drifts e.g. temperature change, the TCF has a infinite
9  *      (IIR Filter) memory of "old samples" i.e. a cluster at the start of a readout cycle will be treated
10  *      differently, and the BSL2 has a 8 step pipeline. The ALTRO Class can't emulate this behavior,
11  *      since the data is not recorded.\n
12  *
13  *      @author Roland Bramm
14  *      @version $LastChangedRevision: 688 $
15  *      @date    $LastChangedDate: 2005-12-16 14:07:11 +0100 (Fri, 16 Dec 2005) $
16  *
17  *      \verbinclude Altro/Altro.C.log
18  *
19  */
20 /////////////////////////////////////////////////////////////////////////////////////////////////////
21 //     Class for emulation of the ALTRO chip (Altro digital Chain) in C++                          //
22 //     Author: Roland Bramm                                                                        //
23 //                                                                                                 //
24 //     NOTE: This class has been modified to be conform with the coding conventions of the         //
25 //           ALICE Off-line Project. Keywords for setting the mode of BSC1 were modified           //
26 //           and are shown in the header file ...                                                  //
27 //                           Stefan Rossegger, 8th february 2008                                   //
28 /////////////////////////////////////////////////////////////////////////////////////////////////////
29
30 #include "AliTPCAltroEmulator.h"
31
32 /**     @brief Consturctor of Altro Class
33  *
34  *      Consturctor of Altro Class, some variables are set.\n
35  *      The input Data is altered, so after running the complete emulation you have the
36  *      Altro Processed Data in the Channel Pointer.\n
37  *
38  *      @param timebins an <tt> int </tt> sets the length of the input Data (Channel)
39  *      @param Channel an <tt> short* </tt> Pointer to a 1d short Array with the input Data
40  */
41
42 AliTPCAltroEmulator::AliTPCAltroEmulator(int timebins, short* Channel) : 
43   TNamed(),
44   ftimebins(timebins),
45 //  fChannelIn(Channel),
46   fChannelShort(Channel), 
47   fADCkeep(0),     
48   fOnBSL1(0), 
49   fOnTCF(0),  
50   fOnBSL2(0), 
51   fOnClip(0), 
52   fOnZSU(0),  
53
54   fConfiguredAltro(0),   // ConfiguredAltro
55   fConfiguredBSL1(0),    // ConfiguredBSL1
56   fConfiguredTCF(0),     // ConfiguredTCF
57   fConfiguredBSL2(0),    // ConfiguredBSL2
58   fConfiguredZSU(0),     // ConfiguredZSU
59   fBSL1mode(0),          // BSL1mode
60   fBSL1ValuePeDestal(0), // BSL1ValuePeDestal
61   fBSL1PedestalMem(0),  // BSL1PedestalMem
62   fBSL1polarity(0),      // BSL1polarity
63
64   fTCFK1(0), // K1
65   fTCFK2(0), // K2
66   fTCFK3(0), // K3
67   fTCFL1(0), // L1
68   fTCFL2(0), // L2
69   fTCFL3(0), // L3
70
71   fTCFK1Int(0), // K1Int
72   fTCFK2Int(0), // K2Int
73   fTCFK3Int(0), // K3Int
74   fTCFL1Int(0), // L1Int
75   fTCFL2Int(0), // L2Int
76   fTCFL3Int(0), // L3Int
77
78   fBSL2HighThreshold(0), // BSL2HighThreshold
79   fBSL2LowThreshold(0),  // BSL2LowThreshold
80   fBSL2Offset(0),        // BSL2Offset
81   fBSL2Presamples(0),    // BSL2Presamples(0),
82   fBSL2Postsamples(0),   // BSL2Postsamples
83
84   fZSUThreshold(0),      // ZSUThreshold
85
86   fZSUMinSamplesaboveThreshold(0), // ZSUMinSamplesaboveThreshold
87   fZSUPresamples(0),     // ZSUPresamples
88   fZSUPostsamples(0)     // ZSUPostsamples
89
90 {
91   //
92   // Constructor of Altro Class
93   //
94   /*
95   ftimebins = timebins;
96   
97   fChannelShort = Channel;
98   
99   fOnBSL1 = 0;
100   fOnTCF = 0;
101   fOnBSL2 = 0;
102   fOnClip = 0;
103   fOnZSU = 0;
104   
105   fConfiguredAltro = 0;
106   fConfiguredBSL1 = 0;
107   fConfiguredTCF = 0;
108   fConfiguredBSL2 = 0;
109   fConfiguredZSU = 0;
110   */
111 }
112
113
114 AliTPCAltroEmulator::AliTPCAltroEmulator(const AliTPCAltroEmulator &altro):
115   TNamed(),
116   ftimebins(altro.ftimebins),
117 //  fChannelIn(Channel),
118   fChannelShort(altro.fChannelShort), 
119   fADCkeep(0),     
120   fOnBSL1(0), 
121   fOnTCF(0),  
122   fOnBSL2(0), 
123   fOnClip(0), 
124   fOnZSU(0),  
125
126   fConfiguredAltro(0),   // ConfiguredAltro
127   fConfiguredBSL1(0),    // ConfiguredBSL1
128   fConfiguredTCF(0),     // ConfiguredTCF
129   fConfiguredBSL2(0),    // ConfiguredBSL2
130   fConfiguredZSU(0),     // ConfiguredZSU
131   fBSL1mode(0),          // BSL1mode
132   fBSL1ValuePeDestal(0), // BSL1ValuePeDestal
133   fBSL1PedestalMem(0),  // BSL1PedestalMem
134   fBSL1polarity(0),      // BSL1polarity
135
136   fTCFK1(0), // K1
137   fTCFK2(0), // K2
138   fTCFK3(0), // K3
139   fTCFL1(0), // L1
140   fTCFL2(0), // L2
141   fTCFL3(0), // L3
142
143   fTCFK1Int(0), // K1Int
144   fTCFK2Int(0), // K2Int
145   fTCFK3Int(0), // K3Int
146   fTCFL1Int(0), // L1Int
147   fTCFL2Int(0), // L2Int
148   fTCFL3Int(0), // L3Int
149
150   fBSL2HighThreshold(0), // BSL2HighThreshold
151   fBSL2LowThreshold(0),  // BSL2LowThreshold
152   fBSL2Offset(0),        // BSL2Offset
153   fBSL2Presamples(0),    // BSL2Presamples(0),
154   fBSL2Postsamples(0),   // BSL2Postsamples
155
156   fZSUThreshold(0),      // ZSUThreshold
157
158   fZSUMinSamplesaboveThreshold(0), // ZSUMinSamplesaboveThreshold
159   fZSUPresamples(0),     // ZSUPresamples
160   fZSUPostsamples(0)     // ZSUPostsamples
161
162 {
163   //
164   // copy constructor of Altro Class
165   //
166
167 }
168
169
170 /**     @brief Destructor of Altro Class
171  *
172  *      Destructor of Altro Class\n
173  */
174 AliTPCAltroEmulator::~AliTPCAltroEmulator(){
175   //
176   // Destructor of Altro Class
177   //
178
179   if(fConfiguredZSU == 1)
180     delete fADCkeep;
181 }
182
183 //_____________________________________________________________________________
184 AliTPCAltroEmulator& AliTPCAltroEmulator::operator = (const AliTPCAltroEmulator &source)
185 {
186   //
187   // AliTPCAltroEmulator assignment operator
188   //
189
190   if (&source == this) return *this;
191   new (this) AliTPCAltroEmulator(source);
192
193   return *this;
194
195 }
196
197
198
199 /**  @brief Configures which modules of the Altro should be on.
200  *
201  *      Configures which modules of the Altro should be on. Each of the modules
202  *      which are configured to be on, have to be configured later before running
203  *      the emulation!\n
204  *
205  *      @param ONBaselineCorrection1 an <tt> int </tt> Switch (0,1) to turn on the Base Line Correction 1 (BSL1) Module
206  *      @param ONTailcancellation an <tt> int </tt> Switch (0,1) to turn on the Tail Cancellation Filter (TCF) Module
207  *      @param ONBaselineCorrection2 an <tt> int </tt> Switch (0,1) to turn on the Moving Average Filter (BSL2) Module
208  *      @param ONClipping an <tt> int </tt> Switch (0,1) to turn on the Clipping Module. This is not possible in the real Altro, there it is always on.
209  *      @param ONZerosuppression an <tt> int </tt> Switch (0,1) to turn on the Zero Suppression (ZSU) Module
210  *      @param ONDataFormatting an <tt> int </tt> Switch (0,1) to turn on the Data Formatting on (not implemented)
211  */
212 void AliTPCAltroEmulator::ConfigAltro(int ONBaselineCorrection1, int ONTailcancellation, int ONBaselineCorrection2, int ONClipping, int ONZerosuppression, int ONDataFormatting){
213   //
214   // Configures which modules of the Altro should be on 
215   //
216   fOnBSL1 = InRange(ONBaselineCorrection1,0,1,"AliTPCAltroEmulator::ConfigAltro","ONBaselineCorrection1");
217   fOnTCF  = InRange(ONTailcancellation,0,1,"AliTPCAltroEmulator::ConfigAltro","ONTailcancellation");
218   fOnBSL2 = InRange(ONBaselineCorrection2,0,1,"AliTPCAltroEmulator::ConfigAltro","ONBaselineCorrection2");
219   fOnClip = InRange(ONClipping,0,1,"AliTPCAltroEmulator::ConfigAltro","ONClipping");
220   fOnZSU = InRange(ONZerosuppression,0,1,"AliTPCAltroEmulator::ConfigAltro","ONZerosuppression");
221   ONDataFormatting; // does not have to be checked
222   fConfiguredAltro = 1;
223 }
224
225 /**  @brief Configures the Base Line Correction 1 (BSL1) Module
226  *
227  *      Configures the Base Line Correction 1 (BSL1) Module. You dont have to build a proper pedestalMemory
228  *      array, a pointer of the correct type is enough, of course you are not allowed to use Basline
229  *      Correction Modes which need then the array ...\n
230  *      All configurable values are "Range checked" and if out of the Range set to the nearest extreme.
231  *      So the Emulation will work, but the result is maybe not the expected one.
232  *
233  *      @param mode an <tt> int </tt> sets the mode of the Baseline Correction. See the Altro manual for a description
234  *      @param ValuePeDestal an <tt> int </tt> this is the baseline of the Channel.
235  *      @param PedestalMem an <tt> *int </tt> Pointer to a 1d short Array with the pedestal memory Data
236  *      @param polarity an <tt> int </tt> Switch (0,1) for the polarity
237  */
238 void AliTPCAltroEmulator::ConfigBaselineCorrection1(int mode, int ValuePeDestal, int *PedestalMem, int polarity){
239   //
240   // Configures the Base Line Correction 1 (BSL1) Module
241   //
242   fBSL1mode          = InRange(mode,0,10,"AliTPCAltroEmulator::ConfigBaselineCorrection1","mode");
243   fBSL1ValuePeDestal = InRange(ValuePeDestal,0,1023,"AliTPCAltroEmulator::BaselineCorrection1","ValuePeDestal");
244   fBSL1PedestalMem = PedestalMem;
245   fBSL1polarity = InRange(polarity,0,1,"AliTPCAltroEmulator::BaselineCorrection1","polarity");
246   fConfiguredBSL1 = 1;
247 }
248
249 /**  @brief Configures the Tail Cancellation Filter (TCF) Module
250  *
251  *      Configures the Tail Cancellation Filter (TCF) Module. You have to set the coefficients in the
252  *      Integer version.\n
253  *      To convert from int to float use (int)*(pow(2,-16)-1)
254  *      To convert from float to int usw (float)*(pow(2,16)-1)
255  *      All configurable values are "Range checked" and if out of the Range set to the nearest extreme.
256  *      So the Emulation will work, but the result is maybe not the expected one.
257  *
258  *      @param K1 an <tt> int </tt> sets the K1 coeeficient of the TCF
259  *      @param K2 an <tt> int </tt> sets the K2 coeeficient of the TCF
260  *      @param K3 an <tt> int </tt> sets the K3 coeeficient of the TCF
261  *      @param L1 an <tt> int </tt> sets the L1 coeeficient of the TCF
262  *      @param L2 an <tt> int </tt> sets the L2 coeeficient of the TCF
263  *      @param L3 an <tt> int </tt> sets the L3 coeeficient of the TCF
264  */
265 void AliTPCAltroEmulator::ConfigTailCancellationFilter(int K1, int K2, int K3, int L1, int L2, int L3){
266   //
267   // Configures the Tail Cancellation Filter (TCF) Module
268   //
269   // conf from int to fp:  (int)*(pow(2,-16)-1)
270   //             backway:  (float)*(pow(2,16)-1)
271   fTCFK1Int = InRange(K1,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K1");
272   fTCFK2Int = InRange(K2,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K2");
273   fTCFK3Int = InRange(K3,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","K3");
274   
275   fTCFL1Int = InRange(L1,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L1");
276   fTCFL2Int = InRange(L2,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L2");
277   fTCFL3Int = InRange(L3,0,65535,"AliTPCAltroEmulator::ConfigTailCancellationFilter","L3");
278   fConfiguredTCF = 1;
279 }
280
281 /**  @brief Configures the Moving Average Filter (BSL2) Module
282  *
283  *      Configures the Moving Average Filter (BSL2) Module.
284  *      All configurable values are "Range checked" and if out of the Range set to the nearest extreme.
285  *      So the Emulation will work, but the result is maybe not the expected one.
286  *
287  *      @param HighThreshold an <tt> int </tt> sets the high Threshold
288  *      @param LowThreshold an <tt> int </tt> sets the low Theshold
289  *      @param Offset an <tt> int </tt> sets the the offset which is added to the Signal
290  *      @param Presamples an <tt> int </tt> sets the number of pre samples excluded from the moving average caclulation
291  *      @param Postsamples an <tt> int </tt> sets the number of post samples excluded from the moving average caclulation
292  */
293 void AliTPCAltroEmulator::ConfigBaselineCorrection2(int HighThreshold, int LowThreshold, int Offset, int Presamples, int Postsamples){
294   //
295   // Configures the Moving Average Filter (BSL2) Module
296   //
297   fBSL2HighThreshold = InRange(HighThreshold,0,1023,"AliTPCAltroEmulator::ConfigBaselineCorrection2","HighThreshold");
298   fBSL2LowThreshold  = InRange(LowThreshold,0,1023,"AliTPCAltroEmulator::ConfigBaselineCorrection2","LowThreshold");
299   fBSL2Offset        = InRange(Offset,0,1023,"AliTPCAltroEmulator::ConfigBaselineCorrection2","Offset");
300   fBSL2Presamples    = InRange(Presamples,0,3,"AliTPCAltroEmulator::ConfigBaselineCorrection2","Presamples");
301   fBSL2Postsamples   = InRange(Postsamples,0,15,"AliTPCAltroEmulator::ConfigBaselineCorrection2","Postsamples");
302   fConfiguredBSL2 = 1;
303 }
304
305 /**  @brief Configures the Zero Suppression Module (ZSU)
306  *
307  *      Configures the Zero Suppression Module (ZSU).
308  *      All configurable values are "Range checked" and if out of the Range set to the nearest extreme.
309  *      So the Emulation will work, but the result is maybe not the expected one.
310  *
311  *      @param Threshold an <tt> int </tt> sets the Threshold
312  *      @param MinSamplesaboveThreshold an <tt> int </tt> sets the minimum number of samples which have to be greater than the threshold
313  *      @param Presamples an <tt> int </tt> sets the number of pre samples which are kept
314  *      @param Postsamples an <tt> int </tt> sets the number of post samples which are kept
315  */
316 void AliTPCAltroEmulator::ConfigZerosuppression(int Threshold, int MinSamplesaboveThreshold, int Presamples, int Postsamples){
317   //
318   // Configures the Zero Suppression Module (ZSU)
319   //
320   fZSUThreshold                = InRange(Threshold,0,1023,"AliTPCAltroEmulator::BaselineCorrection1","Threshold");
321   fZSUMinSamplesaboveThreshold = InRange(MinSamplesaboveThreshold,1,3,"AliTPCAltroEmulator::BaselineCorrection1","MinSamplesaboveThreshold");
322   fZSUPresamples               = InRange(Presamples,0,3,"AliTPCAltroEmulator::BaselineCorrection1","Presamples");
323   fZSUPostsamples              = InRange(Postsamples,0,7,"AliTPCAltroEmulator::BaselineCorrection1","Postsamples");
324   fADCkeep = (short *)calloc(sizeof(short),ftimebins);
325   
326   for(int i = 0; i < ftimebins; i++){
327     fADCkeep[i] = 0;
328   }
329   fConfiguredZSU = 1;
330 }
331
332 /**  @brief Prints the set Parameters, if module is configured
333  *
334  *      Prints the set Parameters, if module is configured.
335  */
336 void AliTPCAltroEmulator::PrintParameters(){
337   //
338   // Prints the set Parameters, if module is configured
339   //
340   cout << "+-------------------------------------------+" << endl;
341   cout << "| Configured Parameters of the Altro Module |" << endl;
342   cout << "+-------------------------------------------+" << endl << endl;
343   
344   cout << "Parameters set in the Altro Modules:" << endl << endl;
345   cout << "ONBaselineCorrection1: " << fOnBSL1 << endl;
346   cout << "ONTailcancellation   : " << fOnTCF << endl;
347   cout << "ONBaselineCorrection2: " << fOnBSL2 << endl;
348   cout << "ONClipping           : " << fOnClip << endl;
349   cout << "ONZerosuppression    : " << fOnZSU << endl << endl << endl;
350   if(fConfiguredBSL1 == 1){
351     cout << "Parameters set in the BSL1 (Baseline Correction 1) Module:" << endl << endl;
352     cout << "mode                 : " << fBSL1mode << endl;
353     cout << "ValuePeDestal        : " << fBSL1ValuePeDestal << endl;
354     cout << "polarity             : " << fBSL1ValuePeDestal << endl << endl << endl;
355   }else{
356     cout << "BSL1 (Baseline Correction 1) Module not configured!" << endl << endl << endl;
357   }
358   if(fConfiguredTCF == 1){
359     cout << "Parameters set in the TCF (TailCancellation Filter) Module:" << endl << endl;
360     cout << "K1       (int|float) : " << fTCFK1Int << " | " << fTCFK1Int/(float)((1<<16)-1) << endl;
361     cout << "K2       (int|float) : " << fTCFK2Int << " | " << fTCFK2Int/(float)((1<<16)-1) << endl;
362     cout << "K3       (int|float) : " << fTCFK3Int << " | " << fTCFK3Int/(float)((1<<16)-1) << endl;
363     cout << "L1       (int|float) : " << fTCFL1Int << " | " << fTCFL1Int/(float)((1<<16)-1) << endl;
364     cout << "L2       (int|float) : " << fTCFL2Int << " | " << fTCFL2Int/(float)((1<<16)-1) << endl;
365     cout << "L3       (int|float) : " << fTCFL3Int << " | " << fTCFL3Int/(float)((1<<16)-1) << endl << endl << endl;
366   }else{
367     cout << "TCF (TailCancellation Filter) Module not configured!" << endl << endl << endl;
368   }
369   if(fConfiguredBSL2 == 1){
370     cout << "Parameters set in the BSL2 (Baseline Correction 2) Module:" << endl << endl;
371     cout << "HighThreshold        : " << fBSL2HighThreshold << endl;
372     cout << "LowThreshold         : " << fBSL2LowThreshold << endl;
373     cout << "Offset               : " << fBSL2Offset << endl;
374     cout << "Presamples           : " << fBSL2Presamples << endl;
375     cout << "Postsamples          : " << fBSL2Postsamples << endl << endl << endl;
376   }else{
377     cout << "BSL2 (Baseline Correction 2) Module not configured!" << endl << endl << endl;
378   }
379   if(fConfiguredZSU == 1){
380     cout << "Parameters set in the ZSU (Zero Suppression Unit) Module:" << endl << endl;
381     cout << "Threshold            : " << fZSUThreshold << endl;
382     cout << "MinSampaboveThreshold: " << fZSUMinSamplesaboveThreshold << endl;
383     cout << "Presamples           : " << fZSUPresamples << endl;
384     cout << "Postsamples          : " << fZSUPostsamples << endl << endl << endl;
385   }else{
386     cout << "ZSU (Zero Suppression Unit) Module not configured!" << endl << endl << endl;
387   }
388 }
389
390 /**  @brief Runs the emulation of all configured Modules.
391  *
392  *      Runs the emulation of all configured Modules. This changes then the content of the
393  *      input Array
394  */
395 void AliTPCAltroEmulator::RunEmulation(){
396   //
397   // Runs the emulation of all configured Modules.
398   //
399
400   //cout << "AliTPCAltroEmulator::RunEmulation | start" << endl;
401   if(fConfiguredAltro == 0){
402     cout << "ERROR cant run Altro Emulation because not configured" << endl;
403     return;
404   }
405   
406   //cout << "AliTPCAltroEmulator::RunEmulation | start BSL1 on: " << fOnBSL1 << " configures: " << fConfiguredBSL1 << endl;
407   if(fOnBSL1 == 1){
408     if(fConfiguredBSL1 == 1){
409       BaselineCorrection1(fBSL1mode, fBSL1ValuePeDestal, fBSL1PedestalMem, fBSL1polarity);
410     }else{
411       cout << "ERROR cant run Baseline Correction 1 because not configured" << endl;
412       return;
413     }
414   }
415   
416   //cout << "AliTPCAltroEmulator::RunEmulation | start TCF on: " << fOnTCF << " configures: " << fConfiguredTCF << endl;
417   if(fOnTCF == 1){
418     if(fConfiguredTCF == 1){
419       TailCancellationFilterFixedPoint(fTCFK1Int, fTCFK2Int, fTCFK3Int, fTCFL1Int, fTCFL2Int, fTCFL3Int);
420     }else{
421       cout << "ERROR cant run Tail Cancellation Filter because not configured" << endl;
422       return;
423     }
424   }
425   
426   //cout << "AliTPCAltroEmulator::RunEmulation | start BSL2 on: " << fOnBSL2 << " configures: " << fConfiguredBSL2 << endl;
427   if(fOnBSL2 == 1){
428     if(fConfiguredBSL2 == 1){
429       BaselineCorrection2RTL(fBSL2HighThreshold, fBSL2LowThreshold, fBSL2Offset, fBSL2Presamples, fBSL2Postsamples);
430     }else{
431       cout << "ERROR cant run Baseline Correction 2 because not configured" << endl;
432       return;
433     }
434   }
435   //cout << "AliTPCAltroEmulator::RunEmulation | start CLIP on: " << fOnClip << endl;
436   if(fOnClip == 1){
437     Clipping();
438   }
439   //cout << "AliTPCAltroEmulator::RunEmulation | start ZSU on: " << fOnZSU << " configures: " << fConfiguredZSU << endl;
440   if(fOnZSU == 1){
441     if(fConfiguredZSU == 1){
442       Zerosuppression(fZSUThreshold,fZSUMinSamplesaboveThreshold,fZSUPresamples,fZSUPostsamples);
443     }else{
444       cout << "ERROR cant run Zero Suppression Unit because not configured" << endl;
445       return;
446     }
447   }
448 }
449
450 void AliTPCAltroEmulator::BaselineCorrection1(int mode, int ValuePeDestal, int *PedestalMem, int polarity){
451   //
452   // BaselineCorrection1
453   //
454
455   //VPD == 0 !!
456   int fixedPeDestal = 0;
457   
458   if(polarity ==1){
459     for(int i = 0; i < ftimebins; i++){
460       fChannelShort[i]  = 1023 - fChannelShort[i];
461     }
462   }
463   
464   switch(mode) {
465   case kDINxFPD:
466     for(int i = 0; i < ftimebins; i++)
467       fChannelShort[i]  = fChannelShort[i]  - fixedPeDestal;
468     break;
469   case kDINxFT:
470     for(int i = 0; i < ftimebins; i++)
471       fChannelShort[i]  = fChannelShort[i]  - PedestalMem[i];
472     break;
473   case kDINxFDIN:
474     for(int i = 0; i < ftimebins; i++)
475       fChannelShort[i]  = fChannelShort[i]  - PedestalMem[ fChannelShort[i] ];
476     break;
477   case kDINxFDINxVPD:
478     for(int i = 0; i < ftimebins; i++)
479       fChannelShort[i]  = fChannelShort[i]  - PedestalMem[ fChannelShort[i] - ValuePeDestal];
480     break;
481   case kDINxVPDxFPD:
482     for(int i = 0; i < ftimebins; i++)
483       fChannelShort[i]  = fChannelShort[i]  - ValuePeDestal - fixedPeDestal;
484     break;
485   case kDINxVPDxFT:
486     for(int i = 0; i < ftimebins; i++)
487       fChannelShort[i]  = fChannelShort[i]  - ValuePeDestal - PedestalMem[i];
488     break;
489   case kDINxVPDxFDIN:
490     for(int i = 0; i < ftimebins; i++)
491       fChannelShort[i]  = fChannelShort[i]  - ValuePeDestal - PedestalMem[ fChannelShort[i] ];
492     break;
493   case kDINxVPDxFDINxVPD:
494     for(int i = 0; i < ftimebins; i++)
495       fChannelShort[i]  = fChannelShort[i]  - ValuePeDestal - PedestalMem[ fChannelShort[i] - ValuePeDestal ];
496     break;
497   case kFDINxFPD:
498     for(int i = 0; i < ftimebins; i++)
499       fChannelShort[i]  = PedestalMem[ fChannelShort[i] ] - fixedPeDestal;
500     break;
501   case kFDINxVPDxFPD:
502     for(int i = 0; i < ftimebins; i++)
503       fChannelShort[i]  = PedestalMem[ fChannelShort[i] - ValuePeDestal ] - fixedPeDestal;
504     break;
505   case kFTxFPD:
506     for(int i = 0; i < ftimebins; i++)
507       fChannelShort[i]  = PedestalMem[i] - fixedPeDestal;
508     break;
509   }
510 }
511
512 int AliTPCAltroEmulator::Multiply36(int P, int N){
513   //
514   // multiply function to emulate the 36 bit fixed point multiplication of the Altro.
515   //
516   long long retval =0;
517   long long temp = 0;
518   long long vAX = 0;
519   temp = (long long)P*(long long)N;
520   vAX = (( Mask(temp,35,18) + ((long long)(-P)<<18) ) + Mask(temp,17,0));
521   if ( Maskandshift(N,17,17) == 1){
522     retval = ((Maskandshift(vAX,35,35)<<17) + Maskandshift(vAX,32,16));
523   }else{
524     retval = Maskandshift(temp,32,16);
525   }
526   return retval;
527 }
528 long long AliTPCAltroEmulator::Mask(long long in, int left, int right){
529   //
530   //
531   //
532   long long retval;
533   long long pattern;
534   long long length = abs(left - right)+1;
535   pattern = ((1<<length)-1)<<right;
536   retval = in&pattern;
537   return retval;
538 }
539
540 long long AliTPCAltroEmulator::Maskandshift(long long in, int left, int right){
541   //
542   //
543   //
544   long long retval;
545   long long pattern;
546   long long length = abs(left - right)+1;
547   pattern = ((1<<length)-1);
548   retval = (in>>right)&pattern;
549   return retval;
550 }
551
552 void AliTPCAltroEmulator::TailCancellationFilterFixedPoint(int K1, int K2, int K3, int L1, int L2, int L3){
553   //
554   // TailCancellationFilterFixedPoint
555   //
556   int c1n = 0, c2n = 0, c3n = 0;
557   int c1o = 0, c2o = 0, c3o = 0;
558   int d1  = 0, d2  = 0;
559   int dout = 0;
560   int din = 0;
561   int bit = 0;
562   for(int i = 0; i < ftimebins; i++){
563     din = fChannelShort[i];
564     
565     din = (din<<2);
566     c1n             = Mask( (Mask(din,17,0) + Multiply36(K1,Mask(c1o,17,0)) ) ,17,0);
567     d1              = Mask( (Mask(c1n,17,0) - Multiply36(L1,Mask(c1o,17,0)) ) ,17,0);
568     //d1              = Mask( (Mask(c1n,17,0) + Mask(~Multiply36(L1,Mask(c1o,17,0))+1,17,0) ) ,17,0);
569     
570     c2n             = Mask( (Mask(d1 ,17,0) + Multiply36(K2,Mask(c2o,17,0)) ) ,17,0);
571     d2              = Mask( (Mask(c2n,17,0) - Multiply36(L2,Mask(c2o,17,0)) ) ,17,0);
572     //d2              = Mask( (Mask(c2n,17,0) + Mask(~Multiply36(L2,Mask(c2o,17,0))+1,17,0) ) ,17,0);
573     
574     c3n             = Mask( (Mask(d2 ,17,0) + Multiply36(K3,Mask(c3o,17,0)) ) ,17,0);
575     dout            = Mask( (Mask(c3n,17,0) - Multiply36(L3,Mask(c3o,17,0)) ) ,17,0);
576     //dout            = Mask( (Mask(c3n,17,0) + Mask(~Multiply36(L3,Mask(c3o,17,0))+1,17,0) ) ,17,0);
577     
578     if( (Maskandshift(dout,2,2) == 1) || (Maskandshift(dout,1,1) == 1)){
579       bit = 1;
580     }else{
581       bit = 0;
582     }
583     
584     dout = ((dout>>3)<<1) + bit;
585     if(Maskandshift(dout,15,15) == 1){
586       //is needed to get the correct coding when getting negative results
587       dout = -Mask((-Mask(dout,9,0)),9,0);
588     }else{
589       dout = Mask(dout,9,0);
590     }
591     
592     fChannelShort[i] = (short) dout;
593     c1o = c1n;
594     c2o = c2n;
595     c3o = c3n;
596   }
597 }
598
599 void AliTPCAltroEmulator::BaselineCorrection2RTL(int HighThreshold, int LowThreshold, int Offset, int Presamples, int Postsamples){
600   //
601   // BaselineCorrection2RTL
602   //
603
604   //cout << "Altro::BaselineCorrection2RTL | HighThreshold: " << HighThreshold << " LowThreshold: " << LowThreshold << " Offset: " << Offset << " Presamples: " << Presamples << " Postsamples: " << Postsamples << endl;
605   //more or less direct "translation" of the hdl code.
606   //Input signals
607   int din;
608   int dout;
609   int edges[6]; // = Postsamples*4 + Presamples;
610   int offset = Offset;
611   int thrlo = LowThreshold;//called thr_mau[19] ...
612   int thrhi = HighThreshold;
613   
614   // Variables
615   int fOld[4]; //flag pipe
616   int fNew[4]; //flag pipe
617   int dOld[4]; //data pipe
618   int dNew[4]; //data pipe
619   int dxOld;
620   int dxNew;
621   int pstscnt; // Counter for Postsamples
622   int zOld[9]; // Filter stages
623   int zNew[9]; // Filter stages
624   int zxOld; //Accumulator stage
625   int zxNew; //Accumulator stage
626   int valcntOld; //Valid sample counter
627   int valcntNew = 0; //Valid sample counter
628   
629   int valid; //Valid flag
630   int fx; //postsample flag
631   //int s07; // differentiator result
632   int s8; // Acc + Diff result
633   int flag;
634   //int bsth; //baseline threshold
635   //int din_p; //Data input strictly positive
636   int bsl;
637   //int dx_bsls; // dx -bsl
638   //int dx_clip; // dxbsl clipped
639   //int bsl_of = 0;
640   
641   //initialisation
642   for(int i = 0; i < 9 ; i++)
643     zOld[i] = 0;
644   for(int i = 0; i < 4 ; i++){
645     fOld[i] = 0;
646     dOld[i] = 0;
647   }
648   dxOld= 0;
649   pstscnt = 0;
650   zxOld = 0;
651   valcntOld = 0;
652   valid = 0;
653   for(int i = 0; i < 2 ; i++){
654     edges[i] = (Presamples&(1<<i))>>i;
655   }
656   for(int i = 0; i < 4 ; i++){
657     edges[(3-i)+2] = (Postsamples&(1<<i))>>i;
658   }
659   /*cout << "edges :";
660     for(int i = 0; i < 6 ; i++)
661     cout << edges[i] << ":";
662     cout << " Presamples: " << Presamples << " Postsamples: " << Postsamples << endl;*/
663   
664   //Loop
665   //cout << "AliTPCAltroEmulator::BaselineCorrection2_RTL | starting Loop" << endl;
666   for(int timebin = -12; timebin < ftimebins+10; timebin++){
667     //cout << "AliTPCAltroEmulator::BaselineCorrection2_RTL | in Loop timebin: " << timebin << endl;
668     din = GetElement(fChannelShort,timebin);
669     
670     s8 = zxOld + (zOld[8] - zOld[0]);
671     
672     if(valid == 1)
673       bsl = s8>>3;// ...
674     else
675       bsl = 0;
676     
677     //assign flag = (din_p > thrhi) | (thrlo > din_p);  // Signal samples between thresholds
678     if( (din <= (bsl + thrhi)) && (din >= (bsl - thrlo)) )
679       flag = 0;
680     else
681       flag = 1;
682     
683     if(pstscnt == 0)
684       fx = 0;
685     else
686       fx = 1;
687     
688     if(valcntOld >= 12)
689       valid = 1;
690     else
691       valid = 0;
692     
693     fNew[3] = flag;
694     
695     if( (fOld[3] == 1) || ( (flag == 1) && ( (edges[0] == 1) || (edges[1] == 1) ) ) ) //f[2] =  f[3] | (flag&(edges[0]|edges[1]));
696       fNew[2] = 1;
697     else
698       fNew[2] = 0;
699     
700     if( (fOld[2] == 1) || ( (edges[1] == 1) && (flag == 1) ) ) //               f[1] =  f[2] | (edges[1] & flag);
701       fNew[1] = 1;
702     else
703       fNew[1] = 0;
704     
705     if( ( (fOld[1] == 1) || ( (flag == 1) && (edges[0] == 1) && (edges[1] == 1) )  || (fx==1) ) && (valid==1) ) //              f[0] = (f[1] | (edges[1] & edges[0] & flag) | fx) & valid;
706       fNew[0] = 1;
707     else
708       fNew[0] = 0;
709     
710     dxNew = dOld[0];
711     for(int i = 0; i < 3; i++)
712       dNew[i] = dOld[i+1];
713     dNew[3] = din;
714     
715     if( (fOld[1]==1) && (fOld[2]==0) )
716       pstscnt = Postsamples;
717     else if(fx == 1)
718       pstscnt--;
719     
720     if(fOld[0] == 0){
721       if(valid == 0)
722         valcntNew =  ++valcntOld;
723       
724       zxNew = s8;
725       for(int i = 0; i < 8; i++)
726         zNew[i] = zOld[i+1];
727       zNew[8] = dOld[0];
728     }else{
729       zxNew = zxOld;
730       for(int i = 0; i < 9; i++)
731         zNew[i] = zOld[i];
732     }
733     dout = dxOld - (bsl - offset);
734     //if(dout <0)
735     //  dout = 0;
736     
737     SetElement(fChannelShort,timebin-5,(short)dout);
738     //sim clockschange
739     for(int i = 0; i < 9 ; i++)
740       zOld[i] = zNew[i];
741     zxOld = zxNew;
742     for(int i = 0; i < 4 ; i++){
743       fOld[i] = fNew[i];
744       dOld[i] = dNew[i];
745     }
746     dxOld = dxNew;
747     valcntOld = valcntNew;
748   }
749 }
750
751 void AliTPCAltroEmulator::Clipping(){ 
752   //
753   // implement if no BC2 clipping has to run
754   //
755   for(int i = 0; i < ftimebins; i++){
756     if(fChannelShort[i] < 0)
757       fChannelShort[i] = 0;
758   }
759 }
760
761 void AliTPCAltroEmulator::Zerosuppression(int Threshold, int MinSamplesaboveThreshold, int Presamples, int Postsamples){
762   //
763   // add again altro feature
764   //
765
766   //TODO: Implement "Altro zsu merging"
767   //int Postsamplecounter = 0;
768   //int setPostsample = 0;
769   for(int i = 0; i < ftimebins; i++){
770     if(fChannelShort[i] >= Threshold){
771       fADCkeep[i] = 1;
772     }
773   }
774   
775   int startofclustersequence = -1;
776   int endofClustersInSequence = -1;
777   
778   for(int i = 0; i < ftimebins; i++){
779     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i-1) == 0) ){
780       startofclustersequence = i;
781     }
782     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i+1) == 0) ){
783       endofClustersInSequence = i;
784     }
785     //cout << i << " startofclustersequence: " << startofclustersequence << " endofClustersInSequence: " << endofClustersInSequence;
786     if( (startofclustersequence != -1) && (endofClustersInSequence != -1) ){
787       //cout << " found! " <<  (endofClustersInSequence - startofclustersequence + 1);
788       if ( (endofClustersInSequence - startofclustersequence + 1) < MinSamplesaboveThreshold ){
789         for(int j = startofclustersequence; j <= endofClustersInSequence ; j++){
790           fADCkeep[j] = 0;
791         }
792       }
793       startofclustersequence = -1;
794       endofClustersInSequence = -1;
795     }
796     //cout << endl;
797   }
798   
799   /*for(int i = 0; i < ftimebins; i++){
800     if( (GetElement(fADCkeep,i-1) == 1) && (GetElement(fADCkeep,i) == 0) && (GetElement(fADCkeep,i+1) == 1) ){
801     SetElement(fADCkeep,i,1);
802     }
803     }*/
804   
805   for(int i = 0; i < ftimebins; i++){
806     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i-1) == 0) ){
807       for(int j = i-Presamples ; j <= i; j++){
808         SetElement(fADCkeep,j,1);
809       }
810     }
811   }
812   for(int i = ftimebins; i >= 0; i--){
813     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i+1) == 0) ){
814       for(int j = i ; j <= i+Postsamples; j++){
815         SetElement(fADCkeep,j,1);
816       }
817     }
818   }
819   /*cout << " Postsamplecounter: " << Postsamplecounter;
820     for(int j = i+1 ; j <= i+Postsamples; j++){
821     SetElement(fADCkeep,j,1);
822     i+=Postsamples;
823     }
824     cout << endl;
825     }
826     cout << i << " ADCK: " << GetElement(fADCkeep,i);
827     cout << " Postsam: " << Postsamplecounter << " ADCK: " << GetElement(fADCkeep,i);*/
828   
829   for(int i = 0; i < ftimebins; i++){
830     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i+1) == 0) && ( (GetElement(fADCkeep,i+3) == 1) || (GetElement(fADCkeep,i+2) == 1) ) ){
831       SetElement(fADCkeep,i+1,1);
832       SetElement(fADCkeep,i+2,1);
833     }
834   }
835 }
836
837 /**  @brief formats the data like the ALTRO. Result is a 64 bit array
838  *
839  *      formats the data like the ALTRO. Result is a 64 bit array
840  *
841  */
842
843 void AliTPCAltroEmulator::DataFormater(){
844   //
845   // formats the data like the ALTRO. Result is a 64 bit array
846   //
847
848
849 }
850
851
852 /**  @brief calculates the compression out of the bitmask
853  *
854  *      calculates the compression out of the bitmask with the set adc values
855  *
856  *      @return \c float consisting of the compression factor
857  */
858 float AliTPCAltroEmulator::CalculateCompression(){
859   //
860   // calculates the compression out of the bitmask
861   //
862
863   // calculation is based on altro 10 bit words ..
864   int sample = 0;
865   int cluster = 0;
866   int data = 0;
867   float retval = 0.0;
868   
869   for(int i = 0; i < ftimebins; i++){
870     if(fADCkeep[i] == 1){
871       sample++;
872     }
873     if( (fADCkeep[i] == 1) && (GetElement(fADCkeep,i+1) == 0) ){
874       cluster++;
875     }
876   }
877   data = sample + cluster*2;
878   data = data + data%4 + 4;
879   if(data >0){
880     retval = ftimebins / (float)data;//num of timebins is equal to max number of samples
881   }else{
882     retval = 1.0;
883   }
884   return retval;
885 }
886
887 short AliTPCAltroEmulator::GetElement(short* Array,int index){
888   //
889   //
890   //
891   if (index < 0)
892     return 0;
893   else if(index >= ftimebins)
894     return 0;
895   else
896     return Array[index];
897 }
898
899 void AliTPCAltroEmulator::SetElement(short* Array,int index,short value){
900   //
901   //
902   //
903   if (index < 0)
904     return;
905   else if(index >= ftimebins)
906     return;
907   else
908     Array[index] = value;
909 }
910
911 int AliTPCAltroEmulator::InBand(int ADC,int bsl, int LowThreshold, int HighThreshold){
912   //
913   //
914   //
915   int fLow = bsl - LowThreshold;
916   int fHigh = bsl + HighThreshold;
917   if( (ADC <= fHigh) && (ADC >= fLow) )
918     return 1;
919   else
920     return 0;
921 }
922
923 int AliTPCAltroEmulator::InRange(int parameter,int Low,int High,char *Module,char *ParameterName){
924   //
925   //
926   //
927
928   char out[255];
929   int retval;
930   if(parameter > High){
931     sprintf(out,"Error | %s | Parameter %s is to big, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,High);
932     cout << out << endl;
933     retval = High;
934   }else if(parameter < Low){
935     sprintf(out,"Error | %s | Parameter %s is to small, has to be %d <= %s <= %d, is %d, now set to %d",Module,ParameterName,Low,ParameterName,High,parameter,Low);
936     cout << out << endl;
937     retval = Low;
938   }else{
939     retval = parameter;
940   }
941   return retval;
942 }
943
944 short AliTPCAltroEmulator::GetShortChannel(int i){
945   //
946   //
947   //
948   return GetElement(fChannelShort,i);
949 }
950
951 short AliTPCAltroEmulator::GetKeepChannel(int i){
952   //
953   //
954   //
955   return GetElement(fADCkeep,i);
956 }