]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSHandleDaSSD.cxx
first steps to set the covariance matrix from the errors calculated in conformal...
[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: 19/05/2008
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <string>
27 #include <fstream>
28 #include <Riostream.h> 
29 #include "AliITSHandleDaSSD.h"
30 #include <math.h>
31 #include <limits.h>
32 #include "event.h"
33 #include "TFile.h"
34 #include "TString.h"
35 #include "TObjArray.h"
36 #include "AliLog.h"
37 #include "AliITSNoiseSSD.h"
38 #include "AliITSPedestalSSD.h"
39 #include "AliITSBadChannelsSSD.h"
40 #include "AliITSRawStreamSSD.h"
41 #include "AliRawReaderDate.h"
42 #include "AliITSRawStreamSSD.h"
43 #include "AliITSChannelDaSSD.h"
44
45
46 ClassImp(AliITSHandleDaSSD)
47
48 using namespace std;
49
50
51 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModules = 1698;       // Number of SSD modules in ITS
52 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerDdl = 108;  // Number of SSD modules in DDL
53 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerSlot = 12;  // Number of SSD modules in Slot
54 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDSlotsPerDDL = 9;      // Number of SSD slots per DDL
55 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDDDLs = 16;            // Number of SSD modules in Slot
56 const Float_t  AliITSHandleDaSSD::fgkPedestalThresholdFactor = 3.0;   // Defalt value for fPedestalThresholdFactor 
57 const Float_t  AliITSHandleDaSSD::fgkCmThresholdFactor = 3.0;         // Defalt value for fCmThresholdFactor
58
59 const UInt_t   AliITSHandleDaSSD::fgkZsBitMask      = 0x0000003F;     // Bit mask for FEROM ZS
60 const UInt_t   AliITSHandleDaSSD::fgkOffSetBitMask  = 0x000003FF;     // Bit mask for FEROM Offset correction
61 const UInt_t   AliITSHandleDaSSD::fgkBadChannelMask = 0x00000001;     // Mask to suppress the channel from the bad channel list
62 const Int_t    AliITSHandleDaSSD::fgkAdcPerDBlock   = 6;              // FEROM configuration file constant
63
64 //______________________________________________________________________________
65 AliITSHandleDaSSD::AliITSHandleDaSSD() :
66   fRawDataFileName(NULL),
67   fNumberOfModules(0),
68   fModules(NULL),
69   fModIndProcessed(0),
70   fModIndRead(0),
71   fModIndex(NULL),
72   fNumberOfEvents(0),
73   fStaticBadChannelsMap(NULL),
74   fDDLModuleMap(NULL),
75   fLdcId(0),
76   fRunId(0),
77   fPedestalThresholdFactor(fgkPedestalThresholdFactor),
78   fCmThresholdFactor(fgkCmThresholdFactor),
79   fZsDefault(-1),
80   fOffsetDefault(INT_MAX),
81   fZsFactor(3.0)
82 {
83 // Default constructor
84 }
85
86
87 //______________________________________________________________________________
88 AliITSHandleDaSSD::AliITSHandleDaSSD(Char_t *rdfname) :
89   fRawDataFileName(NULL),
90   fNumberOfModules(0),
91   fModules(NULL),
92   fModIndProcessed(0),
93   fModIndRead(0),
94   fModIndex(NULL),
95   fNumberOfEvents(0),
96   fStaticBadChannelsMap(NULL),
97   fDDLModuleMap(NULL),
98   fLdcId(0),
99   fRunId(0),
100   fPedestalThresholdFactor(fgkPedestalThresholdFactor) ,
101   fCmThresholdFactor(fgkCmThresholdFactor),
102   fZsDefault(-1),
103   fOffsetDefault(INT_MAX),
104   fZsFactor(3.0)
105 {
106   if (!Init(rdfname)) AliError("AliITSHandleDaSSD::AliITSHandleDaSSD() initialization error!");
107 }
108
109
110 //______________________________________________________________________________
111 AliITSHandleDaSSD::AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc) :
112   TObject(ssdadldc),
113   fRawDataFileName(ssdadldc.fRawDataFileName),
114   fNumberOfModules(ssdadldc.fNumberOfModules),
115   fModules(NULL),
116   fModIndProcessed(ssdadldc.fModIndProcessed),
117   fModIndRead(ssdadldc.fModIndRead),
118   fModIndex(NULL),
119   fNumberOfEvents(ssdadldc.fNumberOfEvents),
120   fStaticBadChannelsMap(ssdadldc.fStaticBadChannelsMap),
121   fDDLModuleMap(ssdadldc.fDDLModuleMap),
122   fLdcId(ssdadldc.fLdcId),
123   fRunId(ssdadldc.fRunId),
124   fPedestalThresholdFactor(ssdadldc.fPedestalThresholdFactor),
125   fCmThresholdFactor(ssdadldc.fCmThresholdFactor),
126   fZsDefault(ssdadldc.fZsDefault),
127   fOffsetDefault(ssdadldc.fOffsetDefault),
128   fZsFactor(ssdadldc.fZsFactor)
129 {
130   // copy constructor
131   if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
132     fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
133     if (fModules) {
134       for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
135         if (ssdadldc.fModules[modind]) {
136           fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
137           if (!fModules[modind]) { 
138             AliError("AliITSHandleDaSSD: Error copy constructor");
139             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
140             delete [] fModules;
141             fModules = NULL;
142             break;
143           }
144         } else fModules[modind] = NULL; 
145       }   
146     } else {
147       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
148       fNumberOfModules = 0;
149       fModules = NULL;
150     }
151   }
152 }
153
154
155 //______________________________________________________________________________
156 AliITSHandleDaSSD& AliITSHandleDaSSD::operator = (const AliITSHandleDaSSD& ssdadldc)
157 {
158 // assignment operator
159   if (this == &ssdadldc)  return *this;
160   if (fModules) {
161     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
162     delete [] fModules; 
163     fModules = NULL;
164   }
165   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
166   if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
167     fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
168     if (fModules) {
169       for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
170         if (ssdadldc.fModules[modind]) {
171           fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
172           if (!fModules[modind]) { 
173             AliError("AliITSHandleDaSSD: Error assignment operator");
174             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
175             delete [] fModules;
176             fModules = NULL;
177             break;
178           }
179         } else fModules[modind] = NULL; 
180       }   
181     } else {
182       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
183       fNumberOfModules = 0;
184       fModules = NULL;
185     }
186   }
187   return *this;
188 }
189
190
191 //______________________________________________________________________________
192 AliITSHandleDaSSD::~AliITSHandleDaSSD()
193 {
194 // Default destructor 
195   if (fModules) 
196   {
197     for (Int_t i = 0; i < fNumberOfModules; i++)
198     { 
199       if (fModules[i]) delete fModules[i];
200     }
201     delete [] fModules;
202   }
203   if (fModIndex) delete [] fModIndex;
204   if (fStaticBadChannelsMap) { fStaticBadChannelsMap->Delete(); delete fStaticBadChannelsMap; }
205   if (fDDLModuleMap) delete [] fDDLModuleMap;
206 }
207
208
209
210 //______________________________________________________________________________
211 void AliITSHandleDaSSD::Reset()
212 {
213 // Delete array of AliITSModuleDaSSD* objects. 
214   if (fModules) {
215     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
216     delete [] fModules;
217     fModules = NULL;
218   }
219   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
220 /*
221   if (fStaticBadChannelsMap) {
222     fStaticBadChannelsMap->Delete();
223     delete fStaticBadChannelsMap;
224     fStaticBadChannelsMap = NULL; 
225   }    
226   if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL; }
227 */
228   fRawDataFileName = NULL;
229   fModIndProcessed = fModIndRead = 0;
230   fNumberOfEvents = 0;
231   fLdcId = fRunId = 0;
232   fPedestalThresholdFactor = fgkPedestalThresholdFactor;
233   fCmThresholdFactor = fgkCmThresholdFactor;
234 }
235
236
237
238 //______________________________________________________________________________
239 Bool_t AliITSHandleDaSSD::Init(Char_t *rdfname)
240 {
241 // Read raw data file and set initial and configuration parameters
242   Long_t physeventind = 0, strn = 0, nofstripsev, nofstrips = 0;
243   Int_t  nofeqipmentev, nofeqipment = 0, eqn = 0; 
244   AliRawReaderDate  *rawreaderdate = NULL;
245   UChar_t *data = NULL;
246   Long_t datasize = 0, eqbelsize = 1;
247
248   rawreaderdate = new AliRawReaderDate(rdfname, 0);
249   if (!(rawreaderdate->GetAttributes() || rawreaderdate->GetEventId())) {
250     AliError(Form("AliITSHandleDaSSD: Error reading raw data file %s by RawReaderDate", rdfname));
251     MakeZombie();
252     return kFALSE;
253   }
254   if (rawreaderdate->NextEvent()) {
255     fRunId = rawreaderdate->GetRunNumber(); 
256     rawreaderdate->RewindEvents();
257   } else { MakeZombie(); return kFALSE; }
258   if (fModules) Reset();
259   rawreaderdate->SelectEvents(-1);
260   rawreaderdate->Select("ITSSSD");  
261   nofstrips = 0;
262   while (rawreaderdate->NextEvent()) {
263     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
264     nofstripsev = 0;
265     nofeqipmentev = 0;
266     while (rawreaderdate->ReadNextData(data)) {
267       fLdcId = rawreaderdate->GetLDCId();
268       nofeqipmentev += 1;
269       datasize = rawreaderdate->GetDataSize();
270       eqbelsize = rawreaderdate->GetEquipmentElementSize();
271       if ( datasize % eqbelsize ) {
272         AliError(Form("AliITSHandleDaSSD: Error Init(%s): event data size %i is not an integer of equipment data size %i", 
273                                     rdfname, datasize, eqbelsize));
274         MakeZombie();
275             return kFALSE;
276       }
277       nofstripsev += (Int_t) (datasize / eqbelsize);
278     }
279     if (physeventind++) {
280       if (nofstrips != nofstripsev) AliWarning(Form("AliITSHandleDaSSD: number of strips varies from event to event, ev = %i, %i", 
281                                                      physeventind, nofstripsev));
282       if (nofeqipment != nofeqipmentev) AliWarning("AliITSHandleDaSSD: number of DDLs varies from event to event");
283     }
284     nofstrips = nofstripsev;
285     nofeqipment = nofeqipmentev;
286     if (strn < nofstrips)  strn = nofstrips;
287     if (eqn < nofeqipment) eqn = nofeqipment;
288   }
289   delete rawreaderdate;
290   if ((physeventind > 0) && (strn > 0))
291   {
292     fNumberOfEvents = physeventind;
293     fRawDataFileName = rdfname;
294     fModIndex = new (nothrow) Int_t [fgkNumberOfSSDModulesPerDdl * eqn];
295     if (fModIndex) 
296       for (Int_t i = 0; i < fgkNumberOfSSDModulesPerDdl * eqn; i++) fModIndex[i] = -1; 
297     else AliWarning(Form("AliITSHandleDaSSD: Error Init(%s): Index array for %i modules was not created", 
298                                      rdfname, fgkNumberOfSSDModulesPerDdl * eqn));
299     if (SetNumberOfModules(fgkNumberOfSSDModulesPerDdl * eqn)) {
300       TString str = TString::Format("Max number of equipment: %i, max number of channels: %i\n", eqn, strn);
301       DumpInitData(str.Data());
302       return kTRUE;
303     }  
304   }
305   MakeZombie();
306   return kFALSE;
307 }
308
309
310
311 //______________________________________________________________________________
312 AliITSModuleDaSSD* AliITSHandleDaSSD::GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
313 {
314 // Retrieve AliITSModuleDaSSD object from the array
315    if (!fModules) return NULL;
316    for (Int_t i = 0; i < fNumberOfModules; i++) {
317      if (fModules[i]) {
318        if (    (fModules[i]->GetDdlId() == ddlID)
319             && (fModules[i]->GetAD() == ad)
320             && (fModules[i]->GetADC() == adc))
321             return fModules[i];
322      }      
323    }
324    return NULL;
325 }
326
327
328 Int_t AliITSHandleDaSSD::GetModuleIndex (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
329 {
330 // Retrieve the position of AliITSModuleDaSSD object in the array
331    if (!fModules) return -1;
332    for (Int_t i = 0; i < fNumberOfModules; i++) {
333      if (fModules[i]) {
334        if (    (fModules[i]->GetDdlId() == ddlID)
335             && (fModules[i]->GetAD() == ad)
336             && (fModules[i]->GetADC() == adc))
337             return i;
338      }      
339    }
340    return -1;
341 }
342
343
344
345 //______________________________________________________________________________
346 AliITSChannelDaSSD* AliITSHandleDaSSD::GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const
347 {
348 // Retrieve AliITSChannalDaSSD object from the array
349   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
350     if (    (fModules[modind]->GetDdlId() == ddlID)
351          && (fModules[modind]->GetAD() == ad)
352          && (fModules[modind]->GetADC() == adc)  ) 
353         {
354        return fModules[modind]->GetStrip(stripID);
355     }
356   }
357   AliError(Form("AliITSHandleDaSSD: Error GetStrip (%i, %i, %i, %i), strip not found, returns NULL!",
358                 ddlID, ad, adc, stripID));
359   return NULL;
360 }
361
362
363
364 //______________________________________________________________________________
365 Bool_t AliITSHandleDaSSD::SetModule(AliITSModuleDaSSD *const module, const Int_t index)
366
367 // Assign array element with AliITSModuleDaSSD object
368    if ((index < fNumberOfModules) && (index >= 0)) 
369      { 
370         if (fModules[index]) delete fModules[index];
371         fModules[index] = module;
372         return kTRUE;
373       }
374    else return kFALSE;                         
375 }
376
377
378
379 //______________________________________________________________________________
380 Bool_t AliITSHandleDaSSD::SetNumberOfModules (const Int_t numberofmodules)
381 {
382 // Allocates memory for AliITSModuleDaSSD objects
383   if (numberofmodules > fgkNumberOfSSDModules)
384     AliWarning(Form("AliITSHandleDaSSD: the number of modules %i you use exceeds the maximum %i for ALICE ITS SSD", 
385                      numberofmodules, fgkNumberOfSSDModules));
386   if (fModules) {
387     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
388     delete [] fModules; 
389     fModules = NULL;
390   }
391   fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
392   if (fModules) {
393     fNumberOfModules = numberofmodules;
394     memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
395     return kTRUE;
396   } else {
397      AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules));
398      fNumberOfModules = 0;
399      fModules = NULL;
400   }
401   return kFALSE;
402 }
403
404
405
406 //______________________________________________________________________________
407 Bool_t AliITSHandleDaSSD::ReadStaticBadChannelsMap(const Char_t *filename)
408 {
409 // Reads Static Bad Channels Map from the file
410   TFile *bcfile;
411   if (!filename) {
412     AliWarning("No file name is specified for Static Bad Channels Map!");
413     return kFALSE;
414   } 
415   bcfile = new TFile(filename, "READ");
416   if (bcfile->IsZombie()) {
417     AliWarning(Form("Error reading file %s with Static Bad Channels Map!", filename));
418     return kFALSE;
419   }
420   bcfile->GetObject("BadChannels;1", fStaticBadChannelsMap);
421   if (!fStaticBadChannelsMap) {
422     AliWarning("Error fStaticBadChannelsMap == NULL!");
423     bcfile->Close();
424     delete bcfile;
425     return kFALSE;
426   }
427   bcfile->Close();
428   delete bcfile;
429   return kTRUE;
430 }
431
432
433
434 Bool_t AliITSHandleDaSSD::ReadDDLModuleMap(const Char_t *filename)
435 {
436 // Reads the SSD DDL Map from the file
437   ifstream             ddlmfile;
438   AliRawReaderDate    *rwr = NULL;
439   AliITSRawStreamSSD  *rsm = NULL;
440   void                *event = NULL;
441   if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
442   fDDLModuleMap = new (nothrow) Int_t [fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl];
443   if (!fDDLModuleMap) {
444     AliWarning("Error allocation memory for DDL Map!");
445     return kFALSE;
446   }    
447   if (!filename) {
448     AliWarning("No file name is specified for SSD DDL Map, using the one from AliITSRawStreamSSD!");
449     rwr = new AliRawReaderDate(event);
450     rsm = new AliITSRawStreamSSD(rwr);
451     rsm->Setv11HybridDDLMapping();
452     for (Int_t ddli = 0; ddli < fgkNumberOfSSDDDLs; ddli++)
453       for (Int_t mi = 0; mi < fgkNumberOfSSDModulesPerDdl; mi++)
454         fDDLModuleMap[(ddli * fgkNumberOfSSDModulesPerDdl + mi)] = rsm->GetModuleNumber(ddli, mi);
455     if (rsm) delete rsm;
456     if (rwr) delete rwr;        
457     return kTRUE;
458   } 
459   ddlmfile.open(filename, ios::in);
460   if (!ddlmfile.is_open()) {
461     AliWarning(Form("Error reading file %s with SSD DDL Map!", filename));
462     if (fDDLModuleMap) { delete [] fDDLModuleMap; fDDLModuleMap = NULL;}
463     return kFALSE;
464   }
465   Int_t ind = 0;
466   while((!ddlmfile.eof()) && (ind < (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))) {
467     ddlmfile >> fDDLModuleMap[ind++];
468     if (ddlmfile.fail()) AliError(Form("Error extracting number from the DDL map file %s, ind: ", filename, ind));
469   }
470   if (ind != (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl))
471     AliWarning(Form("Only %i (< %i) entries were read from DDL Map!", ind, (fgkNumberOfSSDDDLs * fgkNumberOfSSDModulesPerDdl)));
472   ddlmfile.close();
473   return kTRUE;
474 }
475
476
477
478 //______________________________________________________________________________
479 Int_t AliITSHandleDaSSD::ReadCalibrationDataFile (char* fileName, const Long_t eventsnumber)
480 {
481 // Reads raw data from file
482   if (!Init(fileName)){
483     AliError("AliITSHandleDaSSD: Error ReadCalibrationDataFile");
484     return kFALSE;
485   }
486   fNumberOfEvents = eventsnumber;
487   return ReadModuleRawData (fNumberOfModules);  
488 }
489
490
491
492 //______________________________________________________________________________
493 Int_t AliITSHandleDaSSD::ReadModuleRawData (const Int_t modulesnumber)
494 {
495 // Reads raw data from file
496   AliRawReader        *rawreaderdate = NULL;
497   AliITSRawStreamSSD  *stream = NULL;
498   AliITSModuleDaSSD   *module;
499   AliITSChannelDaSSD  *strip;
500   Long_t            eventind = 0;
501   Int_t             nofeqipmentev, equipid, prequipid;
502   UShort_t          modind;
503   if (!(rawreaderdate = new AliRawReaderDate(fRawDataFileName, 0))) return 0;
504   if (!fModules) {
505     AliError("AliITSHandleDaSSD: Error ReadModuleRawData: no structure was allocated for data");
506     return 0;
507   }
508   if (!fDDLModuleMap) if (!ReadDDLModuleMap()) AliWarning("DDL map is not defined, ModuleID will be set to 0!");
509   stream = new AliITSRawStreamSSD(rawreaderdate);
510   stream->Setv11HybridDDLMapping();
511   rawreaderdate->SelectEvents(-1);
512   modind = 0;
513   while (rawreaderdate->NextEvent()) {
514     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
515     nofeqipmentev = 0;
516     prequipid = -1;
517     while (stream->Next()) {
518       equipid    = rawreaderdate->GetEquipmentId(); 
519       if ((equipid != prequipid) && (prequipid >= 0)) nofeqipmentev += 1;
520       prequipid = equipid;
521       Int_t     equiptype  = rawreaderdate->GetEquipmentType();
522       UChar_t   ddlID      = (UChar_t)rawreaderdate->GetDDLID();
523       UChar_t   ad      = stream->GetAD();
524       UChar_t   adc     = stream->GetADC();
525       UShort_t  stripID = stream->GetSideFlag() ? AliITSChannelDaSSD::GetMaxStripIdConst() - stream->GetStrip() : stream->GetStrip();
526       Short_t   signal  = stream->GetSignal();
527
528       Int_t indpos = (nofeqipmentev * fgkNumberOfSSDModulesPerDdl)
529                    + ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
530       Int_t modpos = fModIndex[indpos];
531       if (((modpos > 0) && (modpos < fModIndRead)) || ((modpos < 0) && (modind == modulesnumber))) continue;
532       if ((modpos < 0) && (modind < modulesnumber)) {
533         module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
534         Int_t mddli;
535         if (fDDLModuleMap) mddli = RetrieveModuleId(ddlID, ad, adc);
536         else  mddli = 0;
537         if (!module->SetModuleIdData (ddlID, ad, adc, mddli)) return 0;
538     module->SetModuleRorcId (equipid, equiptype);
539         module->SetCMFeromEventsNumber(fNumberOfEvents);
540         modpos = fModIndRead + modind;
541         modind += 1;
542         fModules[modpos] = module;
543         fModIndex[indpos] = modpos;
544       } 
545       if (stripID < AliITSModuleDaSSD::GetStripsPerModuleConst()) {
546         if (!(strip = fModules[modpos]->GetStrip(stripID))) {
547           strip = new AliITSChannelDaSSD(stripID, fNumberOfEvents);
548           fModules[modpos]->SetStrip(strip, stripID);
549         }
550         strip->SetSignal(eventind, signal);
551       } else  fModules[modpos]->SetCMFerom(signal, (stripID - AliITSModuleDaSSD::GetStripsPerModuleConst()), eventind);
552     }
553     if (++eventind > fNumberOfEvents) break;
554   }
555   delete stream;
556   delete rawreaderdate;
557   if (modind) cout << "The memory was allocated for " <<  modind << " modules." << endl;
558   fModIndRead += modind;
559   if (modind < modulesnumber) RelocateModules();
560   return modind;
561 }
562
563
564
565 //______________________________________________________________________________
566 Bool_t AliITSHandleDaSSD::RelocateModules()
567 {
568 // Relocate memory for AliITSModuleDaSSD object array
569   Int_t         nm = 0;
570   AliITSModuleDaSSD **marray = NULL;
571   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
572     if (fModules[modind]) nm += 1;
573   if (nm == fNumberOfModules) return kTRUE;
574   marray = new (nothrow) AliITSModuleDaSSD* [nm];
575   if (!marray) {
576     AliError(Form("AliITSHandleDaSSD: Error relocating memory for %i AliITSModuleDaSSD* objects!", nm));
577     return kFALSE;
578   }
579   nm = 0;
580   for (Int_t modind = 0; modind < fNumberOfModules; modind++)  
581     if (fModules[modind]) marray[nm++] = fModules[modind];
582   delete [] fModules;
583   fModules = marray;
584   fNumberOfModules = fModIndRead = nm;
585   return kTRUE;
586 }
587
588
589
590 //______________________________________________________________________________
591 Bool_t AliITSHandleDaSSD::AddFeromCm(AliITSModuleDaSSD *const module)
592 // Restore the original signal value adding CM calculated and subtracted in ferom
593 {
594   AliITSChannelDaSSD *strip;
595   Short_t            *signal, *cmferom;
596
597   if (!module) return kFALSE;
598   for (Int_t chipind = 0; chipind < AliITSModuleDaSSD::GetChipsPerModuleConst(); chipind++) {
599     if (!(cmferom = module->GetCMFerom(chipind))) {
600       AliWarning(Form("AliITSHandleDaSSD: There is no Ferom CM values for chip %i, module %i!", chipind, module->GetModuleId()));
601       continue;
602     }
603     for (Int_t strind = (chipind * AliITSModuleDaSSD::GetStripsPerChip()); 
604                strind < ((chipind + 1) * AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
605       if (!(strip = module->GetStrip(strind))) continue;
606       if (!(signal = strip->GetSignal())) continue;
607 //      if (strip->GetEventsNumber() != module->GetEventsNumber()) return kFALSE;
608       Long_t ovev = 0;
609       for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
610         if (SignalOutOfRange(signal[ev]) || SignalOutOfRange(cmferom[ev])) ovev += 1;
611         else {
612           Short_t signal1 = signal[ev] + cmferom[ev];
613           strip->SetSignal(ev, signal1);
614         }  
615       } 
616     } 
617   }  
618   return kTRUE;
619 }
620
621
622
623 //______________________________________________________________________________
624 Bool_t AliITSHandleDaSSD::CalculatePedestal(AliITSModuleDaSSD *const module)
625 {
626 // Calculates Pedestal
627   AliITSChannelDaSSD *strip;
628   Float_t             pedestal, noise;
629   Short_t            *signal;
630   Long_t              ovev, ev, n;
631   if (!module) return kFALSE;
632   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
633     if (!(strip = module->GetStrip(strind))) continue;
634     if (!(signal = strip->GetSignal())) {
635       AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
636                      module->GetModuleId(), strind));
637       continue; 
638     }
639 //************* pedestal first pass ****************
640     pedestal = 0.0f;
641     ovev = 0l;
642     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
643       if (SignalOutOfRange(signal[ev])) ovev += 1;
644       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Float_t>(ev - ovev + 1) : signal[ev];
645     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
646     strip->SetPedestal(pedestal);       
647 //************* noise *******************************
648     Double_t nsum = 0.0L;
649     ovev = 0l;
650     for (ev = 0; ev < strip->GetEventsNumber(); ev++) {
651       if (SignalOutOfRange(signal[ev])) ovev += 1;
652       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
653     } 
654     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
655     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
656     strip->SetNoise(noise);
657 //************* pedestal second pass ****************
658     pedestal = 0.0f;
659     ovev = 0l;
660     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
661       if (   SignalOutOfRange(signal[ev]) 
662           || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
663       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Float_t>(ev - ovev + 1) : signal[ev];
664     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
665     strip->SetPedestal(pedestal);       
666     strip->SetOverflowNumber(ovev);     
667   }
668   return kTRUE;
669 }
670
671
672 //______________________________________________________________________________
673 Bool_t AliITSHandleDaSSD::CalculateNoise(AliITSModuleDaSSD *const module)
674 {
675 // Calculates Noise
676   AliITSChannelDaSSD *strip;
677   Short_t    *signal;
678   Float_t     noise;
679   Long_t      ovev, n;
680   if (!module) return kFALSE;
681   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
682     if (!(strip = module->GetStrip(strind))) continue;
683     if (!(signal = strip->GetSignal())) {
684       strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
685       AliError(Form("AliITSHandleDaSSD: Error CalculateNoise(): there are no events data for module[%i] strip[%i]->GetSignal()",
686                      module->GetModuleId(), strind));
687       continue;
688     }
689     Double_t nsum = 0.0L;
690     ovev = 0l;
691     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
692       if (SignalOutOfRange(signal[ev])) ovev += 1;
693       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
694     } 
695     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
696     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
697     strip->SetNoise(noise);
698   }
699   return kTRUE;
700 }
701
702
703
704 //______________________________________________________________________________
705 Bool_t AliITSHandleDaSSD::CalculateNoiseCM(AliITSModuleDaSSD *const module)
706 {
707 // Calculates Noise with CM correction
708   AliITSChannelDaSSD  *strip = NULL;
709   Short_t     *signal;
710   Float_t      noise;
711   Long_t       ovev, n;
712   if (!CalculateCM(module)) { 
713     AliError("Error: AliITSHandleDaSSD::CalculateCM() returned kFALSE");
714     return kFALSE;
715   }  
716   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
717     if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
718     if (!(signal = strip->GetSignal())) {
719       strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
720       AliError(Form("SSDDAModule: Error CalculateNoiseWithoutCM(): there are no events data for module[%i] strip[%i]->GetSignal()", 
721                      module->GetModuleId(), strind));
722       continue; //return kFALSE;
723     }
724     Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
725     Double_t nsum = 0.0L;
726     ovev = 0l;
727     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
728       if (SignalOutOfRange(signal[ev])) ovev += 1;
729       else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
730     } 
731     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
732     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
733     strip->SetNoiseCM(noise);
734   }
735   return kTRUE;
736 }
737
738
739
740 //______________________________________________________________________________
741 Bool_t AliITSHandleDaSSD::CalculateCM(AliITSModuleDaSSD *const module)
742 {
743 // Calculates CM
744   AliITSChannelDaSSD  *strip = NULL;
745   Short_t             *signal;
746   Long_t               ovstr, n;
747   Int_t                stripind;
748   module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
749   for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
750     stripind = chipind * module->GetStripsPerChip();
751     module->GetCM()[chipind].Set(fNumberOfEvents);
752     module->GetCM()[chipind].Reset(0.0f);
753     for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
754     // calculate firs approximation of CM.
755       Double_t cm0 = 0.0L;
756       ovstr = 0l;
757       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
758         if (!(strip = module->GetStrip(strind))) continue; //return kFALSE; 
759         if (!(signal = strip->GetSignal())) {
760           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()", 
761                         module->GetModuleId(), strind));
762           return kFALSE;
763         }
764         if ((signal[ev] >= AliITSChannelDaSSD::GetOverflowConst()) || 
765             (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
766         else cm0 += (signal[ev] - strip->GetPedestal());
767       }
768       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cm0 /= (Float_t)(n);
769       else { module->SetCM(0.0f, chipind, ev); continue; }
770     // calculate avarage (cm - (signal - pedestal)) over the chip
771       Double_t cmsigma = 0.0L;
772       ovstr = 0l;
773       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
774         if (!(strip = module->GetStrip(strind))) continue;
775         if (!(signal = strip->GetSignal())) {
776           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()\n",
777                          module->GetModuleId(), strind));
778           return kFALSE;
779         }
780         if ((signal[ev] >= AliITSChannelDaSSD::GetOverflowConst()) || 
781             (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
782         else cmsigma += pow((cm0 - (signal[ev] - strip->GetPedestal())), 2);
783       }
784       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsigma = sqrt(cmsigma / (Float_t)(n));
785       else { module->SetCM(0.0f, chipind, ev); continue; }
786    // calculate cm with threshold
787       Double_t cmsum = 0.0L;
788       ovstr = 0l;
789       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
790         if (!(strip = module->GetStrip(strind))) continue;
791         signal = strip->GetSignal();
792         if (   (signal[ev] >= AliITSChannelDaSSD::GetOverflowConst())
793             || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue()) 
794             || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
795         else cmsum += (signal[ev] - strip->GetPedestal());
796       }
797       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Float_t)(n);
798       else cmsum = 0.0L;
799       if (!module->SetCM(cmsum, chipind, ev));
800     } 
801   }
802   return kTRUE; 
803 }
804
805
806 //______________________________________________________________________________
807 Bool_t AliITSHandleDaSSD::ProcessRawData(const Int_t nmread)
808 {
809 // Performs calculation of calibration parameters (pedestal, noise, ...) 
810   Int_t nm = 0;
811   if (nmread <= 0) return kFALSE;
812   if (!fModules) return kFALSE;
813   while ((nm = ReadModuleRawData(nmread)) > 0) {
814     cout << "Processing next " << nm << " modules;" << endl;  
815     for (Int_t modind = fModIndProcessed; modind < fModIndRead; modind++) {
816       if (!fModules[modind]) {
817         AliError(Form("AliITSHandleDaSSD: Error ProcessRawData(): No AliITSModuleDaSSD object with index %i is allocated in AliITSHandleDaSSD\n",
818                        modind));
819         return kFALSE;
820       }
821       AddFeromCm(fModules[modind]);
822       CalculatePedestal(fModules[modind]);
823       CalculateNoise(fModules[modind]);
824       CalculateNoiseCM(fModules[modind]);
825     }
826     DeleteSignal();
827     DeleteCM();
828     DeleteCMFerom();
829     fModIndProcessed = fModIndRead;
830     cout << fModIndProcessed << " - done" << endl;
831   }
832   return kTRUE;  
833 }
834
835
836 //______________________________________________________________________________
837 Short_t AliITSHandleDaSSD::RetrieveModuleId(const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
838 {
839 // Retrieve ModuleId from the DDL map which is defined in AliITSRawStreamSSD class
840   if (!fDDLModuleMap) return 0;
841   Int_t mddli = ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
842   if ((ddlID < fgkNumberOfSSDDDLs) && (mddli < fgkNumberOfSSDModulesPerDdl)) {
843     mddli = fDDLModuleMap[ddlID * fgkNumberOfSSDModulesPerDdl + mddli];
844   }  
845   else {
846     AliWarning(Form("Module index  = %d or ddlID = %d is out of range 0 is rturned", ddlID, mddli));
847     mddli = 0;
848   }
849   if (mddli > SHRT_MAX) return SHRT_MAX;
850   else return (Short_t)mddli;
851 }
852
853
854
855 //______________________________________________________________________________
856 TObjArray* AliITSHandleDaSSD::GetCalibrationSSDLDC() const
857 {
858 // Fill in the array for OCDB 
859   TObjArray *ldcc;
860   TObject   *modcalibobj;
861   if (!fModules) return NULL;
862   ldcc = new TObjArray(fNumberOfModules, 0);
863   for (Int_t i = 0; i < fNumberOfModules; i++) {
864     if (!fModules[i]) {
865       delete ldcc;
866       return NULL;
867     }
868     modcalibobj = dynamic_cast<TObject*>(fModules[i]->GetCalibrationNoise());
869     ldcc->AddAt(modcalibobj, i);
870   }
871   ldcc->Compress();
872   return ldcc;
873 }
874
875
876 //______________________________________________________________________________
877 Bool_t AliITSHandleDaSSD::SaveCalibrationSSDLDC(Char_t*& dafname) const
878 {
879 // Save Calibration data locally
880   TObjArray      *ldcn, *ldcp, *ldcbc;
881   TObject        *modobjn, *modobjp, *modobjbc;
882   Char_t         *tmpfname;
883   TString         dadatafilename("");
884   if (!fModules) return kFALSE;
885   ldcn = new TObjArray(fNumberOfModules, 0);
886   ldcn->SetName("Noise");
887   ldcp = new TObjArray(fNumberOfModules, 0);
888   ldcp->SetName("Pedestal");
889   ldcbc = new TObjArray(fNumberOfModules, 0);
890   ldcbc->SetName("BadChannels");
891   for (Int_t i = 0; i < fNumberOfModules; i++) {
892     if (!fModules[i]) {
893       delete ldcn;
894       return kFALSE;
895     }
896     modobjn = dynamic_cast<TObject*>(fModules[i]->GetCalibrationNoise());
897     modobjp = dynamic_cast<TObject*>(fModules[i]->GetCalibrationPedestal());
898     modobjbc = dynamic_cast<TObject*>(fModules[i]->GetCalibrationBadChannels());
899     ldcn->AddAt(modobjn, i);
900     ldcp->AddAt(modobjp, i);
901     ldcbc->AddAt(modobjbc, i);
902   }
903   ldcn->Compress();
904   ldcp->Compress();
905   ldcbc->Compress();
906   if (dafname) dadatafilename.Form("%s/", dafname);
907   dadatafilename += TString::Format("ITSSSDda_%i.root", fLdcId);
908   tmpfname = new Char_t[dadatafilename.Length()+1];
909   dafname = strcpy(tmpfname, dadatafilename.Data());
910   TFile *fileRun = new TFile (dadatafilename.Data(),"RECREATE");
911   if (fileRun->IsZombie()) {
912     AliError(Form("AliITSHandleDaSSD: SaveCalibrationSSDLDC() error open file %s", dadatafilename.Data()));
913     ldcn->Delete();
914     delete fileRun;
915     delete ldcn;
916     delete ldcp;
917     delete ldcbc;
918     return kFALSE;
919   }
920   fileRun->WriteTObject(ldcn);
921   fileRun->WriteTObject(ldcp);
922   if (fStaticBadChannelsMap) fileRun->WriteTObject(fStaticBadChannelsMap);
923   else fileRun->WriteTObject(ldcbc);
924   fileRun->Close();
925   ldcn->Delete();
926   delete fileRun;
927   delete ldcn;
928   delete ldcp;
929   delete ldcbc;
930   return kTRUE;
931 }
932
933
934
935 //______________________________________________________________________________
936 Bool_t AliITSHandleDaSSD::DumpModInfo(const Float_t meannosethreshold) const
937 {
938 // Dump calibration parameters
939   AliITSModuleDaSSD    *mod;
940   AliITSChannelDaSSD   *strip;
941   if (!fModules) {
942     cout << "SSDDALDC::DumpModInfo():  Error, no modules are allocated!" << endl;
943     return kFALSE;
944   }  
945   cout << "Modules with MeanNoise > " << meannosethreshold << endl;
946   for (Int_t i = 0; i < fNumberOfModules; i++) {
947     if (!(mod = fModules[i])) continue;
948     Float_t  maxnoise = 0.0f, meannoise = 0.0f, maxped = 0.0f;
949     Int_t    maxstrind = 0, novfstr = 0;  
950     for (Int_t strind = 0; strind < mod->GetNumberOfStrips(); strind++) {
951       if (!(strip = mod->GetStrip(strind))) {novfstr++;  continue; }
952       if (strip->GetNoiseCM() >= AliITSChannelDaSSD::GetUndefinedValue() ) {novfstr++;  continue; }
953       if (maxnoise < strip->GetNoiseCM()) { 
954         maxnoise = strip->GetNoiseCM();
955         maxstrind = strind;
956       } 
957       meannoise = (strind - novfstr) ? meannoise + (strip->GetNoiseCM() - meannoise) / static_cast<Float_t>(strind - novfstr + 1) 
958                                     : strip->GetNoiseCM();
959       if (TMath::Abs(maxped) < TMath::Abs(strip->GetPedestal())) maxped = strip->GetPedestal();                     
960     } 
961     if (meannoise > meannosethreshold)
962       cout << "Mod: " << i << ";  DDl: " << (int)mod->GetDdlId() << ";  AD: " << (int)mod->GetAD()  
963                            << ";  ADC: " << (int)mod->GetADC() << "; MaxPed = " << maxped
964                            << ";  MeanNoise = " << meannoise 
965                            << ";  NOfStrips = " << (mod->GetNumberOfStrips() - novfstr) << endl;
966   }
967   return kTRUE;
968 }
969
970
971
972 //______________________________________________________________________________
973 Bool_t AliITSHandleDaSSD::PrintModCalibrationData(const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const Char_t *fname) const
974 {
975 // Print Module calibration data whether in file on in cout
976    AliITSChannelDaSSD  *strip;
977    ofstream             datafile;
978    ostream             *outputfile;
979    if (!fname) { outputfile = &cout; }
980    else {
981      datafile.open(fname, ios::out);
982      if  (datafile.fail()) return kFALSE;
983      outputfile = dynamic_cast<ostream*>(&datafile); 
984    }
985    *outputfile  << "DDL = " << (int)ddlID << ";  AD = " << (int)ad << ";  ADC = " << (int)adc << endl;
986    for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
987      if ( (strip = GetStrip(ddlID, ad, adc, strind)) ) {
988        *outputfile << "Str = " << strind << ";  ped = " << strip->GetPedestal() 
989                    << ";  noise = " << strip->GetNoiseCM() << endl;
990      }
991      else continue;
992    }
993   if (datafile.is_open()) datafile.close(); 
994   return kTRUE;
995 }
996
997
998
999 //______________________________________________________________________________
1000 void AliITSHandleDaSSD::DumpInitData(const Char_t *str) const
1001 {
1002 // Print general information retrieved from raw data file
1003   cout << "Raw data file: " << fRawDataFileName << endl
1004        << "LDC: " << (Int_t)fLdcId << "; RunId: " << fRunId << endl
1005        << "Number of physics events: " << fNumberOfEvents << endl
1006        << str;
1007 }
1008
1009
1010 //______________________________________________________________________________
1011 Bool_t AliITSHandleDaSSD::AllocateSimulatedModules(const Int_t copymodind)
1012 {
1013 // Used to allocate simulated modules to test the performance  
1014   AliITSModuleDaSSD *module;
1015   UChar_t      ad, adc, ddlID;
1016   ad = adc = ddlID = 0;
1017   if (!(fModules[copymodind])) return kFALSE;
1018   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
1019     if (!fModules[modind]) {
1020       module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
1021       if ((adc < 5) || ((adc > 7) && (adc < 13)) ) adc += 1;
1022       else if (adc == 5) adc = 8;
1023       else if (adc == 13) {
1024         adc = 0;
1025         if (ad < 8) ad += 1;
1026         else {
1027           ad = 0;
1028           ddlID +=1;
1029         }
1030       }
1031       if (!module->SetModuleIdData (ddlID, ad, adc, modind)) return kFALSE;
1032       fModules[modind] = module;
1033       for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
1034         AliITSChannelDaSSD *cstrip = fModules[copymodind]->GetStrip(strind);
1035         Long_t      eventsnumber = cstrip->GetEventsNumber();
1036         AliITSChannelDaSSD *strip = new AliITSChannelDaSSD(strind, eventsnumber);
1037         for (Long_t evind = 0; evind < eventsnumber; evind++) {
1038           Short_t sign = *cstrip->GetSignal(evind);
1039           if (!strip->SetSignal(evind, sign)) AliError(Form("AliITSHandleDaSSD: Copy events error! mod = %i, str = %i", modind, strind));
1040         }
1041         module->SetStrip(strip, strind);
1042       }
1043     }
1044     else {
1045       ddlID = fModules[modind]->GetDdlId();
1046       ad    = fModules[modind]->GetAD();
1047       adc   = fModules[modind]->GetADC();
1048     }
1049   }
1050   for (UShort_t modind = 0; modind < fNumberOfModules; modind++) fModules[modind]->SetModuleId(modind + 1080);  
1051   return kTRUE;
1052 }
1053
1054
1055     
1056 //___________________________________________________________________________________________
1057 Bool_t AliITSHandleDaSSD::AdDataPresent(const Int_t ddl, const Int_t ad) const
1058 {
1059 // Check if there are calibration data for given ddl and slot
1060   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
1061     if ((GetModule(modind)->GetAD() == ad) && (GetModule(modind)->GetDdlId() == ddl)) return kTRUE;
1062   return kFALSE;
1063 }
1064
1065    
1066
1067 //___________________________________________________________________________________________
1068 Bool_t AliITSHandleDaSSD::SaveEqSlotCalibrationData(const Int_t ddl, const Int_t ad, const Char_t *fname) const
1069 // Saves calibration files for selected equipment (DDL)
1070 {
1071   fstream    feefile;
1072   Int_t      zsml, offsetml;
1073   ULong_t    zsth, offset, zsoffset;
1074   if (!fname) {
1075     AliError("File name must be specified!");
1076     return kFALSE;   
1077   }
1078   if (!AdDataPresent(ddl, ad)) {
1079     AliError(Form("Error SaveEqSlotCalibrationData(ddl = %i, ad = %i) no data present", ddl, ad)); 
1080     return kFALSE;
1081   }
1082   feefile.open(fname, ios::out);
1083   if (!feefile.is_open()) {
1084         AliError(Form("Can not open the file %s for output!", fname)); 
1085     return kFALSE;
1086   }
1087   for (zsml = 0; fgkZsBitMask >> zsml; zsml++);
1088   for (offsetml = 0; fgkOffSetBitMask >> offsetml; offsetml++);
1089   for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
1090     for (Int_t adcb = 0; adcb < fgkAdcPerDBlock; adcb++) {
1091       zsoffset = 0x0;
1092       for (Int_t j = 0; j < 2; j++) {
1093         Int_t adc = adcb + j * 8;
1094         zsth = ZsThreshold(ddl, ad, adc, strind);
1095         offset = OffsetValue(ddl, ad, adc, strind);
1096         zsoffset = zsoffset | (((zsth << offsetml) | offset) << ((j == 0) ? (offsetml + zsml) : 0));
1097       }
1098       feefile << "0x" << ConvBase(static_cast<unsigned long>(zsoffset), 16) << endl;
1099     }
1100   }
1101   feefile.close();
1102   return kTRUE;
1103 }
1104
1105
1106 //______________________________________________________________________________
1107 Int_t AliITSHandleDaSSD::ChannelIsBad(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1108 {
1109 // Check if the channel is bad
1110   AliITSBadChannelsSSD  *badch = NULL;
1111   TArrayI                bcharray;
1112   Int_t                  strsiden, modn = -1;
1113   if (fStaticBadChannelsMap && fDDLModuleMap) {
1114     modn = RetrieveModuleId(ddl, ad, adc);
1115     if (modn < 0) return -1;
1116     Int_t modind = 0;
1117     while ((modind < fStaticBadChannelsMap->GetEntriesFast() && (!badch))) {
1118       AliITSBadChannelsSSD  *bc = static_cast<AliITSBadChannelsSSD*>(fStaticBadChannelsMap->At(modind++));
1119       if ((bc->GetMod()) == modn) badch = bc; 
1120     }
1121     if (badch) {
1122       if (strn < AliITSModuleDaSSD::GetPNStripsPerModule()) { 
1123         bcharray = badch->GetBadPChannelsList();
1124         strsiden = strn;
1125       } else {
1126         bcharray = badch->GetBadNChannelsList();
1127         strsiden = AliITSChannelDaSSD::GetMaxStripIdConst() - strn;
1128       }
1129       if (bcharray.GetSize() <  AliITSModuleDaSSD::GetPNStripsPerModule()) {
1130             AliWarning(Form("No entry found in bad channels list TArrayI for ddl/ad/adc/str: %i/%i/%i/%i", ddl, ad, adc, strn));
1131             return 0;
1132       } 
1133       return (bcharray[strsiden] & fgkBadChannelMask);
1134     } else {
1135       AliWarning(Form("No entry found in bad channels list for ddl = %i,  ad = %i,  adc = %i", ddl, ad, adc));
1136       return 0;
1137     }  
1138   } else {
1139     AliError("Error ether bad channels list or DDLMap is not initialized or both, return 0!");
1140         return 0;
1141   }
1142 }
1143         
1144         
1145 //______________________________________________________________________________
1146 ULong_t AliITSHandleDaSSD::OffsetValue(const AliITSChannelDaSSD *strip, 
1147                                        const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1148 {
1149 // Calculate the offset value to be upload to FEROM     
1150   Int_t  pedint;
1151   if (fOffsetDefault < INT_MAX) pedint = fOffsetDefault;
1152   else pedint = TMath::Nint(strip->GetPedestal());
1153   if (pedint > static_cast<Int_t>((fgkOffSetBitMask >> 1))) {
1154     if (!ChannelIsBad(ddl, ad, adc, strn)) 
1155       AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i  can not be represented with mask %i, Offset = %i",
1156                    pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(), fgkOffSetBitMask));
1157     return fgkOffSetBitMask;
1158   }  
1159   if ((-pedint) > static_cast<Int_t>(((fgkOffSetBitMask + 1) >> 1))) {
1160     if (!ChannelIsBad(ddl, ad, adc, strn)) 
1161       AliError(Form("Offset %i, channel(ddl/ad/adc/strip) %i/%i/%i/%i  can not be represented with mask %i, Offset = %i", 
1162                    pedint, ddl, ad, adc, strn, ConvBase(fgkOffSetBitMask, 16).c_str(), (fgkOffSetBitMask & (~fgkOffSetBitMask >> 1))));
1163     return fgkOffSetBitMask & (~fgkOffSetBitMask >> 1);
1164   }
1165   return fgkOffSetBitMask & (pedint >= 0 ? pedint : pedint + fgkOffSetBitMask + 1);
1166 }
1167
1168
1169
1170 //______________________________________________________________________________
1171 ULong_t AliITSHandleDaSSD::OffsetValue(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1172 {
1173 // Calculate the offset value to be upload to FEROM     
1174   AliITSChannelDaSSD    *strip = NULL;
1175   AliITSModuleDaSSD     *module = NULL;
1176   if (module = GetModule(ddl, ad, adc)) {
1177     if (strip = module->GetStrip(strn)) return OffsetValue(strip, ddl, ad, adc, strn);
1178     else {
1179       AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i,  strip = %i, 0 is used!", ddl, ad, adc, strn));
1180       return 0ul;
1181     }
1182   } else {
1183     AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i, 0 is used!", ddl, ad, adc));
1184     return 0ul;
1185   }  
1186 }
1187
1188
1189
1190 //______________________________________________________________________________
1191 ULong_t AliITSHandleDaSSD::ZsThreshold(AliITSChannelDaSSD *strip) const
1192
1193 // Calculate the value of zero suppression threshold to be upload to FEROM
1194   ULong_t zs;
1195   if (fZsDefault < 0) zs = TMath::Nint(fZsFactor * strip->GetNoiseCM());
1196   else zs = fZsDefault;
1197   return (zs < fgkZsBitMask) ? (zs & fgkZsBitMask) : fgkZsBitMask;
1198 }
1199
1200
1201 //______________________________________________________________________________
1202 ULong_t AliITSHandleDaSSD::ZsThreshold(const UChar_t ddl, const UChar_t ad, const UChar_t adc, const Int_t strn) const
1203 {
1204 // Calculate the value of zero suppression threshold to be upload to FEROM, account bad channels list
1205   AliITSChannelDaSSD    *strip = NULL;
1206   AliITSModuleDaSSD     *module = NULL;
1207   if (ChannelIsBad(ddl, ad, adc, strn)) return fgkZsBitMask;
1208   if (module = GetModule(ddl, ad, adc)) {
1209     if (strip = module->GetStrip(strn))  return ZsThreshold(strip);
1210     else {
1211       AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i,  strip = %i, 0 is used!", ddl, ad, adc, strn));
1212       return 0ul;
1213     }
1214   } else {
1215     AliWarning(Form("There is no calibration data for ddl = %i,  ad = %i,  adc = %i, 0 is used!", ddl, ad, adc));
1216     return 0ul;
1217   }
1218 }
1219
1220             
1221 //______________________________________________________________________________
1222 string AliITSHandleDaSSD::ConvBase(const unsigned long value, const long base) const
1223 {
1224 // Converts the unsigned long number into that in another base 
1225   string digits = "0123456789ABCDEF";
1226   string result;
1227   unsigned long v = value;
1228   if((base < 2) || (base > 16)) {
1229     result = "Error: base out of range.";
1230   }
1231   else {
1232     int i = 0;
1233     do {
1234       result = digits[v % base] + result;
1235       v /= base;
1236       i++;
1237     }
1238     while((v) || (i<8));
1239   }
1240   return result;
1241 }