]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSOnlineSPDscan.cxx
Fix for a Coverity warning (Ruben)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSPDscan.cxx
1 ////////////////////////////////////////////////////////////
2 // Author: Henrik Tydesjo                                 //
3 // Interface class to the containers of an online scan.   //
4 // Directly connected to a TFile with all containers.     //
5 // Handles reading and writing of this TFile.             //
6 // Hitmaps and information on nr of events with hits      //
7 // is stored in this file (AliITSOnlineSPDHitArray and    //
8 // AliITSOnlineSPDHitEvent). Also some general            //
9 // information is stored (AliITSOnlineSPDscanInfo).       //
10 // When switching between different steps of the scan,    //
11 // the previous step is automatically stored on the file. //
12 // With this scheme there is no risk of running out of    //
13 // memory.                                                //
14 ////////////////////////////////////////////////////////////
15
16 #include <math.h>
17
18 #include <TFile.h>
19 #include "AliITSOnlineSPDscan.h"
20 #include "AliITSOnlineSPDscanInfo.h"
21 #include "AliITSOnlineSPDHitArray.h"
22 #include "AliITSOnlineSPDHitEvent.h"
23
24 AliITSOnlineSPDscan::AliITSOnlineSPDscan(const Char_t *fileName, Bool_t readFromGridFile) :
25   fFile(NULL),
26   fWrite(kFALSE),
27   fCurrentStep(-1),
28   fModified(kFALSE),
29   fInfoModified(kFALSE),
30   fScanInfo(NULL),
31   fFileName(fileName)
32 {
33   // constructor, open file for reading or writing
34   // look for a previously saved info object 
35   // (if file not found create a new one and return, else read)
36
37   Bool_t bRead = readFromGridFile;
38
39   if (!bRead) {
40     FILE* fp0 = fopen(fFileName.Data(), "r");
41     if (fp0 != NULL) {
42       bRead=kTRUE;
43       fclose(fp0);
44     }
45   }
46
47   if (bRead) { // open file for reading
48     fFile = TFile::Open(fFileName.Data(), "READ");
49     if (fFile==NULL) { // grid file not found, create new local default file
50       printf("ERROR: AliITSOnlineSPDscan: File %s not found! Creating 'test999.root' file instead\n",fFileName.Data());
51       // create default empty file:
52       fFileName = "test999.root";
53       fScanInfo = new AliITSOnlineSPDscanInfo();
54       fInfoModified=kTRUE;
55       fFile = new TFile(fFileName.Data(), "RECREATE");
56       fWrite=kTRUE;
57     }
58     else { // read from file (grid or local)
59       fWrite=kFALSE;
60       fFile->GetObject("AliITSOnlineSPDscanInfo", fScanInfo);
61     }
62   }
63   else { // create new local file
64     fScanInfo = new AliITSOnlineSPDscanInfo();
65     fInfoModified=kTRUE;
66     fFile = new TFile(fFileName.Data(), "RECREATE");
67     fWrite=kTRUE;
68   }
69
70   Init();
71 }
72
73 AliITSOnlineSPDscan::AliITSOnlineSPDscan(const AliITSOnlineSPDscan& /*scan*/) :
74   fFile(NULL),
75   fWrite(kFALSE),
76   fCurrentStep(-1),
77   fModified(kFALSE),
78   fInfoModified(kFALSE),
79   fScanInfo(NULL),
80   fFileName(".")
81 {
82   printf("This object should not be copied!");
83 }
84
85 AliITSOnlineSPDscan::~AliITSOnlineSPDscan() {
86   // destructor
87   if (fModified) {
88     SaveCurrentStep();
89   }
90   for (UInt_t hs=0; hs<6; hs++) {
91     if (fCurrentHitArray[hs]!=NULL) {
92       delete fCurrentHitArray[hs];
93       fCurrentHitArray[hs]=NULL;
94     }
95     if (fCurrentHitEvent[hs]!=NULL) {
96       delete fCurrentHitEvent[hs];
97       fCurrentHitEvent[hs]=NULL;
98     }
99   }
100   if (fInfoModified) {
101     if (!fWrite) {
102       fFile->Close();
103       delete fFile;
104       fFile = new TFile(fFileName.Data(), "UPDATE");
105       fWrite=kTRUE;
106     }
107     fFile->Delete("AliITSOnlineSPDscanInfo;*");
108     fFile->WriteTObject(fScanInfo, "AliITSOnlineSPDscanInfo");
109   }
110   if (fFile!=NULL) {
111     delete fFile;
112   }
113 }
114
115 AliITSOnlineSPDscan& AliITSOnlineSPDscan::operator=(const AliITSOnlineSPDscan& scan) {
116   // assignment operator (should not be used)
117   printf("This object should not be copied!");
118   if (this!=&scan) {
119     // still do nothing...
120   }
121   return *this;
122 }
123
124 void AliITSOnlineSPDscan::ClearThis() {
125   // clear this scan, close file and open new
126   for (UInt_t hs=0; hs<6; hs++) {
127     if (fCurrentHitArray[hs]!=NULL) {
128       delete fCurrentHitArray[hs];
129     }
130     fCurrentHitArray[hs] = NULL;
131     if (fCurrentHitEvent[hs]!=NULL) {
132       delete fCurrentHitEvent[hs];
133     }
134     fCurrentHitEvent[hs] = NULL;
135   }
136   fScanInfo->ClearThis();
137   fFile->Close();
138   delete fFile;
139   fFile = new TFile(fFileName.Data(), "RECREATE");
140   fWrite=kTRUE;
141   fFile->WriteTObject(fScanInfo, "AliITSOnlineSPDscanInfo");
142   fInfoModified=kTRUE;
143 }
144
145 void AliITSOnlineSPDscan::Init() {
146   // init hit arrays and hit events
147   for (UInt_t hs=0; hs<6; hs++) {
148     fCurrentHitArray[hs]=NULL;
149     fCurrentHitEvent[hs]=NULL;
150   }
151
152 }
153
154 UInt_t AliITSOnlineSPDscan::AddScanStep() {
155   // add a new scan step
156   CreateNewStep();
157   return fScanInfo->AddScanStep();
158 }
159
160 void AliITSOnlineSPDscan::CreateNewStep() {
161   // create a new step
162   // save current step to file (if modified)
163   if (fModified) {
164     SaveCurrentStep();
165   }
166   // create new step
167   for (UInt_t hs=0; hs<6; hs++) {
168     if (fCurrentHitArray[hs]!=NULL) {
169       delete fCurrentHitArray[hs];
170     }
171     fCurrentHitArray[hs] = new AliITSOnlineSPDHitArray();
172     if (fCurrentHitEvent[hs]!=NULL) {
173       delete fCurrentHitEvent[hs];
174     }
175     fCurrentHitEvent[hs] = new AliITSOnlineSPDHitEvent();
176   }
177   fCurrentStep = fScanInfo->GetNSteps();
178   fModified=kTRUE;
179   fInfoModified=kTRUE;
180 }
181
182 void AliITSOnlineSPDscan::SwitchToStep(UInt_t nsi) {
183   // switch to step nsi (save current step first if needed)
184   if ((Int_t)nsi!=fCurrentStep) {
185     if (fModified) {
186       SaveCurrentStep();
187     }
188     for (UInt_t hs=0; hs<6; hs++) {
189       if (fCurrentHitArray[hs]!=NULL) {
190         delete fCurrentHitArray[hs];
191         fCurrentHitArray[hs]=NULL;
192       }
193       if (fCurrentHitEvent[hs]!=NULL) {
194         delete fCurrentHitEvent[hs];
195         fCurrentHitEvent[hs]=NULL;
196       }
197     }
198     if (nsi>=GetNSteps()) {
199       FillGap(nsi); // makes fCurrentStep = nsi
200     }
201     else {
202       fCurrentStep=nsi;
203       ReadCurrentStep();
204     }
205   }
206 }
207
208 void AliITSOnlineSPDscan::FillGap(UInt_t nsi) {
209   //create new steps until nsi is reached
210   while (nsi>=GetNSteps()) {
211     fCurrentStep = AddScanStep();
212   }
213 }
214
215 void AliITSOnlineSPDscan::ReadCurrentStep() {
216   // read current step index into memory
217   for (UInt_t hs=0; hs<6; hs++) {
218     TString stepName = Form("HitArray_HS%d_Step%d",hs,fCurrentStep);
219     fFile->GetObject(stepName.Data(), fCurrentHitArray[hs]);
220     TString stepName2 = Form("HitEvent_HS%d_Step%d",hs,fCurrentStep);
221     fFile->GetObject(stepName2, fCurrentHitEvent[hs]);
222   }
223 }
224
225 void AliITSOnlineSPDscan::SaveCurrentStep() {
226   // save current step to file
227   if (!fWrite) {
228     fFile->Close();
229     delete fFile;
230     fFile = new TFile(fFileName.Data(), "UPDATE");
231     fWrite=kTRUE;
232   }
233   for (UInt_t hs=0; hs<6; hs++) {
234     TString stepName = Form("HitArray_HS%d_Step%d",hs,fCurrentStep);
235     TString stepDelete = Form("%s;*",stepName.Data());
236     fFile->Delete(stepDelete.Data());
237     fFile->WriteTObject(fCurrentHitArray[hs], stepName.Data());
238     TString stepName2 = Form("HitEvent_HS%d_Step%d",hs,fCurrentStep);
239     TString stepDelete2 = Form("%s;*",stepName2.Data());
240     fFile->Delete(stepDelete2.Data());
241     fFile->WriteTObject(fCurrentHitEvent[hs], stepName2.Data());
242   }
243   fModified=kFALSE;
244 }
245
246 void AliITSOnlineSPDscan::SetHits(UInt_t nsi, UInt_t hs, UInt_t chipi, UInt_t coli, UInt_t rowi, UInt_t val) {
247   // set nr of hits for pixel
248   SwitchToStep(nsi);
249   fCurrentHitArray[hs]->SetHits(chipi,coli,rowi,val);
250   fModified=kTRUE;
251 }
252 void AliITSOnlineSPDscan::IncrementHits(UInt_t nsi, UInt_t hs, UInt_t chipi, UInt_t coli, UInt_t rowi) {
253   // increment nr of hits for pixel
254   SwitchToStep(nsi);
255   fCurrentHitArray[hs]->IncrementHits(chipi,coli,rowi);
256   fModified=kTRUE;
257 }
258 void AliITSOnlineSPDscan::SetHitEvents(UInt_t nsi, UInt_t hs, UInt_t chipi, Int_t val) {
259   // set nr of hit events for a chip
260   SwitchToStep(nsi);
261   fCurrentHitEvent[hs]->SetHitEvent(chipi,val);
262   fModified=kTRUE;
263 }
264 void AliITSOnlineSPDscan::SetHitEventsTot(UInt_t nsi, UInt_t hs, Int_t val) {
265   // set nr of hit events for 10 chips together
266   SetHitEvents(nsi,hs,10,val);
267 }
268 void AliITSOnlineSPDscan::IncrementHitEvents(UInt_t nsi, UInt_t hs, UInt_t chipi) {
269   // increment nr of hit events for a chip
270   SwitchToStep(nsi);
271   fCurrentHitEvent[hs]->IncrementHitEvent(chipi);
272   fModified=kTRUE;
273 }
274 void AliITSOnlineSPDscan::IncrementHitEventsTot(UInt_t nsi, UInt_t hs) {
275   // increment nr of hit events for 10 chips
276   IncrementHitEvents(nsi,hs,10);
277 }
278
279
280 UInt_t AliITSOnlineSPDscan::GetHits(UInt_t nsi, UInt_t hs, UInt_t chipi, UInt_t coli, UInt_t rowi) {
281   // get nr of hits for pixel
282   if (nsi<GetNSteps()) {
283     SwitchToStep(nsi);
284     return fCurrentHitArray[hs]->GetHits(chipi,coli,rowi);
285   }
286   else {
287     return 0;
288   }
289 }
290 Float_t AliITSOnlineSPDscan::GetHitsEfficiency(UInt_t nsi, UInt_t hs, UInt_t chipi, UInt_t coli, UInt_t rowi) {
291   // get the hit efficiency for pixel
292   UInt_t ntr = GetTriggers(nsi);
293   if (ntr>0) {
294     return ((Float_t)GetHits(nsi,hs,chipi,coli,rowi))/ntr;
295   }
296   else {
297     return 0;
298   }
299 }
300 Float_t AliITSOnlineSPDscan::GetHitsEfficiencyError(UInt_t nsi, UInt_t hs, UInt_t chipi, UInt_t coli, UInt_t rowi) {
301   // get error in hit efficiency for pixel
302   Float_t hits = GetHits(nsi,hs,chipi,coli,rowi);
303   UInt_t ntr = GetTriggers(nsi);
304   return sqrt(hits*(ntr-hits)/ntr)/ntr;
305 }
306 UInt_t AliITSOnlineSPDscan::GetHitEvents(UInt_t nsi, UInt_t hs, UInt_t chipi) {
307   // get nr of hit events for a chip
308   if (nsi<GetNSteps()) {
309     SwitchToStep(nsi);
310     return fCurrentHitEvent[hs]->GetHitEvent(chipi);
311   }
312   else {
313     return 0;
314   }
315 }
316 UInt_t AliITSOnlineSPDscan::GetHitEventsTot(UInt_t nsi, UInt_t hs) {
317   // get nr of hit events for 10 chips
318   return GetHitEvents(nsi,hs,10);
319 }
320 Float_t AliITSOnlineSPDscan::GetHitEventsEfficiency(UInt_t nsi, UInt_t hs, UInt_t chipi) {
321   // get the hit events efficiency for a chip
322   UInt_t ntr = GetTriggers(nsi);
323   if (ntr>0) {
324     return ((Float_t)GetHitEvents(nsi,hs,chipi))/ntr;
325   }
326   else {
327     return 0;
328   }
329 }
330 Float_t AliITSOnlineSPDscan::GetHitEventsTotEfficiency(UInt_t nsi, UInt_t hs) {
331   // get the hit events efficiency for 10 chips
332   return GetHitEventsEfficiency(nsi,hs,10);
333 }
334 Float_t AliITSOnlineSPDscan::GetHitEventsEfficiencyError(UInt_t nsi, UInt_t hs, UInt_t chipi) {
335   // get error in hit events efficiency for a chip
336   Float_t hitevents = (Float_t) GetHitEvents(nsi,hs,chipi);
337   UInt_t ntr = GetTriggers(nsi);
338   return sqrt(hitevents*(ntr-hitevents)/ntr)/ntr;
339 }
340 Float_t AliITSOnlineSPDscan::GetHitEventsTotEfficiencyError(UInt_t nsi, UInt_t hs) {
341   // get error in hit events efficiency for 10 chips
342   return GetHitEventsEfficiencyError(nsi,hs,10);
343 }
344 Float_t AliITSOnlineSPDscan::GetAverageMultiplicity(UInt_t nsi, UInt_t hs, UInt_t chipi) {
345   // get average multiplicity for a chip
346   Float_t nrhits = 0;
347   for (UInt_t chip=0;chip<10;chip++) {
348     if (chipi==10 || chip==chipi) {
349       for (Int_t col=0; col<32; col++) {
350         for (Int_t row=0; row<256; row++) {
351           nrhits+=GetHits(nsi,hs,chip,col,row);
352         }
353       }
354     }
355   }
356   UInt_t ntr = GetTriggers(nsi);
357   if (ntr>0) {
358     return nrhits/ntr;
359   }
360   else {
361     return 0;
362   }
363 }
364 Float_t AliITSOnlineSPDscan::GetAverageMultiplicityTot(UInt_t nsi, UInt_t hs) {
365   // get average multiplicity for 10 chips
366   return GetAverageMultiplicity(nsi,hs,10);
367 }
368
369
370 void AliITSOnlineSPDscan::SetType(UInt_t val) {
371   // set type
372   fScanInfo->SetType(val);
373   fInfoModified=kTRUE;
374 }
375 void AliITSOnlineSPDscan::SetRunNr(UInt_t val) {
376   // set run nr
377   fScanInfo->SetRunNr(val);
378   fInfoModified=kTRUE;
379 }
380 void AliITSOnlineSPDscan::SetRouterNr(UInt_t val) {
381   // set router nr
382   fScanInfo->SetRouterNr(val);
383   fInfoModified=kTRUE;
384 }
385 void AliITSOnlineSPDscan::SetHalfStaveScanned(UInt_t val, Bool_t b) {
386   // set half stave scanned
387   fScanInfo->SetHalfStaveScanned(val,b);
388   fInfoModified=kTRUE;
389 }
390 void AliITSOnlineSPDscan::SetDataFormat(UInt_t val) {
391   // set data format (0=normal 1=histogram)
392   fScanInfo->SetDataFormat(val);
393   fInfoModified=kTRUE;
394 }
395 void AliITSOnlineSPDscan::SetTriggers(UInt_t nsi, UInt_t val) {
396   // set nr of triggers
397   SwitchToStep(nsi);
398   fScanInfo->SetTriggers(nsi,val);
399   fInfoModified=kTRUE;
400 }
401 void AliITSOnlineSPDscan::SetChipPresent(UInt_t hs, UInt_t chipi, Bool_t val){
402   // set chip present
403   fScanInfo->SetChipPresent(hs,chipi,val); 
404   fInfoModified=kTRUE;
405 }
406 void AliITSOnlineSPDscan::SetRowStart(UInt_t val){
407   // set row start
408   fScanInfo->SetRowStart(val); 
409   fInfoModified=kTRUE;
410 }
411 void AliITSOnlineSPDscan::SetRowEnd(UInt_t val){
412   // set row end
413   fScanInfo->SetRowEnd(val); 
414   fInfoModified=kTRUE;
415 }
416 void AliITSOnlineSPDscan::SetDacStart(UInt_t val){
417   // set dac start
418   fScanInfo->SetDacStart(val); 
419   fInfoModified=kTRUE;
420 }
421 void AliITSOnlineSPDscan::SetDacEnd(UInt_t val){
422   // set dac end
423   fScanInfo->SetDacEnd(val); 
424   fInfoModified=kTRUE;
425 }  
426 void AliITSOnlineSPDscan::SetDacStep(UInt_t val){
427   // set dac step
428   fScanInfo->SetDacStep(val); 
429   fInfoModified=kTRUE;
430 }
431 void AliITSOnlineSPDscan::SetDCSVersion(UInt_t val){
432   // set dcs db version
433   fScanInfo->SetDCSVersion(val); 
434   fInfoModified=kTRUE;
435 }
436 void AliITSOnlineSPDscan::IncrementTriggers(UInt_t nsi) {
437   // increment nr of triggers
438   SwitchToStep(nsi);
439   fScanInfo->IncrementTriggers(nsi); 
440   fInfoModified=kTRUE;
441 }
442
443
444
445 UInt_t AliITSOnlineSPDscan::GetNSteps() const {
446   return fScanInfo->GetNSteps();
447 }
448 UInt_t AliITSOnlineSPDscan::GetType() const {
449   return fScanInfo->GetType();
450 }
451 UInt_t AliITSOnlineSPDscan::GetRunNr() const {
452   return fScanInfo->GetRunNr();
453 }
454 UInt_t AliITSOnlineSPDscan::GetRouterNr() const {
455   return fScanInfo->GetRouterNr();
456 }
457 Bool_t AliITSOnlineSPDscan::GetHalfStaveScanned(UInt_t val) const {
458   return fScanInfo->GetHalfStaveScanned(val);
459 }
460 UInt_t AliITSOnlineSPDscan::GetDataFormat() const {
461   return fScanInfo->GetDataFormat();
462 }
463 UInt_t AliITSOnlineSPDscan::GetTriggers(UInt_t nsi) const {
464   return fScanInfo->GetTriggers(nsi);
465 }
466 Bool_t AliITSOnlineSPDscan::GetChipPresent(UInt_t hs, UInt_t chipi) const {
467   return fScanInfo->GetChipPresent(hs,chipi);
468 }
469 UInt_t AliITSOnlineSPDscan::GetRowStart() const {
470   return fScanInfo->GetRowStart();
471 }
472 UInt_t AliITSOnlineSPDscan::GetRowEnd() const {
473   return fScanInfo->GetRowEnd();
474 }
475 UInt_t AliITSOnlineSPDscan::GetDacStart() const {
476   return fScanInfo->GetDacStart();
477 }
478 UInt_t AliITSOnlineSPDscan::GetDacEnd() const {
479   return fScanInfo->GetDacEnd();
480 }
481 UInt_t AliITSOnlineSPDscan::GetDacStep() const {
482   return fScanInfo->GetDacStep();
483 }
484 UInt_t AliITSOnlineSPDscan::GetDCSVersion() const {
485   return fScanInfo->GetDCSVersion();
486 }