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