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