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