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