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