Changes related to the extraction of the V0 finder into a separate class (A. Dainese...
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSPDscan.cxx
CommitLineData
b15de2d2 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
a1e17193 16#include <math.h>
17
b15de2d2 18#include <TFile.h>
19#include "AliITSOnlineSPDscan.h"
20#include "AliITSOnlineSPDscanInfo.h"
21#include "AliITSOnlineSPDHitArray.h"
22#include "AliITSOnlineSPDHitEvent.h"
23
6ddf3d66 24AliITSOnlineSPDscan::AliITSOnlineSPDscan(const Char_t *fileName, Bool_t readFromGridFile) :
b15de2d2 25 fFile(NULL),
26 fWrite(kFALSE),
27 fCurrentStep(-1),
28 fModified(kFALSE),
29 fInfoModified(kFALSE),
53ae21ce 30 fScanInfo(NULL),
31 fFileName(fileName)
b15de2d2 32{
33 // constructor, open file for reading or writing
b15de2d2 34 // look for a previously saved info object
35 // (if file not found create a new one and return, else read)
6ddf3d66 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
b15de2d2 64 fScanInfo = new AliITSOnlineSPDscanInfo();
6ddf3d66 65 fInfoModified=kTRUE;
53ae21ce 66 fFile = new TFile(fFileName.Data(), "RECREATE");
b15de2d2 67 fWrite=kTRUE;
68 }
6ddf3d66 69
b15de2d2 70 Init();
71}
72
73AliITSOnlineSPDscan::AliITSOnlineSPDscan(const AliITSOnlineSPDscan& /*scan*/) :
74 fFile(NULL),
75 fWrite(kFALSE),
76 fCurrentStep(-1),
77 fModified(kFALSE),
78 fInfoModified(kFALSE),
53ae21ce 79 fScanInfo(NULL),
80 fFileName(".")
b15de2d2 81{
82 printf("This object should not be copied!");
83}
84
85AliITSOnlineSPDscan::~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;
53ae21ce 104 fFile = new TFile(fFileName.Data(), "UPDATE");
b15de2d2 105 fWrite=kTRUE;
106 }
107 fFile->Delete("AliITSOnlineSPDscanInfo;*");
108 fFile->WriteTObject(fScanInfo, "AliITSOnlineSPDscanInfo");
08ed0911 109 }
110 if (fFile!=NULL) {
b15de2d2 111 delete fFile;
112 }
113}
114
115AliITSOnlineSPDscan& 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
b15de2d2 124void 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;
53ae21ce 139 fFile = new TFile(fFileName.Data(), "RECREATE");
b15de2d2 140 fWrite=kTRUE;
141 fFile->WriteTObject(fScanInfo, "AliITSOnlineSPDscanInfo");
142 fInfoModified=kTRUE;
143}
144
145void 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
154UInt_t AliITSOnlineSPDscan::AddScanStep() {
155 // add a new scan step
156 CreateNewStep();
157 return fScanInfo->AddScanStep();
158}
159
160void 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
182void 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
208void AliITSOnlineSPDscan::FillGap(UInt_t nsi) {
209 //create new steps until nsi is reached
210 while (nsi>=GetNSteps()) {
211 fCurrentStep = AddScanStep();
212 }
213}
214
215void AliITSOnlineSPDscan::ReadCurrentStep() {
216 // read current step index into memory
217 for (UInt_t hs=0; hs<6; hs++) {
53ae21ce 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);
b15de2d2 221 fFile->GetObject(stepName2, fCurrentHitEvent[hs]);
222 }
223}
224
225void AliITSOnlineSPDscan::SaveCurrentStep() {
226 // save current step to file
227 if (!fWrite) {
228 fFile->Close();
229 delete fFile;
53ae21ce 230 fFile = new TFile(fFileName.Data(), "UPDATE");
b15de2d2 231 fWrite=kTRUE;
232 }
233 for (UInt_t hs=0; hs<6; hs++) {
53ae21ce 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());
b15de2d2 242 }
243 fModified=kFALSE;
244}
245
246void 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}
252void 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}
258void 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}
264void 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}
268void 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}
274void 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
280UInt_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}
290Float_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}
300Float_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}
306UInt_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}
316UInt_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}
320Float_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}
330Float_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}
334Float_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}
340Float_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}
344Float_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}
364Float_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
370void AliITSOnlineSPDscan::SetType(UInt_t val) {
371 // set type
372 fScanInfo->SetType(val);
373 fInfoModified=kTRUE;
374}
375void AliITSOnlineSPDscan::SetRunNr(UInt_t val) {
376 // set run nr
377 fScanInfo->SetRunNr(val);
378 fInfoModified=kTRUE;
379}
380void AliITSOnlineSPDscan::SetRouterNr(UInt_t val) {
381 // set router nr
382 fScanInfo->SetRouterNr(val);
383 fInfoModified=kTRUE;
384}
ad18504e 385void AliITSOnlineSPDscan::SetHalfStaveScanned(UInt_t val, Bool_t b) {
386 // set half stave scanned
387 fScanInfo->SetHalfStaveScanned(val,b);
388 fInfoModified=kTRUE;
389}
390void AliITSOnlineSPDscan::SetDataFormat(UInt_t val) {
391 // set data format (0=normal 1=histogram)
392 fScanInfo->SetDataFormat(val);
393 fInfoModified=kTRUE;
394}
b15de2d2 395void AliITSOnlineSPDscan::SetTriggers(UInt_t nsi, UInt_t val) {
396 // set nr of triggers
3db5f733 397 SwitchToStep(nsi);
b15de2d2 398 fScanInfo->SetTriggers(nsi,val);
399 fInfoModified=kTRUE;
400}
401void 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}
406void AliITSOnlineSPDscan::SetRowStart(UInt_t val){
407 // set row start
408 fScanInfo->SetRowStart(val);
409 fInfoModified=kTRUE;
410}
411void AliITSOnlineSPDscan::SetRowEnd(UInt_t val){
412 // set row end
413 fScanInfo->SetRowEnd(val);
414 fInfoModified=kTRUE;
415}
416void AliITSOnlineSPDscan::SetDacStart(UInt_t val){
417 // set dac start
418 fScanInfo->SetDacStart(val);
419 fInfoModified=kTRUE;
420}
421void AliITSOnlineSPDscan::SetDacEnd(UInt_t val){
422 // set dac end
423 fScanInfo->SetDacEnd(val);
424 fInfoModified=kTRUE;
425}
426void AliITSOnlineSPDscan::SetDacStep(UInt_t val){
427 // set dac step
428 fScanInfo->SetDacStep(val);
429 fInfoModified=kTRUE;
430}
53ae21ce 431void AliITSOnlineSPDscan::SetDCSVersion(UInt_t val){
432 // set dcs db version
433 fScanInfo->SetDCSVersion(val);
434 fInfoModified=kTRUE;
435}
b15de2d2 436void AliITSOnlineSPDscan::IncrementTriggers(UInt_t nsi) {
437 // increment nr of triggers
3db5f733 438 SwitchToStep(nsi);
b15de2d2 439 fScanInfo->IncrementTriggers(nsi);
440 fInfoModified=kTRUE;
441}
442
443
444
445UInt_t AliITSOnlineSPDscan::GetNSteps() const {
446 return fScanInfo->GetNSteps();
447}
448UInt_t AliITSOnlineSPDscan::GetType() const {
449 return fScanInfo->GetType();
450}
451UInt_t AliITSOnlineSPDscan::GetRunNr() const {
452 return fScanInfo->GetRunNr();
453}
454UInt_t AliITSOnlineSPDscan::GetRouterNr() const {
455 return fScanInfo->GetRouterNr();
456}
ad18504e 457Bool_t AliITSOnlineSPDscan::GetHalfStaveScanned(UInt_t val) const {
458 return fScanInfo->GetHalfStaveScanned(val);
459}
460UInt_t AliITSOnlineSPDscan::GetDataFormat() const {
461 return fScanInfo->GetDataFormat();
462}
b15de2d2 463UInt_t AliITSOnlineSPDscan::GetTriggers(UInt_t nsi) const {
464 return fScanInfo->GetTriggers(nsi);
465}
466Bool_t AliITSOnlineSPDscan::GetChipPresent(UInt_t hs, UInt_t chipi) const {
467 return fScanInfo->GetChipPresent(hs,chipi);
468}
469UInt_t AliITSOnlineSPDscan::GetRowStart() const {
470 return fScanInfo->GetRowStart();
471}
472UInt_t AliITSOnlineSPDscan::GetRowEnd() const {
473 return fScanInfo->GetRowEnd();
474}
475UInt_t AliITSOnlineSPDscan::GetDacStart() const {
476 return fScanInfo->GetDacStart();
477}
478UInt_t AliITSOnlineSPDscan::GetDacEnd() const {
479 return fScanInfo->GetDacEnd();
480}
481UInt_t AliITSOnlineSPDscan::GetDacStep() const {
482 return fScanInfo->GetDacStep();
483}
53ae21ce 484UInt_t AliITSOnlineSPDscan::GetDCSVersion() const {
485 return fScanInfo->GetDCSVersion();
486}