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