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