]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSHandleDaSSD.cxx
Coding conventions
[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 <sstream>
29 #include <string>
30 #include "event.h"
31 #include "TFile.h"
32 #include "TObjArray.h"
33 #include "AliITSNoiseSSD.h"
34 #include "AliRawReaderDate.h"
35
36 #include "AliITSChannelDaSSD.h"
37
38
39 ClassImp(AliITSHandleDaSSD)
40
41 using namespace std;
42
43
44 AliITSHandleDaSSD::AliITSHandleDaSSD() :
45   fNumberOfModules(0),
46   fModules(NULL),
47   fLdcId(0),
48   fRunId(0)
49 {
50 // Default constructor
51 }
52
53
54 AliITSHandleDaSSD::AliITSHandleDaSSD(const Int_t numberofmodules) :
55   fNumberOfModules(0),
56   fModules(NULL),
57   fLdcId(0),
58   fRunId(0)
59 {
60 // Constructor allocates memory for AliITSModuleDaSSD objects
61   if (numberofmodules > fgkNumberOfSSDModules) 
62       cout << "ALICE ITS SSD contains " <<  fgkNumberOfSSDModules << "modules!"<< endl;
63   fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
64   if (fModules) {
65     fNumberOfModules = numberofmodules;
66     memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
67   } else {
68      Error("AliITSHandleDaSSD", "Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules);
69      fNumberOfModules = 0;
70      fModules = NULL;
71   }
72 }
73
74
75 AliITSHandleDaSSD::AliITSHandleDaSSD(const AliITSHandleDaSSD& ssdadldc) :
76   TObject(ssdadldc),
77   fNumberOfModules(ssdadldc.fNumberOfModules),
78   fModules(ssdadldc.fModules),
79   fLdcId(ssdadldc.fLdcId),
80   fRunId(ssdadldc.fRunId)
81 {
82   // copy constructor
83
84   Fatal("AliITSHandleDaSSD", "copy constructor not implemented");
85 }
86
87
88 AliITSHandleDaSSD& AliITSHandleDaSSD::operator = (const AliITSHandleDaSSD& ssdadldc)
89 {
90 // assignment operator
91
92   Fatal("operator =", "assignment operator not implemented");
93   return *this;
94 }
95
96
97 AliITSHandleDaSSD::~AliITSHandleDaSSD()
98 {
99 // Default destructor 
100   if (fModules) 
101   {
102     for (Int_t i = 0; i < fNumberOfModules; i++)
103     { 
104       if (fModules[i]) delete fModules[i];
105     }
106     delete [] fModules;
107   }
108 }
109
110
111
112 AliITSModuleDaSSD* AliITSHandleDaSSD::GetModule (const UChar_t ddlID, const UChar_t ad, const UChar_t adc) const
113 {
114 // Retrieve AliITSModuleDaSSD object from the array
115    if (!fModules) return NULL;
116    for (Int_t i = 0; i < fNumberOfModules; i++) {
117      if (fModules[i]) {
118        if (    (fModules[i]->GetDdlId() == ddlID)
119             && (fModules[i]->GetAD() == ad)
120             && (fModules[i]->GetADC() == adc))
121             return fModules[i];
122      }      
123    }
124    return NULL;
125 }
126
127
128
129 AliITSChannelDaSSD* AliITSHandleDaSSD::GetStrip (const UChar_t ddlID, const UChar_t ad, const UChar_t adc, const UShort_t stripID) const
130 {
131 // Retrieve AliITSChannalDaSSD object from the array
132   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
133     if (    (fModules[modind]->GetDdlId() == ddlID)
134          && (fModules[modind]->GetAD() == ad)
135          && (fModules[modind]->GetADC() == adc)  ) 
136         {
137        return fModules[modind]->GetStrip(stripID);
138     }
139   }
140   Error("AliITSHandleDaSSD", "Error GetStrip (%i, %i, %i, %i), strip not found, returns NULL!", ddlID, ad, adc, stripID);
141   return NULL;
142 }
143
144
145
146 Bool_t AliITSHandleDaSSD::SetModule(AliITSModuleDaSSD *const module, const Int_t index)
147
148 // Assign array element with AliITSModuleDaSSD object
149    if ((index < fNumberOfModules) && (index >= 0)) 
150      { 
151         if (fModules[index]) delete fModules[index];
152         fModules[index] = module;
153         return kTRUE;
154       }
155    else return kFALSE;                         
156 }
157
158
159 Bool_t AliITSHandleDaSSD::SetNumberOfModules (const Int_t numberofmodules)
160 {
161 // Allocates memory for AliITSModuleDaSSD objects
162   if (numberofmodules > fgkNumberOfSSDModules)
163     Warning("AliITSHandleDaSSD", "The number of modules %i you use exceeds the maximum %i for ALICE ITS SSD", numberofmodules, fgkNumberOfSSDModules);
164   if (fModules) { delete [] fModules; fModules = NULL; }
165   fModules = new (nothrow) AliITSModuleDaSSD* [numberofmodules];
166   if (fModules) {
167     fNumberOfModules = numberofmodules;
168     memset(fModules, 0, sizeof(AliITSModuleDaSSD*) * numberofmodules);
169     return kTRUE;
170   } else {
171      Error("AliITSHandleDaSSD", "Error allocating memory for %i AliITSModulesDaSSD* objects!", numberofmodules);
172      fNumberOfModules = 0;
173      fModules = NULL;
174   }
175   return kFALSE;
176 }
177
178
179 Bool_t AliITSHandleDaSSD::ReadCalibrationDataFile (const char* fileName, const Long_t eventsnumber)
180 {
181 // Reads raw data from file
182   AliRawReaderDate    *rawreaderdate = new AliRawReaderDate(fileName, 0);
183   AliITSModuleDaSSD   *module;
184   AliITSChannelDaSSD  *strip;
185   Long_t            datasize, eventind = 0;
186   Int_t             nofstrips, eqbelsize;
187   UShort_t          modind;
188   long32           *data;
189   if (!fModules) {
190     Error("AliITSHandleDaSSD", "Error ReadCalibrationDataFile: no structure was allocated for data");
191     return kFALSE;
192   }
193   rawreaderdate->SelectEvents(-1);
194   while (rawreaderdate->NextEvent()) {
195     if ((rawreaderdate->GetType() != PHYSICS_EVENT) && (rawreaderdate->GetType() != CALIBRATION_EVENT)) continue;
196     fLdcId = rawreaderdate->GetLDCId();
197     fRunId = rawreaderdate->GetRunNumber(); 
198     modind = 0;
199     while (rawreaderdate->ReadNextData((UChar_t*&)data)) {
200       Int_t     equipid    = rawreaderdate->GetEquipmentId();              //  EquipmentID required to access to rorc
201       Int_t     equiptype  = rawreaderdate->GetEquipmentType();            //
202       UChar_t   ddlID      = (UChar_t)rawreaderdate->GetDDLID();           // GetDDLID(); index of DDL, ITS SSD: 33-48
203       datasize = rawreaderdate->GetDataSize();
204       eqbelsize = rawreaderdate->GetEquipmentElementSize();
205       if ( (datasize % eqbelsize) || (eqbelsize != sizeof(long32)) ) {
206         Error("AliITSHandleDaSSD", "Error ReadCalibrationDataFile: event data size %i is not an integer of equipment data size %i", datasize,
207                eqbelsize);
208         return kFALSE;
209       }
210       nofstrips = (Int_t) (datasize / eqbelsize);                
211       for (Int_t strind = 0; strind < nofstrips; strind++) {
212         UChar_t   ad      = (UChar_t) (data[strind] >> 28) & 0x0000000F;  // index of AD module     0-9
213         UChar_t   adc     = (UChar_t) (data[strind] >> 24) & 0x0000000F;  // index of ADC module    0-5, 8-13
214         UShort_t  stripID = (UShort_t)(data[strind] >> 12) & 0x000007FF;  // strip number           0-1535
215         Short_t   signal  = (Short_t)(data[strind] & 0x00000FFF);
216                   signal  = (signal > AliITSChannelDaSSD::GetUnderflowConst()) ? (signal - 2 * AliITSChannelDaSSD::GetUnderflowConst()) 
217                                                                                : signal;
218         if (!(module = GetModule(ddlID, ad, adc))) {
219           module = new AliITSModuleDaSSD(AliITSModuleDaSSD::GetStripsPerModuleConst());
220           if (!module->SetModuleIdData (ddlID, ad, adc, modind)) return kFALSE;
221           module->SetModuleRorcId (equipid, equiptype);
222           fModules[modind++] = module;
223         }
224         if (!(strip = module->GetStrip(stripID))) {
225           strip = new AliITSChannelDaSSD(stripID, eventsnumber);
226           module->SetStrip(strip, stripID);
227         }
228         strip->SetSignal(eventind, signal);
229      }
230      if (modind) cout << "The memory was allocated for " <<  modind << " modules." << endl;
231    }
232    if (++eventind > eventsnumber) break;
233   }
234   delete rawreaderdate;
235   return RelocateModules();
236 }
237
238
239 Bool_t AliITSHandleDaSSD::RelocateModules()
240 {
241 // Relocate memory for AliITSModuleDaSSD object array
242   Int_t         nm = 0;
243   AliITSModuleDaSSD **marray = NULL;
244   for (Int_t modind = 0; modind < fNumberOfModules; modind++)
245     if (fModules[modind]) nm += 1;
246   if (nm == fNumberOfModules) return kTRUE;
247   marray = new (nothrow) AliITSModuleDaSSD* [nm];
248   if (!marray) {
249     Error("AliITSHandleDaSSD", "Error relocating memory for %i AliITSModuleDaSSD* objects!", nm);
250     return kFALSE;
251   }
252   nm = 0;
253   for (Int_t modind = 0; modind < fNumberOfModules; modind++)  
254     if (fModules[modind]) marray[nm++] = fModules[modind];
255   delete [] fModules;
256   fModules = marray;
257   fNumberOfModules = nm;
258   return kTRUE;
259 }
260
261
262 Bool_t AliITSHandleDaSSD::CalculatePedestal()
263 {
264 // Calculates Pedestal
265   Float_t     pedestal;
266   Short_t    *signal;
267   AliITSChannelDaSSD *strip;
268   Long_t      ovev, ev;
269   if (!fModules) return kFALSE;
270   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
271     if (!fModules[modind]) {
272       Error("AliITSHandleDaSSD", "Error CalculatePedestal(): No AliITSChannelDaSSD object with index %i is allocated in AliITSModuleDaSSD\n", modind);
273       return kFALSE;
274     }
275     for (Int_t strind = 0; strind < fModules[modind]->GetNumberOfStrips(); strind++) {
276       if (!(strip = fModules[modind]->GetStrip(strind))) return kFALSE;
277       if (!(signal = strip->GetSignal())) {
278         Error("AliITSHandleDaSSD", "Error CalculatePedestal(): there are no events data for module[%i] strip[%i]->GetSignal()", modind, strind);
279         return kFALSE;
280       }
281       pedestal = 0.0f;
282       ovev = 0l;
283       for (ev = 0; ev < strip->GetEventsNumber(); ev++)
284         if (SignalOutOfRange(signal[ev])) ovev += 1;
285 //      else pedestal += signal[ev];
286         else pedestal = ((ev - ovev)) ? pedestal + (signal[ev] - pedestal) / static_cast<Float_t>(ev - ovev + 1) : signal[ev];
287         if (ev == ovev) pedestal = AliITSChannelDaSSD::GetUndefinedValue();
288 //      if ((Long_t n = strip->GetEventsNumber() - ovev)) pedestal /= static_cast<Float_t>(n);
289 //      else return kFALSE;
290       strip->SetPedestal(pedestal);     
291     }
292   }
293   return kTRUE;
294 }
295
296
297
298 Bool_t AliITSHandleDaSSD::CalculateNoise()
299 {
300 // Calculates Noise
301   AliITSChannelDaSSD *strip;
302   Short_t    *signal;
303   Float_t     noise;
304   Long_t      ovev, n;
305   if (!fModules) return kFALSE;
306   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
307     if (!fModules[modind]) {
308       Error("AliITSHandleDaSSD", "Error CalculateNoise(): No AliITSChannelDaSSD object with index %i is allocated in AliITSModuleDaSSD\n", modind);
309       return kFALSE;
310     }
311     for (Int_t strind = 0; strind < fModules[modind]->GetNumberOfStrips(); strind++) {
312       if (!(strip = fModules[modind]->GetStrip(strind))) return kFALSE;
313       if (!(signal = strip->GetSignal())) {
314         Error("AliITSHandleDaSSD", "Error CalculateNoise(): there are no events data for module[%i] strip[%i]->GetSignal()", modind, strind);
315         return kFALSE;
316       }
317       Double_t nsum = 0.0L;
318       ovev = 0l;
319       for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
320         if (SignalOutOfRange(signal[ev])) ovev += 1;
321         else nsum += pow((signal[ev] - strip->GetPedestal()), 2);
322 //      else nsum = ((ev - ovev)) ? nsum + (pow((signal[ev] - strip->GetPedestal()), 2) - nsum) / static_cast<Double_t>(ev - ovev)
323 //                                : pow((signal[ev] - strip->GetPedestal()), 2);
324       } 
325 //      noise =  sqrt(nsum);
326       if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
327       else  noise = AliITSChannelDaSSD::GetUndefinedValue();
328       strip->SetNoise(noise);
329     }
330   }
331   return kTRUE;
332 }
333
334
335
336 Bool_t AliITSHandleDaSSD::CalculateCM(const Int_t modind, const Int_t stripind, Float_t* const cm)
337 {
338 // Calculates CM
339   AliITSChannelDaSSD *strip = NULL;
340   Short_t    *signal;
341   Long_t      ovstr, evn, n;
342   if ((stripind + AliITSModuleDaSSD::GetStripsPerChip()) > fModules[modind]->GetNumberOfStrips()) return kFALSE;
343   if (!(strip = fModules[modind]->GetStrip(stripind))) return kFALSE;
344   evn = fModules[modind]->GetStrip(stripind)->GetEventsNumber();
345   for (Long_t ev = 0; ev < evn; ev++) {
346     Double_t cmsum = 0.0L;
347     ovstr = 0l;
348     for (Int_t strind = stripind; strind < (stripind + AliITSModuleDaSSD::GetStripsPerChip()); strind++) {
349       if (!(strip = fModules[modind]->GetStrip(strind))) return kFALSE;
350       if (!(signal = strip->GetSignal())) {
351         Error("AliITSHandleDaSSD", "Error CalculateCM: there are no events data for module[%i] strip[%i]->GetSignal()", modind, strind);
352         return kFALSE;
353       }
354       if (SignalOutOfRange(signal[ev])) ovstr += 1;
355       else cmsum += (signal[ev] - strip->GetPedestal());
356     }
357     if ((n = AliITSModuleDaSSD::GetStripsPerChip() - ovstr)) cm[ev] = cmsum / (Float_t)(n);
358     else cm[ev] = 0.0;
359   } 
360   return kTRUE; 
361 }
362
363
364
365 Bool_t  AliITSHandleDaSSD::CalculateNoiseCM()
366 {
367 // Calculates Noise with CM correction
368   Short_t     *signal;
369   AliITSChannelDaSSD  *strip = NULL;
370   Float_t      noise, *cm = NULL;
371   Long_t       ovev, n;
372   if (!fModules) return kFALSE;
373   for (Int_t modind = 0; modind < fNumberOfModules; modind++) {
374     if (!fModules[modind]) {
375       Error("AliITSHandleDaSSD", "Error CalculateNoiseCM(): No AliITSChannelDaSSD object with index %i is allocated in AliITSModuleDaSSD", modind);
376       return kFALSE;
377     }
378     for (Int_t strind = 0; strind < fModules[modind]->GetNumberOfStrips(); strind++) {
379       if (!(strip = fModules[modind]->GetStrip(strind))) return kFALSE;
380       if (!(signal = strip->GetSignal())) {
381         Error("AliITSHandleDaSSD", "Error CalculateNoiseCM(): there are no events data for module[%i] strip[%i]->GetSignal()", modind, strind);
382         return kFALSE;
383       }
384       if (!(strind % AliITSModuleDaSSD::GetStripsPerChip())) {
385         if (!cm) {
386           cm = new (nothrow) Float_t [strip->GetEventsNumber()];
387           if (!cm) {
388             Warning("AliITSHandleDaSSD", "Noise calculation with common mode correction failed becouse of memory allocation problems.");
389             return kFALSE; 
390           }
391         }
392 // calculate cm;
393         if (!CalculateCM(modind, strind, cm)) return kFALSE;
394       }
395 // calculate noise;     
396       Double_t nsum = 0.0L;
397       ovev = 0l;
398       for (Long_t ev = 0; ev < strip->GetEventsNumber(); ev++) {
399         if (SignalOutOfRange(signal[ev])) ovev += 1;
400         else nsum += pow((signal[ev] - strip->GetPedestal() - cm[ev]), 2);
401 //      else nsum = ((ev - ovev)) ? nsum + (pow((signal[ev] - strip->GetPedestal() - cm[ev]), 2) - nsum) / static_cast<Double_t>(ev - ovev)
402 //                                : pow((signal[ev] - strip->GetPedestal() - cm[ev]), 2);
403       } 
404 //      noise =  sqrt(nsum);
405       if ((n = strip->GetEventsNumber() - ovev - 1) > 0) noise =  sqrt(nsum / (Float_t)(n));
406       else  noise = AliITSChannelDaSSD::GetUndefinedValue();
407       strip->SetNoise(noise);
408       }
409     }
410   if (cm) delete [] cm;
411   return kTRUE;
412 }
413
414
415
416 TObjArray* AliITSHandleDaSSD::GetCalibrationSSDLDC() const
417 {
418 // Fill in the array for OCDB 
419   TObjArray *ldcc;
420   if (!fModules) return NULL;
421   ldcc = new TObjArray(fNumberOfModules, 0);
422   for (Int_t i = 0; i < fNumberOfModules; i++) {
423     if (!fModules[i]) {
424       delete ldcc;
425       return NULL;
426     }
427     ldcc->AddAt(dynamic_cast<TObject*>(fModules[i]->GetCalibrationSSDModule()), i);
428   }
429   ldcc->Compress();
430   return ldcc;
431 }
432
433
434 Bool_t AliITSHandleDaSSD::SaveCalibrationSSDLDC(string& dafname) const
435 {
436 // Save Calibration data locally
437   ostringstream   dadatafilename;
438   TObjArray      *ldcc;
439   if (!fModules) return kFALSE;
440   ldcc = new TObjArray(fNumberOfModules, 0);
441   for (Int_t i = 0; i < fNumberOfModules; i++) {
442     if (!fModules[i]) {
443       delete ldcc;
444       return kFALSE;
445     }
446     ldcc->AddAt(dynamic_cast<TObject*>(fModules[i]->GetCalibrationSSDModule()), i);
447   }
448   ldcc->Compress();
449   dadatafilename << dafname << "/ITSSSDda_" << fLdcId << "_" << fRunId << ".root";
450   dafname = dadatafilename.str();
451   TFile *fileRun = new TFile (dadatafilename.str().data(),"RECREATE");
452   if (fileRun->IsZombie()) {
453     Error("AliITSHandleDaSSD", "SaveCalibrationSSDLDC() error open file %s", dadatafilename.str().data());
454     ldcc->Delete();
455     delete fileRun;
456     return kFALSE;
457   }
458   fileRun->WriteTObject(ldcc);
459   fileRun->Close();
460   ldcc->Delete();
461   delete fileRun;
462   return kTRUE;
463 }