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