]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSHandleDaSSD.cxx
New names for the PWG subdirectories
[u/mrichter/AliRoot.git] / ITS / AliITSHandleDaSSD.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-2009, 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 ///
20 /// This class provides ITS SSD data handling
21 /// used by DA. 
22 //  Author: Oleksandr Borysov
23 //  Date: 18/07/2008
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <cstring>
27 #include <fstream>
28 #include <new>
29 #include <Riostream.h> 
30 #include "AliITSHandleDaSSD.h"
31 //#include <math.h>
32 #include <limits.h>
33 #include "event.h"
34 #include "TFile.h"
35 #include "TString.h"
36 #include "TMath.h"
37 #include "AliLog.h"
38 #include "AliITSNoiseSSDv2.h"
39 #include "AliITSPedestalSSDv2.h"
40 #include "AliITSBadChannelsSSDv2.h"
41 #include "AliITSRawStreamSSD.h"
42 #include "AliRawReaderDate.h"
43 #include "AliITSRawStreamSSD.h"
44 #include "AliITSChannelDaSSD.h"
45
46
47 ClassImp(AliITSHandleDaSSD)
48
49 using namespace std;
50
51
52 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModules = 1698;       // Number of SSD modules in ITS
53 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerDdl = 108;  // Number of SSD modules in DDL
54 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerSlot = 12;  // Number of SSD modules in Slot
55 const Short_t  AliITSHandleDaSSD::fgkMinSSDModuleId = 500;            // Initial SSD modules number
56 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDSlotsPerDDL = 9;      // Number of SSD slots per DDL
57 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDDDLs = 16;            // Number of SSD modules in Slot
58 const Float_t  AliITSHandleDaSSD::fgkPedestalThresholdFactor = 3.0;   // Defalt value for fPedestalThresholdFactor 
59 const Float_t  AliITSHandleDaSSD::fgkCmThresholdFactor = 3.0;         // Defalt value for fCmThresholdFactor
60
61 const UInt_t   AliITSHandleDaSSD::fgkZsBitMask      = 0x0000003F;     // Bit mask for FEROM ZS
62 const UInt_t   AliITSHandleDaSSD::fgkOffSetBitMask  = 0x000003FF;     // Bit mask for FEROM Offset correction
63 const UInt_t   AliITSHandleDaSSD::fgkBadChannelMask = 0x00000001;     // Mask to suppress the channel from the bad channel list
64 const Int_t    AliITSHandleDaSSD::fgkAdcPerDBlock   = 6;              // FEROM configuration file constant
65
66 //______________________________________________________________________________
67 AliITSHandleDaSSD::AliITSHandleDaSSD() :
68   fRawDataFileName(NULL),
69   fNumberOfModules(0),
70   fModules(NULL),
71   fModIndProcessed(0),
72   fModIndRead(0),
73   fModIndex(NULL),
74   fEqIndex(0),
75   fNumberOfEvents(0),
76   fBadChannelsList(NULL),
77   fDDLModuleMap(NULL),
78   fALaddersOff(0),
79   fCLaddersOff(0),
80   fLdcId(0),
81   fRunId(0),
82   fPedestalThresholdFactor(fgkPedestalThresholdFactor),
83   fCmThresholdFactor(fgkCmThresholdFactor),
84   fZsDefault(-1),
85   fOffsetDefault(INT_MAX),
86   fZsMinimum(2),
87   fMergeBCLists(1),
88   fZsFactor(3.0)
89 {
90 // Default constructor
91 }
92
93
94 //______________________________________________________________________________
95 AliITSHandleDaSSD::AliITSHandleDaSSD(Char_t *rdfname) :
96   fRawDataFileName(NULL),
97   fNumberOfModules(0),
98   fModules(NULL),
99   fModIndProcessed(0),
100   fModIndRead(0),
101   fModIndex(NULL),
102   fEqIndex(0),
103   fNumberOfEvents(0),
104   fBadChannelsList(NULL),
105   fDDLModuleMap(NULL),
106   fALaddersOff(0),
107   fCLaddersOff(0),
108   fLdcId(0),
109   fRunId(0),
110   fPedestalThresholdFactor(fgkPedestalThresholdFactor) ,
111   fCmThresholdFactor(fgkCmThresholdFactor),
112   fZsDefault(-1),
113   fOffsetDefault(INT_MAX),
114   fZsMinimum(2),
115   fMergeBCLists(1),
116   fZsFactor(3.0)
117 {
118   if (!Init(rdfname)) AliError("AliITSHandleDaSSD::AliITSHandleDaSSD() initialization error!");
119 }
120
121
122 //______________________________________________________________________________
123 AliITSHandleDaSSD::AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc) :
124   TObject(ssdadldc),
125   fRawDataFileName(ssdadldc.fRawDataFileName),
126   fNumberOfModules(ssdadldc.fNumberOfModules),
127   fModules(NULL),
128   fModIndProcessed(ssdadldc.fModIndProcessed),
129   fModIndRead(ssdadldc.fModIndRead),
130   fModIndex(NULL),
131   fEqIndex(0),
132   fNumberOfEvents(ssdadldc.fNumberOfEvents),
133   fBadChannelsList(NULL),
134   fDDLModuleMap(NULL),
135   fALaddersOff(ssdadldc.fALaddersOff),
136   fCLaddersOff(ssdadldc.fCLaddersOff),
137   fLdcId(ssdadldc.fLdcId),
138   fRunId(ssdadldc.fRunId),
139   fPedestalThresholdFactor(ssdadldc.fPedestalThresholdFactor),
140   fCmThresholdFactor(ssdadldc.fCmThresholdFactor),
141   fZsDefault(ssdadldc.fZsDefault),
142   fOffsetDefault(ssdadldc.fOffsetDefault),
143   fZsMinimum(ssdadldc.fZsMinimum),
144   fMergeBCLists(ssdadldc.fMergeBCLists),
145   fZsFactor(ssdadldc.fZsFactor)
146 {
147   // copy constructor
148   if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
149     fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
150     if (fModules) {
151       for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
152         if (ssdadldc.fModules[modind]) {
153           fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
154           if (!fModules[modind]) { 
155             AliError("AliITSHandleDaSSD: Error copy constructor");
156             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
157             delete [] fModules;
158             fModules = NULL;
159             break;
160           }
161         } else fModules[modind] = NULL; 
162       }   
163     } else {
164       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
165       fNumberOfModules = 0;
166       fModules = NULL;
167     }
168   }
169   if (ssdadldc.fBadChannelsList) AliWarning("fBadChannelsList is not copied by copy constructor, use other methods to init it!");
170   if (ssdadldc.fDDLModuleMap) AliWarning("fDDLModuleMap is not copied by copy constructor, use other methods to init it!");
171 }
172
173
174 //______________________________________________________________________________
175 AliITSHandleDaSSD& AliITSHandleDaSSD::operator = (const AliITSHandleDaSSD& ssdadldc)
176 {
177 // assignment operator
178   if (this == &ssdadldc)  return *this;
179   TObject::operator=(ssdadldc);
180   if (fModules) {
181     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
182     delete [] fModules; 
183     fModules = NULL;
184   }
185   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
186   fNumberOfModules = ssdadldc.fNumberOfModules;
187   if ((fNumberOfModules > 0) && (ssdadldc.fModules)) {
188     fModules = new (nothrow) AliITSModuleDaSSD* [fNumberOfModules];
189     if (fModules) {
190       for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
191         if (ssdadldc.fModules[modind]) {
192               fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
193           if (!fModules[modind]) { 
194                 AliError("AliITSHandleDaSSD: Error assignment operator");
195             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
196             delete [] fModules;
197             fModules = NULL;
198             break;
199           }
200         } else fModules[modind] = NULL; 
201       }   
202     } else {
203       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
204       fNumberOfModules = 0;
205       fModules = NULL;
206     }
207   }
208   if (ssdadldc.fRawDataFileName) {
209     fRawDataFileName = new (nothrow) Char_t[strlen(ssdadldc.fRawDataFileName)+1];
210     if (fRawDataFileName) strncpy(fRawDataFileName, ssdadldc.fRawDataFileName, strlen(ssdadldc.fRawDataFileName)+1);
211   } else fRawDataFileName = ssdadldc.fRawDataFileName;
212   fModIndProcessed = ssdadldc.fModIndProcessed;
213   fModIndRead = ssdadldc.fModIndRead;
214
215   if (ssdadldc.fModIndex) {
216     fModIndex = new (nothrow) Int_t [ssdadldc.fNumberOfModules];
217     if (fModIndex) memcpy(fModIndex, ssdadldc.fModIndex, ssdadldc.fNumberOfModules*sizeof(Int_t));
218   } else fModIndex = ssdadldc.fModIndex;
219
220   fEqIndex = ssdadldc.fEqIndex;
221   fNumberOfEvents = ssdadldc.fNumberOfEvents;
222   fLdcId = ssdadldc.fLdcId;
223   fRunId = ssdadldc.fRunId;
224   fPedestalThresholdFactor = ssdadldc.fPedestalThresholdFactor;
225   fCmThresholdFactor = ssdadldc.fCmThresholdFactor;
226   fZsDefault = ssdadldc.fZsDefault;
227   fOffsetDefault = ssdadldc.fOffsetDefault;
228   fZsMinimum = ssdadldc.fZsMinimum;
229   fMergeBCLists = ssdadldc.fMergeBCLists;
230   fZsFactor = ssdadldc.fZsFactor;
231   fALaddersOff = ssdadldc.fALaddersOff;
232   fCLaddersOff = ssdadldc.fCLaddersOff;
233   if (fBadChannelsList) delete fBadChannelsList;
234   fBadChannelsList = new AliITSBadChannelsSSDv2(*ssdadldc.fBadChannelsList);
235   if (fDDLModuleMap) delete [] fDDLModuleMap; 
236   if (ssdadldc.fDDLModuleMap) {
237     fDDLModuleMap = new (nothrow) Int_t [fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl];
238     if (fDDLModuleMap) memcpy(fDDLModuleMap, ssdadldc.fDDLModuleMap, fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl * sizeof(Int_t)); 
239   } else fDDLModuleMap = 0;
240
241   if (ssdadldc.fBadChannelsList) AliWarning("fBadChannelsList is not copied by assignment operator, use other methods to init it!");
242   if (ssdadldc.fDDLModuleMap) AliWarning("fDDLModuleMap is not copied by assignment operator, use other methods to init it!");
243   return *this;
244 }
245
246
247 //______________________________________________________________________________
248 AliITSHandleDaSSD::~AliITSHandleDaSSD()
249 {
250 // Default destructor 
251   if (fModules) 
252   {
253     for (Int_t i = 0; i < fNumberOfModules; i++)
254     { 
255       if (fModules[i]) delete fModules[i];
256     }
257     delete [] fModules;
258   }
259   if (fModIndex) delete [] fModIndex;
260   if (fBadChannelsList)  delete fBadChannelsList;
261   if (fDDLModuleMap) delete [] fDDLModuleMap;
262 }
263
264
265
266 //______________________________________________________________________________
267 void AliITSHandleDaSSD::Reset()
268 {
269 // Delete array of AliITSModuleDaSSD* objects. 
270   if (fModules) {
271     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
272     delete [] fModules;
273     fModules = NULL;
274   }
275   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
276 /*
277   if (fBadChannelsList) {
278     delete fBadChannelsList;
279     fBadChannelsList = NULL; 
280   }    
281   if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL; }
282 */
283   fALaddersOff.Set(0);
284   fCLaddersOff.Set(0);
285   fRawDataFileName = NULL;
286   fModIndProcessed = fModIndRead = 0;
287   fNumberOfEvents = 0;
288   fLdcId = fRunId = 0;
289   fPedestalThresholdFactor = fgkPedestalThresholdFactor;
290   fCmThresholdFactor = fgkCmThresholdFactor;
291 }
292
293
294
295 //______________________________________________________________________________
296 Bool_t AliITSHandleDaSSD::Init(Char_t *rdfname)
297 {
298 // Read raw data file and set initial and configuration parameters
299   Long_t physeventind = 0, strn = 0, nofstripsev, nofstrips = 0;
300   Int_t  nofeqipmentev, nofeqipment = 0, eqn = 0; 
301   AliRawReaderDate  *rawreaderdate = NULL;
302   UChar_t *data = NULL;
303   Long_t datasize = 0, eqbelsize = 1;
304
305   rawreaderdate = new AliRawReaderDate(rdfname, 0);
306   if (!(rawreaderdate->GetAttributes() || rawreaderdate->GetEventId())) {
307     AliError(Form("AliITSHandleDaSSD: Error reading raw data file %s by RawReaderDate", rdfname));
308     MakeZombie();
309     return kFALSE;
310   }
311   if (rawreaderdate->NextEvent()) {
312     fRunId = rawreaderdate->GetRunNumber(); 
313     rawreaderdate->RewindEvents();
314   } else { MakeZombie(); return kFALSE; }
315   if (fModules) Reset();
316   //rawreaderdate->SelectEvents(-1);
317   rawreaderdate->Select("ITSSSD");  
318   nofstrips = 0;
319   while (rawreaderdate->NextEvent()) {
320     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
321     nofstripsev = 0;
322     nofeqipmentev = 0;
323     while (rawreaderdate->ReadNextData(data)) {
324       fLdcId = rawreaderdate->GetLDCId();
325       nofeqipmentev += 1;
326       datasize = rawreaderdate->GetDataSize();
327       eqbelsize = rawreaderdate->GetEquipmentElementSize();
328       if ( datasize % eqbelsize ) {
329         AliError(Form("AliITSHandleDaSSD: Error Init(%s): event data size %ld is not an integer of equipment data size %ld", 
330                                     rdfname, datasize, eqbelsize));
331         MakeZombie();
332             return kFALSE;
333       }
334       nofstripsev += (Int_t) (datasize / eqbelsize);
335     }
336     if (physeventind++) {
337       if (nofstrips != nofstripsev) AliWarning(Form("AliITSHandleDaSSD: number of strips varies from event to event, ev = %ld, %ld", 
338                                                      physeventind, nofstripsev));
339       if (nofeqipment != nofeqipmentev) AliWarning("AliITSHandleDaSSD: number of DDLs varies from event to event");
340     }
341     nofstrips = nofstripsev;
342     nofeqipment = nofeqipmentev;
343     if (strn < nofstrips)  strn = nofstrips;
344     if (eqn < nofeqipment) eqn = nofeqipment;
345   }
346   delete rawreaderdate;
347   if ((physeventind > 0) && (strn > 0))
348   {
349     fNumberOfEvents = physeventind;
350     fRawDataFileName = rdfname;
351     fEqIndex.Set(eqn);
352     fEqIndex.Reset(-1);
353     fModIndex = new (nothrow) Int_t [fgkNumberOfSSDModulesPerDdl * eqn];
354     if (fModIndex) 
355       for (Int_t i = 0; i < fgkNumberOfSSDModulesPerDdl * eqn; i++) fModIndex[i] = -1; 
356     else AliWarning(Form("AliITSHandleDaSSD: Error Init(%s): Index array for %i modules was not created", 
357                                      rdfname, fgkNumberOfSSDModulesPerDdl * eqn));
358     if (SetNumberOfModules(fgkNumberOfSSDModulesPerDdl * eqn)) {
359       TString str = TString::Format("Max number of equipment: %d, max number of channels: %ld\n", eqn, strn);
360       DumpInitData(str.Data());
361       return kTRUE;
362     }  
363   }
364   MakeZombie();
365   return kFALSE;
366 }
367
368
369
370 //______________________________________________________________________________
371 AliITSModuleDaSSD* AliITSHandleDaSSD::GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
372 {
373 // Retrieve AliITSModuleDaSSD object from the array
374    if (!fModules) return NULL;
375    for (Int_t i = 0; i < fNumberOfModules; i++) {
376      if (fModules[i]) {
377        if (    (fModules[i]->GetDdlId() == ddlID)
378             && (fModules[i]->GetAD() == ad)
379             && (fModules[i]->GetADC() == adc))
380             return fModules[i];
381      }      
382    }
383    return NULL;
384 }
385
386
387 Int_t AliITSHandleDaSSD::GetModuleIndex (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
388 {
389 // Retrieve the position of AliITSModuleDaSSD object in the array
390    if (!fModules) return -1;
391    for (Int_t i = 0; i < fNumberOfModules; i++) {
392      if (fModules[i]) {
393        if (    (fModules[i]->GetDdlId() == ddlID)
394             && (fModules[i]->GetAD() == ad)
395             && (fModules[i]->GetADC() == adc))
396             return i;
397      }      
398    }
399    return -1;
400 }
401
402
403
404 //______________________________________________________________________________
405 AliITSChannelDaSSD* AliITSHandleDaSSD::GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const
406 {
407 // Retrieve AliITSChannalDaSSD object from the array
408   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
409     if (    (fModules[modind]->GetDdlId() == ddlID)
410          && (fModules[modind]->GetAD() == ad)
411          && (fModules[modind]->GetADC() == adc)  ) 
412         {
413        return fModules[modind]->GetStrip(stripID);
414     }
415   }
416   AliError(Form("AliITSHandleDaSSD: Error GetStrip (%i, %i, %i, %i), strip not found, returns NULL!",
417                 ddlID, ad, adc, stripID));
418   return NULL;
419 }
420
421
422
423 //______________________________________________________________________________
424 Bool_t AliITSHandleDaSSD::SetModule(AliITSModuleDaSSD *const module, const Int_t index)
425
426 // Assign array element with AliITSModuleDaSSD object
427    if ((index < fNumberOfModules) && (index >= 0)) 
428      { 
429         if (fModules[index]) delete fModules[index];
430         fModules[index] = module;
431         return kTRUE;
432       }
433    else return kFALSE;                         
434 }
435
436
437
438 //______________________________________________________________________________
439 Bool_t AliITSHandleDaSSD::SetNumberOfModules (const Int_t numberofmodules)
440 {
441 // Allocates memory for AliITSModuleDaSSD objects
442   if (numberofmodules > fgkNumberOfSSDModules)
443     AliWarning(Form("AliITSHandleDaSSD: the number of modules %i you use exceeds the maximum %i for ALICE ITS SSD", 
444                      numberofmodules, fgkNumberOfSSDModules));
445   if (fModules) {
446     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
447     delete [] fModules; 
448     fModules = NULL;
449   }
450   fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
451   if (fModules) {
452     fNumberOfModules = numberofmodules;
453     memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
454     return kTRUE;
455   } else {
456      AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules));
457      fNumberOfModules = 0;
458      fModules = NULL;
459   }
460   return kFALSE;
461 }
462
463
464
465 //______________________________________________________________________________
466 Bool_t AliITSHandleDaSSD::ReadStaticBadChannelsMap(const Char_t *filename)
467 {
468 // Reads Static Bad Channels Map from the file
469   TFile *bcfile;
470   if (!filename) {
471     AliWarning("No file name is specified for Static Bad Channels Map!");
472     return kFALSE;
473   } 
474   bcfile = new TFile(filename, "READ");
475   if (bcfile->IsZombie()) {
476     AliWarning(Form("Error reading file %s with Static Bad Channels Map!", filename));
477     return kFALSE;
478   }
479   bcfile->GetObject("AliITSBadChannelsSSDv2;1", fBadChannelsList);
480   if (!fBadChannelsList) {
481     AliWarning("Error fBadChannelsList == NULL!");
482     bcfile->Close();
483     delete bcfile;
484     return kFALSE;
485   }
486   bcfile->Close();
487   delete bcfile;
488   return kTRUE;
489 }
490
491
492
493 Bool_t AliITSHandleDaSSD::ReadDDLModuleMap(const Char_t *filename)
494 {
495 // Reads the SSD DDL Map from the file
496   ifstream             ddlmfile;
497   AliRawReaderDate    *rwr = NULL;
498   AliITSRawStreamSSD  *rsm = NULL;
499   void                *event = NULL;
500   if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
501   fDDLModuleMap = new (nothrow) Int_t [fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl];
502   if (!fDDLModuleMap) {
503     AliWarning("Error allocation memory for DDL Map!");
504     return kFALSE;
505   }    
506   if (!filename) {
507     AliWarning("No file name is specified for SSD DDL Map, using the one from AliITSRawStreamSSD!");
508     rwr = new AliRawReaderDate(event);
509     rsm = new AliITSRawStreamSSD(rwr);
510     rsm->Setv11HybridDDLMapping();
511     for (Int_t ddli = 0; ddli < fgkNumberOfSSDDDLs; ddli++)
512       for (Int_t mi = 0; mi < fgkNumberOfSSDModulesPerDdl; mi++)
513         fDDLModuleMap[(ddli * fgkNumberOfSSDModulesPerDdl + mi)] = rsm->GetModuleNumber(ddli, mi);
514     if (rsm) delete rsm;
515     if (rwr) delete rwr;        
516     return kTRUE;
517   } 
518   ddlmfile.open(filename, ios::in);
519   if (!ddlmfile.is_open()) {
520     AliWarning(Form("Error reading file %s with SSD DDL Map!", filename));
521     if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
522     return kFALSE;
523   }
524   Int_t ind = 0;
525   while((!ddlmfile.eof()) && (ind < (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))) {
526     ddlmfile >> fDDLModuleMap[ind++];
527     if (ddlmfile.fail()) AliError(Form("Error extracting number from the DDL map file %s, ind: %d ", filename, ind));
528   }
529   if (ind != (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))
530     AliWarning(Form("Only %i (< %i) entries were read from DDL Map!", ind, (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl)));
531   ddlmfile.close();
532   return kTRUE;
533 }
534
535
536
537 //______________________________________________________________________________
538 Int_t AliITSHandleDaSSD::ReadCalibrationDataFile (char* fileName, const Long_t eventsnumber)
539 {
540 // Reads raw data from file
541   if (!Init(fileName)){
542     AliError("AliITSHandleDaSSD: Error ReadCalibrationDataFile");
543     return kFALSE;
544   }
545   fNumberOfEvents = eventsnumber;
546   return ReadModuleRawData (fNumberOfModules);  
547 }
548
549
550
551 //______________________________________________________________________________
552 Int_t AliITSHandleDaSSD::ReadModuleRawData (const Int_t modulesnumber)
553 {
554 // Reads raw data from file
555   AliRawReader        *rawreaderdate = NULL;
556   AliITSRawStreamSSD  *stream = NULL;
557   AliITSModuleDaSSD   *module;
558   AliITSChannelDaSSD  *strip;
559   Long_t            eventind = 0;
560   Int_t             nofeqipment, eqind;
561   Short_t           equipid, prequipid;
562   Short_t           modind;
563   if (!(rawreaderdate = new AliRawReaderDate(fRawDataFileName, 0))) return 0;
564   if (!fModules) {
565     AliError("AliITSHandleDaSSD: Error ReadModuleRawData: no structure was allocated for data");
566     return 0;
567   }
568   if (!fDDLModuleMap) if (!ReadDDLModuleMap()) AliWarning("DDL map is not defined, ModuleID will be set to 0!");
569   stream = new AliITSRawStreamSSD(rawreaderdate);
570   stream->Setv11HybridDDLMapping();
571   //rawreaderdate->SelectEvents(-1);
572   rawreaderdate->Select("ITSSSD");  
573   modind = 0;
574   nofeqipment = 0;
575   while (rawreaderdate->NextEvent()) {
576     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
577     prequipid = -1;
578     eqind = -1;
579     while (stream->Next()) {
580       equipid = rawreaderdate->GetEquipmentId(); 
581       if ((equipid != prequipid)) {
582         if ((eqind = GetEqIndex(equipid)) < 0) { fEqIndex.AddAt(equipid, nofeqipment); eqind = nofeqipment++; }
583         prequipid = equipid;
584       }
585       Int_t     equiptype  = rawreaderdate->GetEquipmentType();
586       UChar_t   ddlID      = (UChar_t)rawreaderdate->GetDDLID();
587       UChar_t   ad      = stream->GetAD();
588       UChar_t   adc     = stream->GetADC();
589       UShort_t  stripID = stream->GetSideFlag() ? AliITSChannelDaSSD::GetMaxStripIdConst() - stream->GetStrip() : stream->GetStrip();
590       Short_t   signal  = stream->GetSignal();
591
592       Int_t indpos = (eqind * fgkNumberOfSSDModulesPerDdl)
593                    + ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
594       Int_t modpos = fModIndex[indpos];
595       if (((modpos > 0) && (modpos < fModIndRead)) || ((modpos < 0) && (modind == modulesnumber))) continue;
596       if ((modpos < 0) && (modind < modulesnumber)) {
597         module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
598         Int_t mddli;
599         if (fDDLModuleMap) mddli = RetrieveModuleId(ddlID, ad, adc);
600         else  mddli = 0;
601         if (!module->SetModuleIdData (ddlID, ad, adc, mddli)) return 0;
602         module->SetModuleRorcId (equipid, equiptype);
603         modpos = fModIndRead + modind;
604         modind += 1;
605         fModules[modpos] = module;
606         fModIndex[indpos] = modpos;
607       } 
608       if (stripID < AliITSModuleDaSSD::GetStripsPerModuleConst()) {
609         if (!(strip = fModules[modpos]->GetStrip(stripID))) {
610           strip = new AliITSChannelDaSSD(stripID, fNumberOfEvents);
611           fModules[modpos]->SetStrip(strip, stripID);
612         }
613         strip->SetSignal(eventind, signal);
614           } else  {
615         if (!(fModules[modpos]->GetCMFerom())) {
616           fModules[modpos]->AllocateCMFeromArray();
617           fModules[modpos]->SetCMFeromEventsNumber(fNumberOfEvents);
618                 }
619             fModules[modpos]->SetCMFerom(signal, (stripID - AliITSModuleDaSSD::GetStripsPerModuleConst()), eventind);
620           }
621     }
622     if (++eventind > fNumberOfEvents) break;
623   }
624   delete stream;
625   delete rawreaderdate;
626   if (modind) cout << "The memory was allocated for " <<  modind << " modules." << endl;
627   fModIndRead += modind;
628   if (modind < modulesnumber) RelocateModules();
629   return modind;
630 }
631
632
633
634 //______________________________________________________________________________
635 Bool_t AliITSHandleDaSSD::RelocateModules()
636 {
637 // Relocate memory for AliITSModuleDaSSD object array
638   Int_t         nm = 0;
639   AliITSModuleDaSSD **marray = NULL;
640   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
641     if (fModules[modind]) nm += 1;
642   if (nm == fNumberOfModules) return kTRUE;
643   marray = new (nothrow) AliITSModuleDaSSD* [nm];
644   if (!marray) {
645     AliError(Form("AliITSHandleDaSSD: Error relocating memory for %i AliITSModuleDaSSD* objects!", nm));
646     return kFALSE;
647   }
648   nm = 0;
649   for (Int_t modind = 0; modind < fNumberOfModules; modind++)  
650     if (fModules[modind]) marray[nm++] = fModules[modind];
651   delete [] fModules;
652   fModules = marray;
653   fNumberOfModules = fModIndRead = nm;
654   return kTRUE;
655 }
656
657
658
659 //______________________________________________________________________________
660 Bool_t AliITSHandleDaSSD::AddFeromCm(const AliITSModuleDaSSD *const module)
661 {
662 // Restore the original signal value adding CM calculated and subtracted in ferom
663   AliITSChannelDaSSD *strip;
664   Short_t            *signal, *cmferom;
665
666   if (!module) return kFALSE;
667   if (!module->GetCMFerom()) return kTRUE;
668   for (Int_t chipind = 0; chipind < AliITSModuleDaSSD::GetChipsPerModuleConst(); chipind++) {
669     if (!(cmferom = module->GetCMFerom(chipind))) {
670       AliWarning(Form("AliITSHandleDaSSD: There is no Ferom CM values for chip %i, module %i!", chipind, module->GetModuleId()));
671       continue;
672     }
673     for (Int_t strind = (chipind * AliITSModuleDaSSD::GetStripsPerChip()); 
674                strind < ((chipind + 1) * AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
675       if (!(strip = module->GetStrip(strind))) continue;
676       if (!(signal = strip->GetSignal())) continue;
677 //      if (strip->GetEventsNumber() != module->GetEventsNumber()) return kFALSE;
678       Long_t ovev = 0;
679       for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
680         if (SignalOutOfRange(signal[ev]) || SignalOutOfRange(cmferom[ev])) ovev += 1;
681         else {
682           Short_t signal1 = signal[ev] + cmferom[ev];
683           strip->SetSignal(ev, signal1);
684             }  
685       } 
686     } 
687   }  
688   return kTRUE;
689 }
690
691
692
693 //______________________________________________________________________________
694 Bool_t AliITSHandleDaSSD::CalculatePedestal(const AliITSModuleDaSSD *const module)
695 {
696 // Calculates Pedestal
697   AliITSChannelDaSSD *strip;
698   Double_t            pedestal, noise;
699   Short_t            *signal;
700   Long_t              ovev, ev, n;
701   if (!module) return kFALSE;
702   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
703     if (!(strip = module->GetStrip(strind))) continue;
704     if (!(signal = strip->GetSignal())) {
705       AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
706                      module->GetModuleId(), strind));
707       continue; 
708     }
709 //************* pedestal first pass ****************
710     pedestal = 0.0L;
711     ovev = 0l;
712     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
713       if (SignalOutOfRange(signal[ev])) ovev += 1;
714       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1) : signal[ev];
715     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
716     strip->SetPedestal(static_cast<Float_t>(pedestal)); 
717 //************* noise *******************************
718     Double_t nsum = 0.0L;
719     ovev = 0l;
720     for (ev = 0; ev < strip->GetEventsNumber(); ev++) {
721       if (SignalOutOfRange(signal[ev])) ovev += 1;
722       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
723     } 
724     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  TMath::Sqrt(nsum / (Double_t)(n));
725     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
726     strip->SetNoise(static_cast<Float_t>(noise));
727 //************* pedestal second pass ****************
728     pedestal = 0.0L;
729     ovev = 0l;
730     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
731       if (   SignalOutOfRange(signal[ev]) 
732           || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
733       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1) : signal[ev];
734     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
735     strip->SetPedestal(static_cast<Float_t>(pedestal)); 
736     strip->SetOverflowNumber(ovev);     
737   }
738   return kTRUE;
739 }
740
741
742 //______________________________________________________________________________
743 Bool_t AliITSHandleDaSSD::CalculateNoise(const AliITSModuleDaSSD *const module)
744 {
745 // Calculates Noise
746   AliITSChannelDaSSD *strip;
747   Short_t    *signal;
748   Float_t     noise;
749   Long_t      ovev, n;
750   if (!module) return kFALSE;
751   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
752     if (!(strip = module->GetStrip(strind))) continue;
753     if (!(signal = strip->GetSignal())) {
754       strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
755       AliError(Form("AliITSHandleDaSSD: Error CalculateNoise(): there are no events data for module[%i] strip[%i]->GetSignal()",
756                      module->GetModuleId(), strind));
757       continue;
758     }
759     Double_t nsum = 0.0L;
760     ovev = 0l;
761     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
762       if (SignalOutOfRange(signal[ev])) ovev += 1;
763       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
764     } 
765     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  static_cast<Float_t>(TMath::Sqrt(nsum / (Float_t)(n)));
766     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
767     strip->SetNoise(noise);
768   }
769   return kTRUE;
770 }
771
772
773
774 //______________________________________________________________________________
775 Bool_t AliITSHandleDaSSD::CalculateNoiseCM(AliITSModuleDaSSD *const module)
776 {
777 // Calculates Noise with CM correction
778   AliITSChannelDaSSD  *strip = NULL;
779   Short_t     *signal;
780   Float_t      noise;
781   Long_t       ovev, n;
782   if (!CalculateCM(module)) { 
783     AliError("Error: AliITSHandleDaSSD::CalculateCM() returned kFALSE");
784     return kFALSE;
785   }  
786   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
787     if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
788     if (!(signal = strip->GetSignal())) {
789       strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
790       AliError(Form("AliITSHandleDaSSD: Error CalculateNoiseCM(): there are no events data for module[%i] strip[%i]->GetSignal()", 
791                      module->GetModuleId(), strind));
792       continue; //return kFALSE;
793     }
794     Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
795     Double_t nsum = 0.0L;
796     ovev = 0l;
797     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
798       if (SignalOutOfRange(signal[ev])) ovev += 1;
799       else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
800     } 
801     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = static_cast<Float_t>(TMath::Sqrt(nsum / (Double_t)(n)));
802     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
803     strip->SetNoiseCM(noise);
804   }
805   return kTRUE;
806 }
807
808
809
810 //______________________________________________________________________________
811 Bool_t AliITSHandleDaSSD::CalculateCM(AliITSModuleDaSSD *const module)
812 {
813 // Calculates CM
814   AliITSChannelDaSSD  *strip = NULL;
815   Short_t             *signal;
816   Long_t               ovstr, n;
817   Int_t                stripind;
818   module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
819   for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
820     stripind = chipind * module->GetStripsPerChip();
821     module->GetCM()[chipind].Set(fNumberOfEvents);
822     module->GetCM()[chipind].Reset(0.0f);
823     for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
824     // calculate firs approximation of CM.
825       Double_t cm0 = 0.0L;
826       ovstr = 0l;
827       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
828         if (!(strip = module->GetStrip(strind))) continue; //return kFALSE; 
829         if (!(signal = strip->GetSignal())) {
830           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()", 
831                         module->GetModuleId(), strind));
832           return kFALSE;
833         }
834         if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
835         else cm0 += (signal[ev] - strip->GetPedestal());
836       }
837       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cm0 /= (Float_t)(n);
838       else { module->SetCM(0.0f, chipind, ev); continue; }
839     // calculate avarage (cm - (signal - pedestal)) over the chip
840       Double_t cmsigma = 0.0L;
841       ovstr = 0l;
842       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
843         if (!(strip = module->GetStrip(strind))) continue;
844         if (!(signal = strip->GetSignal())) {
845           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()\n",
846                          module->GetModuleId(), strind));
847           return kFALSE;
848         }
849         if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
850         else cmsigma += pow((cm0 - (signal[ev] - strip->GetPedestal())), 2);
851       }
852       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsigma = TMath::Sqrt(cmsigma / (Float_t)(n));
853       else { module->SetCM(0.0f, chipind, ev); continue; }
854    // calculate cm with threshold
855       Double_t cmsum = 0.0L;
856       ovstr = 0l;
857       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
858         if (!(strip = module->GetStrip(strind))) continue;
859         signal = strip->GetSignal();
860         if ( (SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue()) 
861                || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
862         else cmsum += (signal[ev] - strip->GetPedestal());
863       }
864       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Float_t)(n);
865       else cmsum = 0.0L;
866       if (!(module->SetCM(cmsum, chipind, ev))) 
867         AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
868                            module->GetModuleId(), chipind, ev));
869     } 
870   }
871   return kTRUE; 
872 }
873
874
875 //______________________________________________________________________________
876 Bool_t AliITSHandleDaSSD::ProcessRawData(const Int_t nmread, const Bool_t usewelford)
877 {
878 // Performs calculation of calibration parameters (pedestal, noise, ...) 
879   Int_t nm = 0;
880   if (nmread <= 0) return kFALSE;
881   if (!fModules) return kFALSE;
882   while ((nm = ReadModuleRawData(nmread)) > 0) {
883     cout << "Processing next " << nm << " modules;" << endl;  
884     for (Int_t modind = fModIndProcessed; modind < fModIndRead; modind++) {
885       if (!fModules[modind]) {
886         AliError(Form("AliITSHandleDaSSD: Error ProcessRawData(): No AliITSModuleDaSSD object with index %i is allocated in AliITSHandleDaSSD\n",
887                        modind));
888         return kFALSE;
889       }
890       AddFeromCm(fModules[modind]);
891       if (usewelford) {
892         CalculatePedNoiseW(fModules[modind]);
893         CalculateNoiseCMW(fModules[modind]);
894       } else {
895         CalculatePedestal(fModules[modind]);
896         CalculateNoise(fModules[modind]);
897         CalculateNoiseCM(fModules[modind]);
898       }
899     }
900     DeleteSignal();
901     DeleteCM();
902     DeleteCMFerom();
903     fModIndProcessed = fModIndRead;
904     cout << fModIndProcessed << " - done" << endl;
905     if (nm < nmread ) break;
906   }
907   return kTRUE;  
908 }
909
910
911 //______________________________________________________________________________
912 Short_t AliITSHandleDaSSD::RetrieveModuleId(const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
913 {
914 // Retrieve ModuleId from the DDL map which is defined in AliITSRawStreamSSD class
915   if (!fDDLModuleMap) {
916     AliError("Error DDLMap is not initialized, return 0!");
917         return 0;
918   }
919   Int_t mddli = ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
920   if ((ddlID < fgkNumberOfSSDDDLs) && (mddli < fgkNumberOfSSDModulesPerDdl)) {
921     mddli = fDDLModuleMap[ddlID * fgkNumberOfSSDModulesPerDdl + mddli];
922   }  
923   else {
924     AliWarning(Form("Module index  = %d or ddlID = %d is out of range 0 is rturned", ddlID, mddli));
925     mddli = 0;
926   }
927   if (mddli > SHRT_MAX) return SHRT_MAX;
928   else return (Short_t)mddli;
929 }
930
931
932
933 //______________________________________________________________________________
934 AliITSNoiseSSDv2* AliITSHandleDaSSD::GetCalibrationOCDBNoise() const
935 {
936 // Fill in the array for OCDB 
937   AliITSNoiseSSDv2         *ldcn = NULL;
938   AliITSModuleDaSSD      *module = NULL;
939   AliITSChannelDaSSD     *strip = NULL; 
940   if (!fModules) return NULL;
941   ldcn = new AliITSNoiseSSDv2;
942   if (!ldcn) {
943     AliError("Error allocation mamory for AliITSBadChannelsSSDv2 object, return NULL!");
944     return NULL;
945   }
946   for (Int_t i = 0; i < fNumberOfModules; i++) {
947     if (!(module = fModules[i])) continue;
948     if (module->GetModuleId() < fgkMinSSDModuleId) continue;
949     for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
950       if (!(strip = module->GetStrip(strind))) continue;
951       Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
952       if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() ) 
953         ldcn->AddNoiseP(modid, strip->GetStripId(), strip->GetNoiseCM());
954       else
955         ldcn->AddNoiseN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetNoiseCM());
956     }
957   }
958   return ldcn;
959 }
960
961
962 //______________________________________________________________________________
963 AliITSBadChannelsSSDv2* AliITSHandleDaSSD::GetCalibrationBadChannels() const
964 {
965 // Fill in the TObjArray with the list of bad channels 
966   AliITSBadChannelsSSDv2   *ldcbc = NULL;
967   AliITSModuleDaSSD      *module = NULL;
968   AliITSChannelDaSSD     *strip = NULL; 
969   if (!fModules) return NULL;
970   ldcbc = new AliITSBadChannelsSSDv2;
971   if (!ldcbc) {
972     AliError("Error allocation mamory for AliITSBadChannelsSSDv2 object, return NULL!");
973     return NULL;
974   }
975   for (Int_t i = 0; i < fNumberOfModules; i++) {
976     if (!(module = fModules[i])) continue;
977     if (module->GetModuleId() < fgkMinSSDModuleId) continue;
978     for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
979       if (!(strip = module->GetStrip(strind))) continue;
980       Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
981       if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() )
982         ldcbc->AddBadChannelP(modid, strip->GetStripId(), EvaluateIfChannelIsBad(module, strip->GetStripId()));
983       else 
984         ldcbc->AddBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), 
985                                      EvaluateIfChannelIsBad(module, strip->GetStripId()));
986     }
987   }
988   return ldcbc;
989 }
990
991
992
993 //______________________________________________________________________________
994 Bool_t AliITSHandleDaSSD::SaveCalibrationSSDLDC(Char_t*& dafname)
995 {
996 // Save Calibration data locally
997   AliITSBadChannelsSSDv2   *ldcbc = NULL;
998   AliITSPedestalSSDv2      *ldcp = NULL;
999   AliITSNoiseSSDv2         *ldcn = NULL;
1000   AliITSModuleDaSSD      *module = NULL;
1001   AliITSChannelDaSSD     *strip = NULL; 
1002   Char_t         *tmpfname;
1003   TString         dadatafilename("");
1004   if (!fModules) return kFALSE;
1005   ldcn = new AliITSNoiseSSDv2;
1006   ldcp = new AliITSPedestalSSDv2;
1007   ldcbc = new AliITSBadChannelsSSDv2;
1008   if ((!ldcn) || (!ldcp) || (!ldcp)) {
1009     AliError("Error allocation mamory for calibration objects, return kFALSE!");
1010     return kFALSE;
1011   }
1012   for (Int_t i = 0; i < fNumberOfModules; i++) {
1013     if (!(module = fModules[i])) continue;
1014     if (module->GetModuleId() < fgkMinSSDModuleId) continue;
1015     for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1016       if (!(strip = module->GetStrip(strind))) continue;
1017       Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
1018       if (strip->GetStripId() < AliITSModuleDaSSD::GetPNStripsPerModule() ) {
1019         ldcn->AddNoiseP(modid, strip->GetStripId(), strip->GetNoiseCM());
1020         ldcp->AddPedestalP(modid, strip->GetStripId(), strip->GetPedestal());
1021         ldcbc->AddBadChannelP(modid, strip->GetStripId(), EvaluateIfChannelIsBad(module, strip->GetStripId()));
1022       } else {
1023         ldcn->AddNoiseN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetNoiseCM());
1024         ldcp->AddPedestalN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), strip->GetPedestal()); 
1025         ldcbc->AddBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strip->GetStripId()), 
1026                                      EvaluateIfChannelIsBad(module, strip->GetStripId()));
1027       }
1028     }
1029   }
1030   if (dafname) dadatafilename.Form("%s/", dafname);
1031   dadatafilename += TString::Format("ITSSSDda_%i.root", fLdcId);
1032   tmpfname = new Char_t[dadatafilename.Length()+1];
1033   Int_t sz = dadatafilename.Sizeof();
1034   dafname = strncpy(tmpfname, dadatafilename.Data(),sz);
1035   TFile *fileRun = new TFile (dadatafilename.Data(),"RECREATE");
1036   if (fileRun->IsZombie()) {
1037     AliError(Form("AliITSHandleDaSSD: SaveCalibrationSSDLDC() error open file %s", dadatafilename.Data()));
1038     ldcn->Delete();
1039     delete fileRun;
1040     delete ldcn;
1041     delete ldcp;
1042     delete ldcbc;
1043     return kFALSE;
1044   }
1045   fileRun->WriteTObject(ldcn);
1046   fileRun->WriteTObject(ldcp);
1047   if (fBadChannelsList) 
1048     if (fMergeBCLists) {
1049       MergeBadChannels(ldcbc);
1050       fileRun->WriteTObject(ldcbc);
1051     } else fileRun->WriteTObject(fBadChannelsList);
1052   else fileRun->WriteTObject(ldcbc);
1053   fileRun->Close();
1054   delete fileRun;
1055   delete ldcn;
1056   delete ldcp;
1057   delete ldcbc;
1058   return kTRUE;
1059 }
1060
1061
1062 //______________________________________________________________________________
1063 Int_t AliITSHandleDaSSD::MergeBadChannels(AliITSBadChannelsSSDv2*&  bcl) const
1064 {
1065 // Merges the statick bad channels list with bad channels got upon calibration
1066   AliITSModuleDaSSD     *module = 0;
1067   Int_t                  nmpch = 0, nmnch = 0, ngpch = 0, ngnch = 0;
1068   if (!fBadChannelsList || !bcl) {
1069     AliWarning("Either fBadChannelsList == NULL or bad_channels_list == NULL, there is nothing to merge!");
1070         return -1;
1071   }
1072   for (Int_t modind = 0; modind < GetNumberOfModules(); modind++) {
1073     if (!(module = fModules[modind])) continue;
1074     if (module->GetModuleId() < fgkMinSSDModuleId) continue;
1075     Short_t modid = module->GetModuleId() - fgkMinSSDModuleId;
1076     for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetPNStripsPerModule(); strind++) {
1077       if ( (!(fBadChannelsList->GetBadChannelP(modid, strind) & fgkBadChannelMask)) 
1078           && (bcl->GetBadChannelP(modid, strind) & fgkBadChannelMask) ) 
1079         ngpch++;
1080       if ( (!(fBadChannelsList->GetBadChannelN(modid, strind) & fgkBadChannelMask)) 
1081           && (bcl->GetBadChannelN(modid, strind) & fgkBadChannelMask) ) 
1082         ngnch++;
1083       if ( (!(bcl->GetBadChannelP(modid, strind) & fgkBadChannelMask)) 
1084           && (fBadChannelsList->GetBadChannelP(modid, strind) & fgkBadChannelMask) ) {
1085         bcl->AddBadChannelP(modid, strind, fBadChannelsList->GetBadChannelP(modid, strind));
1086         nmpch++;  
1087       }    
1088       if ( (!(bcl->GetBadChannelN(modid, strind) & fgkBadChannelMask)) 
1089           && (fBadChannelsList->GetBadChannelN(modid, strind) & fgkBadChannelMask) ) {
1090         bcl->AddBadChannelN(modid, strind, fBadChannelsList->GetBadChannelN(modid, strind));
1091         nmnch++;  
1092       }
1093     }     
1094   }
1095   AliInfo(Form("Static bad, dynamic good: P%d,  N%d", nmpch, nmnch));
1096   AliInfo(Form("Static good, dynamic bad: P%d,  N%d", ngpch, ngnch));
1097   return (nmnch + nmpch);
1098 }
1099
1100
1101
1102 //______________________________________________________________________________
1103 Bool_t AliITSHandleDaSSD::DumpModInfo(const Float_t meannosethreshold) const
1104 {
1105 // Dump calibration parameters
1106   AliITSModuleDaSSD    *mod;
1107   AliITSChannelDaSSD   *strip;
1108   if (!fModules) {
1109     cout << "SSDDALDC::DumpModInfo():  Error, no modules are allocated!" << endl;
1110     return kFALSE;
1111   }  
1112   cout << "Modules with MeanNoise > " << meannosethreshold << endl;
1113   for (Int_t i = 0; i < fNumberOfModules; i++) {
1114     if (!(mod = fModules[i])) continue;
1115     Double_t  maxnoise = 0.0L, meannoise = 0.0L, meanovf = 0.0L;
1116     Float_t   maxped = 0.0f;
1117     Int_t     maxstrind = 0, novfstr = 0, maxovf = 0; 
1118     for (Int_t strind = 0; strind < mod->GetNumberOfStrips(); strind++) {
1119       if (!(strip = mod->GetStrip(strind))) {novfstr++;  continue; }
1120       if (strip->GetNoiseCM() >= AliITSChannelDaSSD::GetUndefinedValue() ) {novfstr++;  continue; }
1121       if (maxnoise < strip->GetNoiseCM()) { 
1122         maxnoise = strip->GetNoiseCM();
1123         maxstrind = strind;
1124       } 
1125       meannoise = (strind - novfstr) ? meannoise + (strip->GetNoiseCM() - meannoise) / static_cast<Double_t>(strind - novfstr + 1) 
1126                                     : strip->GetNoiseCM();
1127       if (TMath::Abs(maxped) < TMath::Abs(strip->GetPedestal())) maxped = strip->GetPedestal();
1128       meanovf = (strind - novfstr) ? meanovf + (strip->GetOverflowNumber() - meanovf) / static_cast<Double_t>(strind - novfstr + 1) 
1129                                     : strip->GetOverflowNumber();
1130       if (strip->GetOverflowNumber() > maxovf) maxovf = strip->GetOverflowNumber();
1131     } 
1132     if (meannoise > meannosethreshold)
1133       cout << "Mod: " << i << ";  DDl: " << (int)mod->GetDdlId() << ";  AD: " << (int)mod->GetAD()  
1134                            << ";  ADC: " << (int)mod->GetADC() << "; MaxPed = " << maxped
1135                            << ";  MeanNoise = " << meannoise << "; MaxNoise = " << maxnoise << "; MaxNoiseStrip = " << maxstrind 
1136                            << ";  NOfStrips = " << (mod->GetNumberOfStrips() - novfstr) << endl;
1137         if (maxovf > 10) cout << "Max number of events with overflow :  " << maxovf << ";  mean : " << meanovf << endl;
1138   }
1139   return kTRUE;
1140 }
1141
1142
1143
1144 //______________________________________________________________________________
1145 Bool_t AliITSHandleDaSSD::PrintModCalibrationData(const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const Char_t *fname) const
1146 {
1147 // Print Module calibration data whether in file or in cout
1148    AliITSChannelDaSSD  *strip;
1149    ofstream             datafile;
1150    ostream             *outputfile;
1151    if (!fname) { outputfile = &cout; }
1152    else {
1153      datafile.open(fname, ios::out);
1154      if  (datafile.fail()) return kFALSE;
1155      outputfile = dynamic_cast<ostream*>(&datafile); 
1156    }
1157    *outputfile  << "DDL = " << (int)ddlID << ";  AD = " << (int)ad << ";  ADC = " << (int)adc << endl;
1158    for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
1159      if ( (strip = GetStrip(ddlID, ad, adc, strind)) ) {
1160        *outputfile << "Str = " << strind << ";  ped = " << strip->GetPedestal() 
1161                    << ";  noise = " << strip->GetNoiseCM() << endl;
1162      }
1163      else continue;
1164    }
1165   if (datafile.is_open()) datafile.close(); 
1166   return kTRUE;
1167 }
1168
1169
1170
1171 //______________________________________________________________________________
1172 void AliITSHandleDaSSD::DumpInitData(const Char_t *str) const
1173 {
1174 // Print general information retrieved from raw data file
1175   cout << "Raw data file: " << fRawDataFileName << endl
1176        << "LDC: " << (Int_t)fLdcId << "; RunId: " << fRunId << endl
1177        << "Number of physics events: " << fNumberOfEvents << endl
1178        << str;
1179 }
1180
1181
1182 //______________________________________________________________________________
1183 Bool_t AliITSHandleDaSSD::AllocateSimulatedModules(const Int_t copymodind)
1184 {
1185 // Used to allocate simulated modules to test the performance  
1186   AliITSModuleDaSSD *module;
1187   UChar_t      ad, adc, ddlID;
1188   ad = adc = ddlID = 0;
1189   if (!(fModules[copymodind])) return kFALSE;
1190   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
1191     if (!fModules[modind]) {
1192       module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
1193       if ((adc < 5) || ((adc > 7) && (adc < 13)) ) adc += 1;
1194       else if (adc == 5) adc = 8;
1195       else if (adc == 13) {
1196         adc = 0;
1197         if (ad < 8) ad += 1;
1198         else {
1199           ad = 0;
1200           ddlID +=1;
1201         }
1202       }
1203       if (!module->SetModuleIdData (ddlID, ad, adc, modind)) return kFALSE;
1204       fModules[modind] = module;
1205       for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1206         AliITSChannelDaSSD *cstrip = fModules[copymodind]->GetStrip(strind);
1207         if(!cstrip)return kFALSE;
1208         Long_t      eventsnumber = cstrip->GetEventsNumber();
1209         AliITSChannelDaSSD *strip = new AliITSChannelDaSSD(strind, eventsnumber);
1210         for (Long_t evind = 0; evind < eventsnumber; evind++) {
1211           Short_t sign = cstrip->GetSignal(evind);
1212           if (!strip->SetSignal(evind, sign)) AliError(Form("AliITSHandleDaSSD: Copy events error! mod = %i, str = %i", modind, strind));
1213         }
1214         module->SetStrip(strip, strind);
1215       }
1216     }
1217     else {
1218       ddlID = fModules[modind]->GetDdlId();
1219       ad    = fModules[modind]->GetAD();
1220       adc   = fModules[modind]->GetADC();
1221     }
1222   }
1223   for (UShort_t modind = 0; modind < fNumberOfModules; modind++) fModules[modind]->SetModuleId(modind + 1080);  
1224   return kTRUE;
1225 }
1226
1227
1228     
1229 //___________________________________________________________________________________________
1230 Bool_t AliITSHandleDaSSD::AdDataPresent(const Int_t ddl, const Int_t ad) const
1231 {
1232 // Check if there are calibration data for given ddl and slot
1233   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
1234     if ((GetModule(modind)->GetAD() == ad) && (GetModule(modind)->GetDdlId() == ddl)) return kTRUE;
1235   return kFALSE;
1236 }
1237
1238    
1239
1240 //___________________________________________________________________________________________
1241 Bool_t AliITSHandleDaSSD::SaveEqSlotCalibrationData(const Int_t ddl, const Int_t ad, const Char_t *fname) const
1242 {
1243 // Saves calibration files for selected equipment (DDL)
1244   fstream    feefile;
1245   Int_t      zsml, offsetml;
1246   ULong_t    zsth, offset, zsoffset;
1247   if (!fname) {
1248     AliError("File name must be specified!");
1249     return kFALSE;   
1250   }
1251   if (!AdDataPresent(ddl, ad)) {
1252     AliError(Form("Error SaveEqSlotCalibrationData(ddl = %i, ad = %i) no data present", ddl, ad)); 
1253     return kFALSE;
1254   }
1255   feefile.open(fname, ios::out);
1256   if (!feefile.is_open()) {
1257         AliError(Form("Can not open the file %s for output!", fname)); 
1258     return kFALSE;
1259   }
1260   for (zsml = 0; fgkZsBitMask >> zsml; zsml++) ;
1261   for (offsetml = 0; fgkOffSetBitMask >> offsetml; offsetml++) ;
1262   for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
1263     for (Int_t adcb = 0; adcb < fgkAdcPerDBlock; adcb++) {
1264       zsoffset = 0x0;
1265       for (Int_t j = 0; j < 2; j++) {
1266         Int_t adc = adcb + j * 8;
1267         zsth = ZsThreshold(ddl, ad, adc, strind);
1268         offset = OffsetValue(ddl, ad, adc, strind);
1269         zsoffset = zsoffset | (((zsth << offsetml) | offset) << ((j == 0) ? (offsetml + zsml) : 0));
1270       }
1271       feefile << "0x" << ConvBase(static_cast<unsigned long>(zsoffset), 16) << endl;
1272     }
1273   }
1274   feefile.close();
1275   return kTRUE;
1276 }
1277
1278
1279 //______________________________________________________________________________
1280 Int_t AliITSHandleDaSSD::ChannelIsBad(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1281 {
1282 // Check if the channel is bad
1283   AliITSModuleDaSSD     *module = NULL;
1284   Int_t                  modn = -1;
1285   if (fBadChannelsList && fDDLModuleMap) {
1286     modn = RetrieveModuleId(ddl, ad, adc);
1287     if (modn < 0) return -1;
1288     if (modn < fgkMinSSDModuleId) {
1289       AliWarning(Form("Module ddl/ad/adc: %d/%d/%d has number %d which is wrong for SSD module %d", ddl, ad, adc, strn, modn));
1290           return -1;
1291     }
1292     Short_t modid = modn - fgkMinSSDModuleId;
1293     if (strn < AliITSModuleDaSSD::GetPNStripsPerModule()) 
1294       return (fBadChannelsList->GetBadChannelP(modid, strn)  & fgkBadChannelMask);
1295     else return (fBadChannelsList->GetBadChannelN(modid, (AliITSChannelDaSSD::GetMaxStripIdConst() - strn)) & fgkBadChannelMask);
1296   } else {
1297     AliError("Error ether bad channels list or DDLMap is not initialized or both, EvaluateIfChannelIsBad(module, strip) is used!");
1298     if ((module = GetModule(ddl, ad, adc))) {
1299       return (EvaluateIfChannelIsBad(module, strn) & fgkBadChannelMask);
1300     } else {
1301       AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i, 0 is used!", ddl, ad, adc));
1302       return 0ul;
1303     }  
1304         return 0;
1305   }
1306 }
1307
1308
1309
1310 //______________________________________________________________________________
1311 Int_t AliITSHandleDaSSD::LadderIsOff(const UChar_t ddl, const UChar_t ad, const UChar_t adc) const
1312 {
1313 //Checks if the module with given ddl, ad, adc is on the ladder which is in the list of ladders which are off
1314   const Int_t nm5 =  500;
1315   const Int_t nm6 =  1248;
1316   const Int_t nml5a = 12;
1317   const Int_t nml5c = 10;
1318   const Int_t nml6a = 12;
1319   const Int_t nml6c = 13;
1320   Int_t               modn, ladder, layer, side;
1321   AliITSModuleDaSSD  *module;
1322   if (!(module = GetModule(ddl, ad, adc))) return 0;
1323   if ((modn = module->GetModuleId()) <= 0)  modn = RetrieveModuleId(ddl, ad, adc);
1324   if (modn <= 0) return 0;
1325   layer = modn >= nm6 ? 1 : 0;     // 6 : 5
1326   ladder = (modn - (layer ? nm6 : nm5)) / (layer ? (nml6a + nml6c) : (nml5a + nml5c));
1327   if ( ((modn - (layer ? nm6 : nm5)) % (layer ? (nml6a + nml6c) : (nml5a + nml5c))) < (layer ? nml6a : nml5a))
1328     side = 0;      // A
1329   else side = 1;   // C
1330   ladder += (layer ? 600 : 500);
1331   layer += 5;
1332   if (side)
1333     if (fCLaddersOff.GetSize()) {
1334       for(Int_t i = 0; i < fCLaddersOff.GetSize(); i++) 
1335         if (fCLaddersOff.At(i) == ladder) return fCLaddersOff.At(i);
1336       return 0;
1337     } else return 0;
1338   else
1339     if (fALaddersOff.GetSize()) {
1340       for(Int_t i = 0; i < fALaddersOff.GetSize(); i++) 
1341         if (fALaddersOff.At(i) == ladder) return fALaddersOff.At(i);
1342       return 0;
1343     } else return 0;
1344   return 0;  
1345 }
1346
1347
1348         
1349 //______________________________________________________________________________
1350 ULong_t AliITSHandleDaSSD::OffsetValue(const AliITSChannelDaSSD *strip, 
1351                                        const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1352 {
1353 // Calculate the offset value to be upload to FEROM     
1354   Int_t  pedint;
1355   if (fOffsetDefault < INT_MAX) pedint = fOffsetDefault;
1356   else pedint = TMath::Nint(strip->GetPedestal());
1357   if (pedint > static_cast<Int_t>((fgkOffSetBitMask >> 1))) {
1358     if (!ChannelIsBad(ddl, ad, adc, strn) && !((fMergeBCLists) && (EvaluateIfChannelIsBad(GetModule(ddl, ad, adc), strn)))) 
1359       AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i  can not be represented with mask 0x%s, Offset = %i",
1360                    pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(), (fgkOffSetBitMask >> 1)));
1361     return (fgkOffSetBitMask >> 1);
1362   }  
1363   if ((-pedint) > static_cast<Int_t>(((fgkOffSetBitMask + 1) >> 1))) {
1364     if (!ChannelIsBad(ddl, ad, adc, strn) && !((fMergeBCLists) && (EvaluateIfChannelIsBad(GetModule(ddl, ad, adc), strn)))) 
1365       AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i  can not be represented with mask 0x%s, Offset = %i", 
1366                pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(), 
1367                ((fgkOffSetBitMask & (~fgkOffSetBitMask >> 1)) - fgkOffSetBitMask - 1)));
1368     return fgkOffSetBitMask & (~fgkOffSetBitMask >> 1);
1369   }
1370   return fgkOffSetBitMask & (pedint >= 0 ? pedint : pedint + fgkOffSetBitMask + 1);
1371 }
1372
1373
1374
1375 //______________________________________________________________________________
1376 ULong_t AliITSHandleDaSSD::OffsetValue(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1377 {
1378 // Calculate the offset value to be upload to FEROM     
1379   AliITSChannelDaSSD    *strip = NULL;
1380   AliITSModuleDaSSD     *module = NULL;
1381   if ((module = GetModule(ddl, ad, adc))) {
1382     if ((strip = module->GetStrip(strn))) return OffsetValue(strip, ddl, ad, adc, strn);
1383     else {
1384       AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i,  strip = %i, 0 is used!", ddl, ad, adc, strn));
1385       return 0ul;
1386     }
1387   } else {
1388     AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i, 0 is used!", ddl, ad, adc));
1389     return 0ul;
1390   }  
1391 }
1392
1393
1394
1395 //______________________________________________________________________________
1396 ULong_t AliITSHandleDaSSD::ZsThreshold(const AliITSChannelDaSSD *strip) const
1397
1398 // Calculate the value of zero suppression threshold to be upload to FEROM
1399   ULong_t zs;
1400   if (fZsDefault < 0) {
1401     zs = TMath::Nint(fZsFactor * strip->GetNoiseCM());
1402     if (zs < static_cast<ULong_t>(fZsMinimum)) zs = static_cast<ULong_t>(fZsMinimum);
1403   }
1404   else zs = fZsDefault;
1405   return (zs < fgkZsBitMask) ? (zs & fgkZsBitMask) : fgkZsBitMask;
1406 }
1407
1408
1409 //______________________________________________________________________________
1410 ULong_t AliITSHandleDaSSD::ZsThreshold(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1411 {
1412 // Calculate the value of zero suppression threshold to be upload to FEROM, account bad channels list
1413   AliITSChannelDaSSD    *strip = NULL;
1414   AliITSModuleDaSSD     *module = NULL;
1415   if (ChannelIsBad(ddl, ad, adc, strn)) return fgkZsBitMask;
1416   if (LadderIsOff(ddl, ad, adc)) return fgkZsBitMask;
1417   if (fZsDefault > 0) if (static_cast<ULong_t>(fZsDefault) >= fgkZsBitMask) return fgkZsBitMask;
1418   if ((module = GetModule(ddl, ad, adc))) {
1419         if (fMergeBCLists) if (EvaluateIfChannelIsBad(module, strn)) return fgkZsBitMask;
1420     if ((strip = module->GetStrip(strn)))  return ZsThreshold(strip);
1421     else {
1422       AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i,  strip = %i, 0 is used!", ddl, ad, adc, strn));
1423       return 0ul;
1424     }
1425   } else {
1426     AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i, 0 is used!", ddl, ad, adc));
1427     return 0ul;
1428   }
1429 }
1430
1431             
1432 //______________________________________________________________________________
1433 string AliITSHandleDaSSD::ConvBase(const unsigned long value, const long base) const
1434 {
1435 // Converts the unsigned long number into that in another base 
1436   string digits = "0123456789ABCDEF";
1437   string result;
1438   unsigned long v = value;
1439   if((base < 2) || (base > 16)) {
1440     result = "Error: base out of range.";
1441   }
1442   else {
1443     int i = 0;
1444     do {
1445       result = digits[v % base] + result;
1446       v /= base;
1447       i++;
1448     }
1449     while((v) || (i<8));
1450   }
1451   return result;
1452 }
1453
1454
1455
1456 //______________________________________________________________________________
1457 Int_t AliITSHandleDaSSD::CheckOffChips() const
1458 {
1459 // Check if the chip, module are off
1460   AliITSChannelDaSSD *strip;
1461   Int_t       offthreshold;
1462   Int_t       strnd, chipnd, modnd, stroff, chipoff, modoff;
1463   offthreshold = TMath::Nint(fZsMinimum/fZsFactor);
1464   modnd = modoff = 0;
1465   for (Int_t mi = 0; mi < fNumberOfModules; mi++) {
1466     if (!fModules[mi]) { modnd++; continue; }
1467     if (fModules[mi]->GetModuleId() < 0) continue;
1468     if (LadderIsOff(fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()) ) continue;
1469     chipoff = chipnd = 0;
1470     for (Int_t chipind = 0; chipind < AliITSModuleDaSSD::GetChipsPerModuleConst(); chipind++) {
1471       strnd = stroff = 0;
1472       Int_t stripind = chipind * AliITSModuleDaSSD::GetStripsPerChip();
1473       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1474         if (!(strip = fModules[mi]->GetStrip(strind))) { strnd++;  continue; }
1475         if (strip->GetNoiseCM() < offthreshold ) stroff++;
1476       }
1477       if (strnd == AliITSModuleDaSSD::GetStripsPerChip()) chipnd++;
1478       else if (stroff == AliITSModuleDaSSD::GetStripsPerChip()) chipoff++;
1479       else if ((stroff + strnd) == AliITSModuleDaSSD::GetStripsPerChip()) chipoff++;
1480     }
1481     if ((!chipoff) && (!chipnd)) continue;
1482     if (chipnd == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1483       AliInfo(Form("Module: (ddl/ad/adc) %i/%i/%i seems to be off and it is not on the ladders which are off!", 
1484                fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1485       modnd++;
1486     }
1487     if (chipoff == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1488       AliInfo(Form("Module (ddl/ad/adc): %i/%i/%i seems to be off and it is not on the ladders which are off!", 
1489                fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1490       modoff++;
1491     }
1492     else if ((chipoff + chipnd) == AliITSModuleDaSSD::GetChipsPerModuleConst()) {
1493       AliInfo(Form("Module: (ddl/ad/adc): %i/%i/%i seems to be off and it is not on the ladders which are off!", 
1494                fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC()));
1495       modoff++;
1496     }
1497     else if (chipoff) {
1498       AliInfo(Form("Module: (ddl/ad/adc): %i/%i/%i has %i chips which are off!", 
1499                fModules[mi]->GetDdlId(), fModules[mi]->GetAD(), fModules[mi]->GetADC(), chipoff));
1500       modoff++;
1501     }
1502   }
1503   return (modoff + modnd);
1504 }
1505
1506
1507 //______________________________________________________________________________
1508 Bool_t AliITSHandleDaSSD::CalculatePedNoiseW(const AliITSModuleDaSSD *const module)
1509 {
1510 // Calculates Pedestal and Noise using Welford algorithm
1511   AliITSChannelDaSSD *strip;
1512   Double_t            pedestal, noise, p0, s0;
1513   Short_t            *signal;
1514   Int_t               ovev, n;
1515   if (!module) return kFALSE;
1516   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1517     if (!(strip = module->GetStrip(strind))) continue;
1518     if (!(signal = strip->GetSignal())) {
1519       AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
1520                      module->GetModuleId(), strind));
1521       continue; 
1522     }
1523 //************* pedestal and noise first pass ****************
1524     pedestal = p0 = noise = 0.0L;
1525     ovev = 0;
1526     for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1527       if (SignalOutOfRange(signal[ev])) ovev += 1;
1528       else 
1529         if (!(ev - ovev)) {
1530           pedestal = p0 = signal[ev];
1531           noise = 0.0L;
1532         } else {
1533           p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1534           s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1535           pedestal = p0;
1536           noise = s0;
1537         }
1538     }
1539     if (strip->GetEventsNumber() == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
1540     strip->SetPedestal(static_cast<Float_t>(pedestal));
1541     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) 
1542       strip->SetNoise( static_cast<Float_t>(TMath::Sqrt(noise / static_cast<Double_t>(n))) );
1543     else {
1544       strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
1545       continue;
1546     }
1547 //************* Second pass excluds event with |p - s|>f*noise *****************
1548     pedestal = p0 = noise = 0.0L;
1549     ovev = 0;
1550     for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1551       if (   SignalOutOfRange(signal[ev]) 
1552           || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
1553       else
1554         if (!(ev - ovev)) {
1555           pedestal = p0 = signal[ev];
1556           noise = 0.0L;
1557         } else {
1558           p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1559           s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1560           pedestal = p0;
1561           noise = s0;
1562         }
1563     }      
1564     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = TMath::Sqrt(noise / static_cast<Double_t>(n));
1565     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
1566     strip->SetNoise(static_cast<Float_t>(noise));
1567     if (strip->GetEventsNumber() == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
1568     strip->SetPedestal(static_cast<Float_t>(pedestal));
1569     strip->SetOverflowNumber(ovev);     
1570   }
1571   return kTRUE;
1572 }
1573
1574
1575 //______________________________________________________________________________
1576 Bool_t AliITSHandleDaSSD::CalculateCMW(AliITSModuleDaSSD *const module)
1577 {
1578 // Calculates CM using Welford algorithm
1579   AliITSChannelDaSSD  *strip = NULL;
1580   Short_t             *signal;
1581   Int_t                ovstr, n;
1582   Int_t                stripind;
1583   Double_t             cm0, cm1, cmsigma, cms1;
1584   module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
1585   for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
1586     stripind = chipind * module->GetStripsPerChip();
1587     module->GetCM()[chipind].Set(fNumberOfEvents);
1588     module->GetCM()[chipind].Reset(0.0f);
1589     for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
1590     // calculate firs approximation of CM and SigmaCM.
1591       cm0 = cm1 = cmsigma = 0.0L;
1592       ovstr = 0;
1593       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1594         if (!(strip = module->GetStrip(strind))) { ovstr += 1; continue; } //return kFALSE; 
1595         if (!(signal = strip->GetSignal())) { ovstr += 1; continue; }  //return kFALSE; 
1596         if ((SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
1597         else {
1598           if (!(strind - stripind - ovstr)) {
1599           cm0 = cm1 = signal[ev] - strip->GetPedestal();
1600           cmsigma = 0.0L;
1601         } else {
1602           cm1 = cm0 + (signal[ev] - strip->GetPedestal() - cm0) / static_cast<Double_t>(strind - stripind - ovstr + 1);
1603           cms1 = cmsigma + (signal[ev] - strip->GetPedestal() - cm0) * (signal[ev] - strip->GetPedestal() - cm1);
1604           cm0 = cm1;
1605           cmsigma = cms1;
1606         } }
1607       }
1608       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr - 1) > 0) cmsigma = TMath::Sqrt(cmsigma / static_cast<Double_t>(n));
1609       else {
1610         AliWarning(Form("AliITSHandleDaSSD: Too little number of strips have a signal for module:chip:event : [%d]:[%d]:[%ld]\n",
1611                    module->GetModuleId(), chipind, ev));
1612         if (!(module->SetCM(0.0f, chipind, ev))) 
1613           AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
1614                    module->GetModuleId(), chipind, ev));
1615         continue;
1616       }
1617    // calculate cm with threshold
1618       Double_t cmsum = 0.0L;
1619       ovstr = 0;
1620       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
1621         if (!(strip = module->GetStrip(strind))) { ovstr += 1; continue; }
1622         if (!(signal = strip->GetSignal())) { ovstr += 1; continue; }
1623         if ( (SignalOutOfRange(signal[ev])) || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue()) 
1624                || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
1625         else cmsum += (signal[ev] - strip->GetPedestal());
1626       }
1627       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Double_t)(n);
1628       else cmsum = 0.0L;
1629       if (!(module->SetCM(static_cast<Float_t>(cmsum), chipind, ev))) 
1630         AliError(Form("AliITSHandleDaSSD: Error, module->SetCM(...) returned kFALSE module:chip:event : [%d]:[%d]:[%ld]\n",
1631                  module->GetModuleId(), chipind, ev));
1632     } 
1633   }
1634   return kTRUE; 
1635 }
1636
1637
1638 //______________________________________________________________________________
1639 Bool_t AliITSHandleDaSSD::CalculateNoiseCMW(AliITSModuleDaSSD *const module)
1640 {
1641 // Calculates Noise with CM correction
1642   AliITSChannelDaSSD  *strip = NULL;
1643   Short_t     *signal;
1644   Int_t        ovev, n;
1645   if (!CalculateCMW(module)) { 
1646     AliError("Error: AliITSHandleDaSSD::CalculateCMW() returned kFALSE");
1647     return kFALSE;
1648   }  
1649   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1650     if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
1651     if (!(signal = strip->GetSignal())) {
1652       strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
1653       AliError(Form("AliITSHandleDaSSD: Error CalculateNoiseCMW(): there are no events data for module[%i] strip[%i]->GetSignal()", 
1654                      module->GetModuleId(), strind));
1655       continue; //return kFALSE;
1656     }
1657 //** To get exactly the same set of events as for pedestal and noise calculation **
1658     Double_t pedestal, noise, p0, s0;
1659     pedestal = p0 = noise = 0.0L;
1660     ovev = 0;
1661     for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1662       if (SignalOutOfRange(signal[ev])) ovev += 1;
1663       else 
1664         if (!(ev - ovev)) {
1665           pedestal = p0 = signal[ev];
1666           noise = 0.0L;
1667         } else {
1668           p0 = pedestal + (signal[ev] - pedestal) / static_cast<Double_t>(ev - ovev + 1);
1669           s0 = noise + (signal[ev] - pedestal) * (signal[ev] - p0);
1670           pedestal = p0;
1671           noise = s0;
1672         }
1673     }
1674     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise = TMath::Sqrt(noise / static_cast<Double_t>(n));
1675     else  {
1676           strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
1677           continue;
1678     }
1679 //** Calculation of CM corrected noise **
1680     Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
1681     Double_t nsum = 0.0L;
1682     ovev = 0;
1683     for (Int_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
1684       if (   SignalOutOfRange(signal[ev])
1685           || TMath::Abs(signal[ev] - pedestal) > (fPedestalThresholdFactor * noise)) ovev += 1;
1686       else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
1687     } 
1688     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  TMath::Sqrt(nsum / static_cast<Double_t>(n));
1689     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
1690     strip->SetNoiseCM(static_cast<Float_t>(noise));
1691   }
1692   return kTRUE;
1693 }
1694
1695
1696
1697 //______________________________________________________________________________
1698 UChar_t AliITSHandleDaSSD::EvaluateIfChannelIsBad(const AliITSModuleDaSSD *const module, const Int_t stripn) const
1699 {
1700 //Applies the bad channel creteria and set the appropriate flags for returned value
1701   AliITSChannelDaSSD  *strip = 0;
1702   UInt_t               bcflags = 0;
1703   if (fZsDefault >= 0) { if (static_cast<ULong_t>(fZsDefault) >= fgkZsBitMask) bcflags |= 3; }
1704   else if (static_cast<ULong_t>(fZsMinimum) >= fgkZsBitMask) bcflags |= 3;
1705   if (LadderIsOff(module->GetDdlId(), module->GetAD(), module->GetADC()) )  bcflags |= 3;
1706   
1707   if (!(strip = module->GetStrip(stripn))) bcflags |= 3;
1708   else {
1709     if (strip->GetNoiseCM() == AliITSChannelDaSSD::GetUndefinedValue()) bcflags |= 8;
1710     if (static_cast<ULong_t>(TMath::Nint(fZsFactor * strip->GetNoiseCM())) >= fgkZsBitMask) bcflags |= 8;
1711     if (strip->GetNoiseCM() < 1) bcflags |= 16;
1712     if (strip->GetPedestal() > ((fgkOffSetBitMask >> 1) - 1))  bcflags |= 4;
1713     else if ((-(strip->GetPedestal())) > (fgkOffSetBitMask >> 1))  bcflags |= 4;
1714     if (bcflags) bcflags |= 3;
1715   }
1716   return bcflags;
1717 }
1718