]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSHandleDaSSD.cxx
Updated comment for compliance with the parsing algorithm
[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: 14/02/2008
24 ///////////////////////////////////////////////////////////////////////////////
25
26 #include <Riostream.h> 
27 #include "AliITSHandleDaSSD.h"
28 #include <math.h>
29 #include <limits.h>
30 #include "event.h"
31 #include "TFile.h"
32 #include "TString.h"
33 #include "TObjArray.h"
34 #include "AliLog.h"
35 #include "AliITSNoiseSSD.h"
36 #include "AliITSPedestalSSD.h"
37 #include "AliITSBadChannelsSSD.h"
38 #include "AliITSRawStreamSSD.h"
39 #include "AliRawReaderDate.h"
40
41 #include "AliITSChannelDaSSD.h"
42
43
44 ClassImp(AliITSHandleDaSSD)
45
46 using namespace std;
47
48
49 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModules = 1698;       // Number of SSD modules in ITS
50 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerDdl = 108;  // Number of SSD modules in DDL
51 const Int_t    AliITSHandleDaSSD::fgkNumberOfSSDModulesPerSlot = 12;  // Number of SSD modules in Slot
52 const Float_t  AliITSHandleDaSSD::fgkPedestalThresholdFactor = 3.0;   // Defalt value for fPedestalThresholdFactor 
53 const Float_t  AliITSHandleDaSSD::fgkCmThresholdFactor = 3.0;         // Defalt value for fCmThresholdFactor
54
55 //______________________________________________________________________________
56 AliITSHandleDaSSD::AliITSHandleDaSSD() :
57   fRawDataFileName(NULL),
58   fNumberOfModules(0),
59   fModules(NULL),
60   fModIndProcessed(0),
61   fModIndRead(0),
62   fModIndex(NULL),
63   fNumberOfEvents(0),
64   fLdcId(0),
65   fRunId(0),
66   fPedestalThresholdFactor(fgkPedestalThresholdFactor),
67   fCmThresholdFactor(fgkCmThresholdFactor) 
68 {
69 // Default constructor
70 }
71
72
73 //______________________________________________________________________________
74 AliITSHandleDaSSD::AliITSHandleDaSSD(Char_t *rdfname) :
75   fRawDataFileName(NULL),
76   fNumberOfModules(0),
77   fModules(NULL),
78   fModIndProcessed(0),
79   fModIndRead(0),
80   fModIndex(NULL),
81   fNumberOfEvents(0),
82   fLdcId(0),
83   fRunId(0),
84   fPedestalThresholdFactor(fgkPedestalThresholdFactor) ,
85   fCmThresholdFactor(fgkCmThresholdFactor) 
86 {
87   if (!Init(rdfname)) AliError("AliITSHandleDaSSD::AliITSHandleDaSSD() initialization error!");
88 }
89
90
91 //______________________________________________________________________________
92 AliITSHandleDaSSD::AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc) :
93   TObject(ssdadldc),
94   fRawDataFileName(ssdadldc.fRawDataFileName),
95   fNumberOfModules(ssdadldc.fNumberOfModules),
96   fModules(NULL),
97   fModIndProcessed(ssdadldc.fModIndProcessed),
98   fModIndRead(ssdadldc.fModIndRead),
99   fModIndex(NULL),
100   fNumberOfEvents(ssdadldc.fNumberOfEvents),
101   fLdcId(ssdadldc.fLdcId),
102   fRunId(ssdadldc.fRunId),
103   fPedestalThresholdFactor(ssdadldc.fPedestalThresholdFactor),
104   fCmThresholdFactor(ssdadldc.fCmThresholdFactor) 
105 {
106   // copy constructor
107   if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
108     fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
109     if (fModules) {
110       for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
111         if (ssdadldc.fModules[modind]) {
112           fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
113           if (!fModules[modind]) { 
114             AliError("AliITSHandleDaSSD: Error copy constructor");
115             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
116             delete [] fModules;
117             fModules = NULL;
118             break;
119           }
120         } else fModules[modind] = NULL; 
121       }   
122     } else {
123       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
124       fNumberOfModules = 0;
125       fModules = NULL;
126     }
127   }
128 }
129
130
131 //______________________________________________________________________________
132 AliITSHandleDaSSD& AliITSHandleDaSSD::operator = (const AliITSHandleDaSSD& ssdadldc)
133 {
134 // assignment operator
135   if (this == &ssdadldc)  return *this;
136   if (fModules) {
137     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
138     delete [] fModules; 
139     fModules = NULL;
140   }
141   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
142   if ((ssdadldc.fNumberOfModules > 0) && (ssdadldc.fModules)) {
143     fModules = new (nothrow) AliITSModuleDaSSD* [ssdadldc.fNumberOfModules];
144     if (fModules) {
145       for (Int_t modind = 0; modind < ssdadldc.fNumberOfModules; modind++) {
146         if (ssdadldc.fModules[modind]) {
147           fModules[modind] = new AliITSModuleDaSSD(*(ssdadldc.fModules[modind]));
148           if (!fModules[modind]) { 
149             AliError("AliITSHandleDaSSD: Error assignment operator");
150             for (Int_t i = (modind - 1); i >= 0; i--) delete fModules[modind];
151             delete [] fModules;
152             fModules = NULL;
153             break;
154           }
155         } else fModules[modind] = NULL; 
156       }   
157     } else {
158       AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", ssdadldc.fNumberOfModules));
159       fNumberOfModules = 0;
160       fModules = NULL;
161     }
162   }
163   return *this;
164 }
165
166
167 //______________________________________________________________________________
168 AliITSHandleDaSSD::~AliITSHandleDaSSD()
169 {
170 // Default destructor 
171   if (fModules) 
172   {
173     for (Int_t i = 0; i < fNumberOfModules; i++)
174     { 
175       if (fModules[i]) delete fModules[i];
176     }
177     delete [] fModules;
178   }
179   if (fModIndex) delete [] fModIndex;
180 }
181
182
183
184 //______________________________________________________________________________
185 void AliITSHandleDaSSD::Reset()
186 {
187 // Delete array of AliITSModuleDaSSD* objects. 
188   if (fModules) {
189     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
190     delete [] fModules;
191     fModules = NULL;
192   }
193   if (fModIndex) { delete [] fModIndex; fModIndex = NULL; }
194   fRawDataFileName = NULL;
195   fModIndProcessed = fModIndRead = 0;
196   fNumberOfEvents = 0;
197   fLdcId = fRunId = 0;
198   fPedestalThresholdFactor = fgkPedestalThresholdFactor;
199   fCmThresholdFactor = fgkCmThresholdFactor;
200 }
201
202
203
204 //______________________________________________________________________________
205 Bool_t AliITSHandleDaSSD::Init(Char_t *rdfname, const Char_t *configfname)
206 {
207 // Read raw data file and set initial and configuration parameters
208   Long_t physeventind = 0, strn = 0, nofstripsev, nofstrips = 0;
209   Int_t  nofeqipmentev, nofeqipment = 0, eqn = 0; 
210   AliRawReaderDate  *rawreaderdate = NULL;
211   UChar_t *data = NULL;
212   Long_t datasize = 0, eqbelsize = 1;
213
214   if (configfname) ReadConfigurationFile(configfname);
215   rawreaderdate = new AliRawReaderDate(rdfname, 0);
216   if (!(rawreaderdate->GetAttributes() || rawreaderdate->GetEventId())) {
217     AliError(Form("AliITSHandleDaSSD: Error reading raw data file %s by RawReaderDate", rdfname));
218     MakeZombie();
219     return kFALSE;
220   }  
221   if (rawreaderdate->NextEvent()) {
222     fRunId = rawreaderdate->GetRunNumber(); 
223     rawreaderdate->RewindEvents();
224   } else { MakeZombie(); return kFALSE; }
225   if (fModules) Reset();
226   rawreaderdate->SelectEvents(-1);
227   nofstrips = 0;
228   while (rawreaderdate->NextEvent()) {
229     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
230     nofstripsev = 0;
231     nofeqipmentev = 0;
232     while (rawreaderdate->ReadNextData(data)) {
233       fLdcId = rawreaderdate->GetLDCId();
234       nofeqipmentev += 1;
235       datasize = rawreaderdate->GetDataSize();
236       eqbelsize = rawreaderdate->GetEquipmentElementSize();
237       if ( (datasize % eqbelsize) || (eqbelsize != sizeof(long32)) ) {
238         AliError(Form("AliITSHandleDaSSD: Error Init(%s): event data size %i is not an integer of equipment data size %i", 
239                                     rdfname, datasize, eqbelsize));
240         MakeZombie();
241         return kFALSE;
242       }
243       nofstripsev += (Int_t) (datasize / eqbelsize);             
244     }
245     if (physeventind++) {
246       if (nofstrips != nofstripsev) AliWarning("AliITSHandleDaSSD: number of strips varies from event to evnt");
247       if (nofeqipment != nofeqipmentev) AliWarning("AliITSHandleDaSSD: number of DDLs varies from event to evnt");
248     }
249     nofstrips = nofstripsev;
250     nofeqipment = nofeqipmentev;
251     if (strn < nofstrips) strn = nofstrips;
252     if (eqn < nofeqipment) eqn = nofeqipment;
253   }
254   delete rawreaderdate;
255   if ((physeventind > 0) && (strn > 0))
256   {
257     fNumberOfEvents = physeventind;
258     fRawDataFileName = rdfname;
259     fModIndex = new (nothrow) Int_t [fgkNumberOfSSDModulesPerDdl * eqn];
260     if (fModIndex) 
261       for (Int_t i = 0; i < fgkNumberOfSSDModulesPerDdl * eqn; i++) fModIndex[i] = -1; 
262     else AliWarning(Form("AliITSHandleDaSSD: Error Init(%s): Index array for %i modules was not created", 
263                                      rdfname, fgkNumberOfSSDModulesPerDdl * eqn));
264     if (SetNumberOfModules(fgkNumberOfSSDModules)) {
265       TString str = TString::Format("Max number of equipment: %i, max number of channels: %i\n", eqn, strn);
266       DumpInitData(str.Data());
267       return kTRUE;
268     }  
269   }
270   MakeZombie();
271   return kFALSE;
272 }
273
274
275 //______________________________________________________________________________
276 Bool_t AliITSHandleDaSSD::ReadConfigurationFile(const Char_t * /* configfname */) 
277 const {
278 // Dowload configuration parameters from configuration file or database
279   return kFALSE;
280 }
281
282
283
284 //______________________________________________________________________________
285 AliITSModuleDaSSD* AliITSHandleDaSSD::GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
286 {
287 // Retrieve AliITSModuleDaSSD object from the array
288    if (!fModules) return NULL;
289    for (Int_t i = 0; i < fNumberOfModules; i++) {
290      if (fModules[i]) {
291        if (    (fModules[i]->GetDdlId() == ddlID)
292             && (fModules[i]->GetAD() == ad)
293             && (fModules[i]->GetADC() == adc))
294             return fModules[i];
295      }      
296    }
297    return NULL;
298 }
299
300
301 Int_t AliITSHandleDaSSD::GetModuleIndex (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
302 {
303 // Retrieve the position of AliITSModuleDaSSD object in the array
304    if (!fModules) return -1;
305    for (Int_t i = 0; i < fNumberOfModules; i++) {
306      if (fModules[i]) {
307        if (    (fModules[i]->GetDdlId() == ddlID)
308             && (fModules[i]->GetAD() == ad)
309             && (fModules[i]->GetADC() == adc))
310             return i;
311      }      
312    }
313    return -1;
314 }
315
316
317
318 //______________________________________________________________________________
319 AliITSChannelDaSSD* AliITSHandleDaSSD::GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const
320 {
321 // Retrieve AliITSChannalDaSSD object from the array
322   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
323     if (    (fModules[modind]->GetDdlId() == ddlID)
324          && (fModules[modind]->GetAD() == ad)
325          && (fModules[modind]->GetADC() == adc)  ) 
326         {
327        return fModules[modind]->GetStrip(stripID);
328     }
329   }
330   AliError(Form("AliITSHandleDaSSD: Error GetStrip (%i, %i, %i, %i), strip not found, returns NULL!",
331                 ddlID, ad, adc, stripID));
332   return NULL;
333 }
334
335
336
337 //______________________________________________________________________________
338 Bool_t AliITSHandleDaSSD::SetModule(AliITSModuleDaSSD *const module, const Int_t index)
339
340 // Assign array element with AliITSModuleDaSSD object
341    if ((index < fNumberOfModules) && (index >= 0)) 
342      { 
343         if (fModules[index]) delete fModules[index];
344         fModules[index] = module;
345         return kTRUE;
346       }
347    else return kFALSE;                         
348 }
349
350
351
352 //______________________________________________________________________________
353 Bool_t AliITSHandleDaSSD::SetNumberOfModules (const Int_t numberofmodules)
354 {
355 // Allocates memory for AliITSModuleDaSSD objects
356   if (numberofmodules > fgkNumberOfSSDModules)
357     AliWarning(Form("AliITSHandleDaSSD: the number of modules %i you use exceeds the maximum %i for ALICE ITS SSD", 
358                      numberofmodules, fgkNumberOfSSDModules));
359   if (fModules) {
360     for (Int_t i = 0; i < fNumberOfModules; i++) if (fModules[i]) delete fModules[i];
361     delete [] fModules; 
362     fModules = NULL;
363   }
364   fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
365   if (fModules) {
366     fNumberOfModules = numberofmodules;
367     memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
368     return kTRUE;
369   } else {
370      AliError(Form("AliITSHandleDaSSD: Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules));
371      fNumberOfModules = 0;
372      fModules = NULL;
373   }
374   return kFALSE;
375 }
376
377
378
379 //______________________________________________________________________________
380 Bool_t AliITSHandleDaSSD::ReadCalibrationDataFile (char* fileName, const Long_t eventsnumber)
381 {
382 // Reads raw data from file
383   AliRawReaderDate    *rawreaderdate = NULL;
384   AliITSModuleDaSSD   *module;
385   AliITSChannelDaSSD  *strip;
386   Long_t            datasize, eventind = 0;
387   Int_t             nofstrips, eqbelsize;
388   UShort_t          modind;
389   long32           *data;
390   UChar_t          *databyte;
391   if (!Init(fileName)){
392     AliError("AliITSHandleDaSSD: Error ReadCalibrationDataFile");
393     return kFALSE;
394   }
395   rawreaderdate = new AliRawReaderDate(fileName, 0);
396   rawreaderdate->SelectEvents(-1);
397   while (rawreaderdate->NextEvent()) {
398     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
399     fRunId = rawreaderdate->GetRunNumber(); 
400     modind = 0;
401     while (rawreaderdate->ReadNextData(databyte)) {
402       fLdcId = rawreaderdate->GetLDCId();
403       data = reinterpret_cast<long32*>(databyte);
404       Int_t     equipid    = rawreaderdate->GetEquipmentId();              //  EquipmentID required to access to rorc
405       Int_t     equiptype  = rawreaderdate->GetEquipmentType();            //
406       UChar_t   ddlID      = (UChar_t)rawreaderdate->GetDDLID();           // GetDDLID(); index of DDL, ITS SSD: 33-48
407       datasize = rawreaderdate->GetDataSize();
408       eqbelsize = rawreaderdate->GetEquipmentElementSize();
409       if ( (datasize % eqbelsize) || (eqbelsize != sizeof(long32)) ) {
410         AliError(Form("AliITSHandleDaSSD: Error ReadCalibrationDataFile: event data size %i is not an integer of equipment data size %i",
411                       datasize, eqbelsize));
412         return kFALSE;
413       }
414       nofstrips = (Int_t) (datasize / eqbelsize);                
415       for (Int_t strind = 0; strind < nofstrips; strind++) {
416         UChar_t   ad      = (UChar_t) (data[strind] >> 28) & 0x0000000F;  // index of AD module     0-9
417         UChar_t   adc     = (UChar_t) (data[strind] >> 24) & 0x0000000F;  // index of ADC module    0-5, 8-13
418         UShort_t  stripID = (UShort_t)(data[strind] >> 12) & 0x000007FF;  // strip number           0-1535
419         Short_t   signal  = (Short_t)(data[strind] & 0x00000FFF);
420                   signal  = (signal > AliITSChannelDaSSD::GetUnderflowConst()) ? (signal - 2 * AliITSChannelDaSSD::GetUnderflowConst()) 
421                                                                                : signal;
422         if (!(module = GetModule(ddlID, ad, adc))) {
423           module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
424           if (!module->SetModuleIdData (ddlID, ad, adc, RetrieveModuleId(ddlID, ad, adc))) return kFALSE;
425           module->SetModuleRorcId (equipid, equiptype);
426           fModules[modind++] = module;
427         }
428         if (!(strip = module->GetStrip(stripID))) {
429           strip = new AliITSChannelDaSSD(stripID, eventsnumber);
430           module->SetStrip(strip, stripID);
431         }
432         strip->SetSignal(eventind, signal);
433      }
434      if (modind) cout << "The memory was allocated for " <<  modind << " modules." << endl;
435    }
436    if (++eventind > eventsnumber) break;
437   }
438   delete rawreaderdate;
439   return RelocateModules();
440 }
441
442
443
444 //______________________________________________________________________________
445 Int_t AliITSHandleDaSSD::ReadModuleRawData (const Int_t modulesnumber)
446 {
447 // Reads raw data from file
448   AliRawReaderDate    *rawreaderdate = NULL;
449   AliITSModuleDaSSD   *module;
450   AliITSChannelDaSSD  *strip;
451   Long_t            datasize, eventind = 0;
452   Int_t             nofstrips, eqbelsize, nofeqipmentev;
453   UShort_t          modind;
454   long32           *data;
455   UChar_t          *databyte;
456   if (!(rawreaderdate = new AliRawReaderDate(fRawDataFileName, 0))) return 0;
457   if (!fModules) {
458     AliError("AliITSHandleDaSSD: Error ReadModuleRawData: no structure was allocated for data");
459     return 0;
460   }
461   rawreaderdate->SelectEvents(-1);
462   modind = 0;
463   while (rawreaderdate->NextEvent()) {
464     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
465     nofeqipmentev = 0;
466     while (rawreaderdate->ReadNextData(databyte)) {
467       data = reinterpret_cast<long32*>(databyte);
468       Int_t     equipid    = rawreaderdate->GetEquipmentId();              //  EquipmentID required to access to rorc
469       Int_t     equiptype  = rawreaderdate->GetEquipmentType();            //
470       UChar_t   ddlID      = (UChar_t)rawreaderdate->GetDDLID();           // GetDDLID(); index of DDL, ITS SSD: 33-48
471       datasize = rawreaderdate->GetDataSize();
472       eqbelsize = rawreaderdate->GetEquipmentElementSize();
473       if ( (datasize % eqbelsize) || (eqbelsize != sizeof(long32)) ) {
474         AliError(Form("AliITSHandleDaSSD: Error ReadCalibrationDataFile: event data size %i is not an integer of equipment data size %i",
475                        datasize, eqbelsize));
476         return kFALSE;
477       }
478       nofstrips = (Int_t) (datasize / eqbelsize);                
479       for (Int_t strind = 0; strind < nofstrips; strind++) {
480         UChar_t   ad      = (UChar_t) (data[strind] >> 28) & 0x0000000F;  // index of AD module     0-9
481         UChar_t   adc     = (UChar_t) (data[strind] >> 24) & 0x0000000F;  // index of ADC module    0-5, 8-13
482         UShort_t  stripID = (UShort_t)(data[strind] >> 12) & 0x000007FF;  // strip number           0-1535
483         Short_t   signal  = (Short_t)(data[strind] & 0x00000FFF);
484                   signal  = (signal > AliITSChannelDaSSD::GetUnderflowConst()) ? (signal - 2 * AliITSChannelDaSSD::GetUnderflowConst()) 
485                                                                                : signal;
486
487         Int_t indpos = (nofeqipmentev * fgkNumberOfSSDModulesPerDdl)
488                         + ((ad - 1) * fgkNumberOfSSDModulesPerSlot) + (adc < 6 ? adc : (adc - 2));
489         Int_t modpos = fModIndex[indpos];
490         if (((modpos > 0) && (modpos < fModIndRead)) || ((modpos < 0) && (modind == modulesnumber))) continue;
491         if ((modpos < 0) && (modind < modulesnumber)) {
492           module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
493           if (!module->SetModuleIdData (ddlID, ad, adc, RetrieveModuleId(ddlID, ad, adc))) return 0;
494           module->SetModuleRorcId (equipid, equiptype);
495           modpos = fModIndRead + modind;
496           modind += 1;
497           fModules[modpos] = module;
498           fModIndex[indpos] = modpos;
499         } 
500         if (!(strip = fModules[modpos]->GetStrip(stripID))) {
501           strip = new AliITSChannelDaSSD(stripID, fNumberOfEvents);
502           fModules[modpos]->SetStrip(strip, stripID);
503         }
504         strip->SetSignal(eventind, signal);
505       } 
506       nofeqipmentev += 1;
507     }
508     if (++eventind > fNumberOfEvents) break;
509   }
510   delete rawreaderdate;
511   if (modind) cout << "The memory was allocated for " <<  modind << " modules." << endl;
512   fModIndRead += modind;
513   if (modind < modulesnumber) RelocateModules();
514   return modind;
515 }
516
517
518
519 //______________________________________________________________________________
520 Bool_t AliITSHandleDaSSD::RelocateModules()
521 {
522 // Relocate memory for AliITSModuleDaSSD object array
523   Int_t         nm = 0;
524   AliITSModuleDaSSD **marray = NULL;
525   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
526     if (fModules[modind]) nm += 1;
527   if (nm == fNumberOfModules) return kTRUE;
528   marray = new (nothrow) AliITSModuleDaSSD* [nm];
529   if (!marray) {
530     AliError(Form("AliITSHandleDaSSD: Error relocating memory for %i AliITSModuleDaSSD* objects!", nm));
531     return kFALSE;
532   }
533   nm = 0;
534   for (Int_t modind = 0; modind < fNumberOfModules; modind++)  
535     if (fModules[modind]) marray[nm++] = fModules[modind];
536   delete [] fModules;
537   fModules = marray;
538   fNumberOfModules = fModIndRead = nm;
539   return kTRUE;
540 }
541
542
543
544 //______________________________________________________________________________
545 Bool_t AliITSHandleDaSSD::CalculatePedestal(AliITSModuleDaSSD *const module)
546 {
547 // Calculates Pedestal
548   AliITSChannelDaSSD *strip;
549   Float_t             pedestal, noise;
550   Short_t            *signal;
551   Long_t              ovev, ev, n;
552   if (!module) return kFALSE;
553   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
554     if (!(strip = module->GetStrip(strind))) continue;
555     if (!(signal = strip->GetSignal())) {
556       AliError(Form("AliITSHandleDaSSD: Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()",
557                      module->GetModuleId(), strind));
558       continue; 
559     }
560 //************* pedestal first pass ****************
561     pedestal = 0.0f;
562     ovev = 0l;
563     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
564       if (SignalOutOfRange(signal[ev])) ovev += 1;
565       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Float_t>(ev - ovev + 1) : signal[ev];
566     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
567     strip->SetPedestal(pedestal);       
568 //************* noise *******************************
569     Double_t nsum = 0.0L;
570     ovev = 0l;
571     for (ev = 0; ev < strip->GetEventsNumber(); ev++) {
572       if (SignalOutOfRange(signal[ev])) ovev += 1;
573       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
574     } 
575     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
576     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
577     strip->SetNoise(noise);
578 //************* pedestal second pass ****************
579     pedestal = 0.0f;
580     ovev = 0l;
581     for (ev = 0; ev < strip->GetEventsNumber(); ev++)
582       if (   SignalOutOfRange(signal[ev]) 
583           || TMath::Abs(signal[ev] - strip->GetPedestal()) > (fPedestalThresholdFactor * strip->GetNoise())) ovev += 1;
584       else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Float_t>(ev - ovev + 1) : signal[ev];
585     if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
586     strip->SetPedestal(pedestal);       
587     strip->SetOverflowNumber(ovev);     
588   }
589   return kTRUE;
590 }
591
592
593 //______________________________________________________________________________
594 Bool_t AliITSHandleDaSSD::CalculateNoise(AliITSModuleDaSSD *const module)
595 {
596 // Calculates Noise
597   AliITSChannelDaSSD *strip;
598   Short_t    *signal;
599   Float_t     noise;
600   Long_t      ovev, n;
601   if (!module) return kFALSE;
602   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
603     if (!(strip = module->GetStrip(strind))) continue;
604     if (!(signal = strip->GetSignal())) {
605       strip->SetNoise(AliITSChannelDaSSD::GetUndefinedValue());
606       AliError(Form("AliITSHandleDaSSD: Error CalculateNoise(): there are no events data for module[%i] strip[%i]->GetSignal()",
607                      module->GetModuleId(), strind));
608       continue;
609     }
610     Double_t nsum = 0.0L;
611     ovev = 0l;
612     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
613       if (SignalOutOfRange(signal[ev])) ovev += 1;
614       else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
615     } 
616     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
617     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
618     strip->SetNoise(noise);
619   }
620   return kTRUE;
621 }
622
623
624
625 //______________________________________________________________________________
626 Bool_t AliITSHandleDaSSD::CalculateNoiseCM(AliITSModuleDaSSD *const module)
627 {
628 // Calculates Noise with CM correction
629   AliITSChannelDaSSD  *strip = NULL;
630   Short_t     *signal;
631   Float_t      noise;
632   Long_t       ovev, n;
633   if (!CalculateCM(module)) { 
634     AliError("Error: AliITSHandleDaSSD::CalculateCM() returned kFALSE");
635     return kFALSE;
636   }  
637   for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
638     if (!(strip = module->GetStrip(strind))) continue; //return kFALSE;
639     if (!(signal = strip->GetSignal())) {
640       strip->SetNoiseCM(AliITSChannelDaSSD::GetUndefinedValue());
641       AliError(Form("SSDDAModule: Error CalculateNoiseWithoutCM(): there are no events data for module[%i] strip[%i]->GetSignal()", 
642                      module->GetModuleId(), strind));
643       continue; //return kFALSE;
644     }
645     Int_t chipind = strind / AliITSModuleDaSSD::GetStripsPerChip();
646     Double_t nsum = 0.0L;
647     ovev = 0l;
648     for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
649       if (SignalOutOfRange(signal[ev])) ovev += 1;
650       else nsum += pow((signal[ev] - strip->GetPedestal() - module->GetCM(chipind, ev)), 2);
651     } 
652     if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
653     else  noise = AliITSChannelDaSSD::GetUndefinedValue();
654     strip->SetNoiseCM(noise);
655   }
656   return kTRUE;
657 }
658
659
660
661 //______________________________________________________________________________
662 Bool_t AliITSHandleDaSSD::CalculateCM(AliITSModuleDaSSD *const module)
663 {
664 // Calculates CM
665   AliITSChannelDaSSD  *strip = NULL;
666   Short_t             *signal;
667   Long_t               ovstr, n;
668   Int_t                stripind;
669   module->SetNumberOfChips(AliITSModuleDaSSD::GetChipsPerModuleConst());
670   for (Int_t chipind = 0; chipind < module->GetNumberOfChips(); chipind++) {
671     stripind = chipind * module->GetStripsPerChip();
672     module->GetCm()[chipind].Set(fNumberOfEvents);
673     module->GetCm()[chipind].Reset(0.0f);
674     for (Long_t ev = 0; ev < fNumberOfEvents; ev++) {
675     // calculate firs approximation of CM.
676       Double_t cm0 = 0.0L;
677       ovstr = 0l;
678       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
679         if (!(strip = module->GetStrip(strind))) continue; //return kFALSE; 
680         if (!(signal = strip->GetSignal())) {
681           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()", 
682                         module->GetModuleId(), strind));
683           return kFALSE;
684         }
685         if ((signal[ev] >= AliITSChannelDaSSD::GetOverflowConst()) || 
686             (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
687         else cm0 += (signal[ev] - strip->GetPedestal());
688       }
689       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cm0 /= (Float_t)(n);
690       else { module->SetCM(0.0f, chipind, ev); continue; }
691     // calculate avarage (cm - (signal - pedestal)) over the chip
692       Double_t cmsigma = 0.0L;
693       ovstr = 0l;
694       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
695         if (!(strip = module->GetStrip(strind))) continue;
696         if (!(signal = strip->GetSignal())) {
697           AliError(Form("AliITSHandleDaSSD: Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()\n",
698                          module->GetModuleId(), strind));
699           return kFALSE;
700         }
701         if ((signal[ev] >= AliITSChannelDaSSD::GetOverflowConst()) || 
702             (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue())) ovstr += 1;
703         else cmsigma += pow((cm0 - (signal[ev] - strip->GetPedestal())), 2);
704       }
705       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsigma = sqrt(cmsigma / (Float_t)(n));
706       else { module->SetCM(0.0f, chipind, ev); continue; }
707    // calculate cm with threshold
708       Double_t cmsum = 0.0L;
709       ovstr = 0l;
710       for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
711         if (!(strip = module->GetStrip(strind))) continue;
712         signal = strip->GetSignal();
713         if (   (signal[ev] >= AliITSChannelDaSSD::GetOverflowConst())
714             || (strip->GetPedestal() == AliITSChannelDaSSD::GetUndefinedValue()) 
715             || (TMath::Abs(cm0 - (signal[ev] - strip->GetPedestal())) > (fCmThresholdFactor * cmsigma)) ) ovstr += 1;
716         else cmsum += (signal[ev] - strip->GetPedestal());
717       }
718       if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cmsum /= (Float_t)(n);
719       else cmsum = 0.0L;
720       if (!module->SetCM(cmsum, chipind, ev));
721     } 
722   }
723   return kTRUE; 
724 }
725
726
727 //______________________________________________________________________________
728 Bool_t AliITSHandleDaSSD::ProcessRawData(const Int_t nmread)
729 {
730 // Performs calculation of calibration parameters (pedestal, noise, ...) 
731   Int_t nm = 0;
732   if (nmread <= 0) return kFALSE;
733   if (!fModules) return kFALSE;
734   while ((nm = ReadModuleRawData(nmread)) > 0) {
735     cout << "Processing next " << nm << " modules;" << endl;  
736     for (Int_t modind = fModIndProcessed; modind < fModIndRead; modind++) {
737       if (!fModules[modind]) {
738         AliError(Form("AliITSHandleDaSSD: Error ProcessRawData(): No AliITSModuleDaSSD object with index %i is allocated in AliITSHandleDaSSD\n",
739                        modind));
740         return kFALSE;
741       }
742       CalculatePedestal(fModules[modind]);
743       CalculateNoise(fModules[modind]);
744       CalculateNoiseCM(fModules[modind]);
745     }
746     DeleteSignal();
747 //    DeleteCM();
748     fModIndProcessed = fModIndRead;
749     cout << fModIndProcessed << " - done" << endl;
750   }
751   return kTRUE;  
752 }
753
754
755 //______________________________________________________________________________
756 Short_t AliITSHandleDaSSD::RetrieveModuleId(const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
757 {
758 // Retrieve ModuleId from the DDL map which is defined in AliITSRawStreamSSD class
759   Int_t mddli = (ad - 1) * 12 + ((adc<6) ? adc : (adc-2));
760   if ((ddlID < AliITSRawStreamSSD::kDDLsNumber) && (mddli < AliITSRawStreamSSD::kModulesPerDDL)) {
761     mddli = AliITSRawStreamSSD::GetModuleNumber(ddlID, mddli);
762     if (mddli > SHRT_MAX) return SHRT_MAX;
763     else return (Short_t)mddli;
764   }
765   else {
766     AliWarning(Form("Module index  = %d or ddlID = %d is out of range -1 is rturned", ddlID, mddli));
767     return -1;
768   }
769 }
770
771
772
773 //______________________________________________________________________________
774 TObjArray* AliITSHandleDaSSD::GetCalibrationSSDLDC() const
775 {
776 // Fill in the array for OCDB 
777   TObjArray *ldcc;
778   TObject   *modcalibobj;
779   if (!fModules) return NULL;
780   ldcc = new TObjArray(fNumberOfModules, 0);
781   for (Int_t i = 0; i < fNumberOfModules; i++) {
782     if (!fModules[i]) {
783       delete ldcc;
784       return NULL;
785     }
786     modcalibobj = dynamic_cast<TObject*>(fModules[i]->GetCalibrationNoise());
787     ldcc->AddAt(modcalibobj, i);
788   }
789   ldcc->Compress();
790   return ldcc;
791 }
792
793
794 //______________________________________________________________________________
795 Bool_t AliITSHandleDaSSD::SaveCalibrationSSDLDC(Char_t*& dafname) const
796 {
797 // Save Calibration data locally
798   TObjArray      *ldcn, *ldcp, *ldcbc;
799   TObject        *modobjn, *modobjp, *modobjbc;
800   Char_t         *tmpfname;
801   TString         dadatafilename("");
802   if (!fModules) return kFALSE;
803   ldcn = new TObjArray(fNumberOfModules, 0);
804   ldcn->SetName("Noise");
805   ldcp = new TObjArray(fNumberOfModules, 0);
806   ldcp->SetName("Pedestal");
807   ldcbc = new TObjArray(fNumberOfModules, 0);
808   ldcbc->SetName("BadChannels");
809   for (Int_t i = 0; i < fNumberOfModules; i++) {
810     if (!fModules[i]) {
811       delete ldcn;
812       return kFALSE;
813     }
814     modobjn = dynamic_cast<TObject*>(fModules[i]->GetCalibrationNoise());
815     modobjp = dynamic_cast<TObject*>(fModules[i]->GetCalibrationPedestal());
816     modobjbc = dynamic_cast<TObject*>(fModules[i]->GetCalibrationBadChannels());
817     ldcn->AddAt(modobjn, i);
818     ldcp->AddAt(modobjp, i);
819     ldcbc->AddAt(modobjbc, i);
820   }
821   ldcn->Compress();
822   ldcp->Compress();
823   ldcbc->Compress();
824   if (dafname) dadatafilename.Form("%s/", dafname);
825   dadatafilename += TString::Format("ITSSSDda_%i_%i.root", fLdcId, fRunId);
826   tmpfname = new Char_t[dadatafilename.Length()+1];
827   dafname = strcpy(tmpfname, dadatafilename.Data());
828   TFile *fileRun = new TFile (dadatafilename.Data(),"RECREATE");
829   if (fileRun->IsZombie()) {
830     AliError(Form("AliITSHandleDaSSD: SaveCalibrationSSDLDC() error open file %s", dadatafilename.Data()));
831     ldcn->Delete();
832     delete fileRun;
833     delete ldcn;
834     delete ldcp;
835     delete ldcbc;
836     return kFALSE;
837   }
838   fileRun->WriteTObject(ldcn);
839   fileRun->WriteTObject(ldcp);
840   fileRun->WriteTObject(ldcbc);
841   fileRun->Close();
842   ldcn->Delete();
843   delete fileRun;
844   delete ldcn;
845   delete ldcp;
846   delete ldcbc;
847   return kTRUE;
848 }
849
850
851
852 //______________________________________________________________________________
853 Bool_t AliITSHandleDaSSD::DumpModInfo(const Float_t meannosethreshold) const
854 {
855 // Dump calibration parameters
856   AliITSModuleDaSSD    *mod;
857   AliITSChannelDaSSD   *strip;
858   if (!fModules) {
859     cout << "SSDDALDC::DumpModInfo():  Error, no modules are allocated!" << endl;
860     return kFALSE;
861   }  
862   cout << "Modules with MeanNoise > " << meannosethreshold << endl;
863   for (Int_t i = 0; i < fNumberOfModules; i++) {
864     if (!(mod = fModules[i])) continue;
865     Float_t  maxnoise = 0.0f, meannoise = 0.0f;
866     Int_t    maxstrind = 0, novfstr = 0;  
867     for (Int_t strind = 0; strind < mod->GetNumberOfStrips(); strind++) {
868       if (!(strip = mod->GetStrip(strind))) {novfstr++;  continue; }
869       if (strip->GetNoiseCM() >= AliITSChannelDaSSD::GetUndefinedValue() ) {novfstr++;  continue; }
870       if (maxnoise < strip->GetNoiseCM()) { 
871         maxnoise = strip->GetNoiseCM();
872         maxstrind = strind;
873       } 
874       meannoise = (strind - novfstr) ? meannoise + (strip->GetNoiseCM() - meannoise) / static_cast<Float_t>(strind - novfstr + 1) 
875                                     : strip->GetNoiseCM();
876     } 
877     if (meannoise > meannosethreshold)
878       cout << "Mod: " << i << ";  DDl: " << (int)mod->GetDdlId() << ";  AD: " << (int)mod->GetAD()  
879                            << ";  ADC: " << (int)mod->GetADC() << ";  MeanNoise = " << meannoise 
880                            << ";  NOfStrips = " << (mod->GetNumberOfStrips() - novfstr) << endl;
881   }
882   return kTRUE;
883 }
884
885
886
887 //______________________________________________________________________________
888 Bool_t AliITSHandleDaSSD::PrintModCalibrationData(const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const Char_t *fname) const
889 {
890 // Print Module calibration data whether in file on in cout
891    AliITSChannelDaSSD  *strip;
892    ofstream             datafile;
893    ostream             *outputfile;
894    if (!fname) { outputfile = &cout; }
895    else {
896      datafile.open(fname, ios::out);
897      if  (datafile.fail()) return kFALSE;
898      outputfile = dynamic_cast<ostream*>(&datafile); 
899    }
900    *outputfile  << "DDL = " << (int)ddlID << ";  AD = " << (int)ad << ";  ADC = " << (int)adc << endl;
901    for (Int_t strind = 0; strind < AliITSModuleDaSSD::GetStripsPerModuleConst(); strind++) {
902      if ( (strip = GetStrip(ddlID, ad, adc, strind)) ) {
903        *outputfile << "Str = " << strind << ";  ped = " << strip->GetPedestal() 
904                    << ";  noise = " << strip->GetNoiseCM() << endl;
905      }
906      else continue;
907    }
908   if (datafile.is_open()) datafile.close(); 
909   return kTRUE;
910 }
911
912
913
914 void AliITSHandleDaSSD::DumpInitData(const Char_t *str) const
915 {
916 // Print general information retrieve from raw data file
917   cout << "Raw data file: " << fRawDataFileName << endl
918        << "LDC: " << (Int_t)fLdcId << "; RunId: " << fRunId << endl
919        << "Number of physics events: " << fNumberOfEvents << endl
920        << str;
921 }
922
923
924 //______________________________________________________________________________
925 Bool_t AliITSHandleDaSSD::AllocateSimulatedModules(const Int_t copymodind)
926 {
927 // Used to allocate simulated modules to test the performance  
928   AliITSModuleDaSSD *module;
929   UChar_t      ad, adc, ddlID;
930   ad = adc = ddlID = 0;
931   if (!(fModules[copymodind])) return kFALSE;
932   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
933     if (!fModules[modind]) {
934       module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
935       if ((adc < 5) || ((adc > 7) && (adc < 13)) ) adc += 1;
936       else if (adc == 5) adc = 8;
937       else if (adc == 13) {
938         adc = 0;
939         if (ad < 8) ad += 1;
940         else {
941           ad = 0;
942           ddlID +=1;
943         }
944       }
945       if (!module->SetModuleIdData (ddlID, ad, adc, modind)) return kFALSE;
946       fModules[modind] = module;
947       for (Int_t strind = 0; strind < module->GetNumberOfStrips(); strind++) {
948         AliITSChannelDaSSD *cstrip = fModules[copymodind]->GetStrip(strind);
949         Long_t      eventsnumber = cstrip->GetEventsNumber();
950         AliITSChannelDaSSD *strip = new AliITSChannelDaSSD(strind, eventsnumber);
951         for (Long_t evind = 0; evind < eventsnumber; evind++) {
952           Short_t sign = *cstrip->GetSignal(evind);
953           if (!strip->SetSignal(evind, sign)) AliError(Form("AliITSHandleDaSSD: Copy events error! mod = %i, str = %i", modind, strind));
954         }
955         module->SetStrip(strip, strind);
956       }
957     }
958     else {
959       ddlID = fModules[modind]->GetDdlId();
960       ad    = fModules[modind]->GetAD();
961       adc   = fModules[modind]->GetADC();
962     }
963   }
964   for (UShort_t modind = 0; modind < fNumberOfModules; modind++) fModules[modind]->SetModuleId(modind + 1080);  
965   return kTRUE;
966 }