]> git.uio.no Git - u/mrichter/AliRoot.git/blob - RALICE/AliSignal.cxx
29-oct-2003 NvE Obsolete include of AliCalorimeter.h removed from AliEvent header...
[u/mrichter/AliRoot.git] / RALICE / AliSignal.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 // $Id$
17
18 ///////////////////////////////////////////////////////////////////////////
19 // Class AliSignal
20 // Generic handling of (extrapolated) detector signals.
21 //
22 // Note :
23 // ------
24 // Signal positions (r) and reference frames (f) are specified via
25 // SetPosition(r,f) under the following conventions :
26 //
27 // f="car" ==> r is Cartesian   (x,y,z)
28 // f="sph" ==> r is Spherical   (r,theta,phi)
29 // f="cyl" ==> r is Cylindrical (rho,phi,z)
30 //
31 // The same holds for SetPositionErrors().
32 //
33 // All angles are in radians.
34 //
35 // Example :
36 // ---------
37 //
38 // AliSignal s;
39 // s.SetName("Start counter");
40 // Float_t pos[3]={-1,25,7};
41 // Float_t err[3]={0.03,0.7,0.18};
42 // Float_t signal=120.8;
43 // Float_t error=1.73;
44 // Float_t offset=-12.78;
45 // Float_t gain=250;
46 // s.SetPosition(pos,"car");
47 // s.SetPositionErrors(err,"car");
48 // s.SetSignal(signal);
49 // s.SetSignalError(error);
50 // s.SetOffset(offset);
51 // s.SetGain(gain);
52 // Float_t loc[3],dr[3],sigma;
53 // s.GetPosition(loc,"sph");
54 // s.GetPositionErrors(dr,"sph");
55 // Float_t adc=s.GetSignal();
56 // Float_t sigma=s.GetSignalError();
57 //
58 // AliSignal q;    // In the example below a signal contains the
59 //                 // following data : timing, ADC and dE/dx
60 // q.SetName("TOF hit");
61 // q.SetPosition(pos,"car");
62 // q.SetPositionErrors(err,"car");
63 // signal=82.5; // e.g. signal time in ns
64 // error=2.01;
65 // offset=0.003;
66 // q.SetSignal(signal,1);
67 // q.SetSignalError(error,1);
68 // q.SetOffset(offset,1);
69 // signal=268.1; // e.g. ADC value of signal
70 // error=3.75;
71 // gain=120.78;
72 // q.SetSignal(signal,2);
73 // q.SetSignalError(error,2);
74 // q.SetGain(gain,2);
75 // signal=23.7; // e.g. corresponding dE/dx value
76 // error=0.48;
77 // offset=0.2;
78 // gain=150;
79 // q.SetSignal(signal,3);
80 // q.SetSignalError(error,3);
81 // q.SetOffset(offset,3);
82 // q.SetGain(gain,3);
83 //
84 //--- Author: Nick van Eijndhoven 23-jan-1999 UU-SAP Utrecht
85 //- Modified: NvE $Date$ UU-SAP Utrecht
86 ///////////////////////////////////////////////////////////////////////////
87
88 #include "AliSignal.h"
89 #include "Riostream.h"
90  
91 ClassImp(AliSignal) // Class implementation to enable ROOT I/O
92  
93 AliSignal::AliSignal() : TNamed(),AliPosition(),AliAttrib()
94 {
95 // Creation of an AliSignal object and initialisation of parameters.
96 // Several signal values (with errors) can be stored in different slots.
97 // If needed, the storage for values (and errors) will be expanded automatically
98 // when entering values and/or errors.
99  fSignals=0;
100  fDsignals=0;
101  fWaveforms=0;
102  SetName("Unspecified");
103  SetTitle("Unspecified");
104 }
105 ///////////////////////////////////////////////////////////////////////////
106 AliSignal::~AliSignal()
107 {
108 // Destructor to delete dynamically allocated memory
109  if (fSignals)
110  {
111   delete fSignals;
112   fSignals=0;
113  }
114  if (fDsignals)
115  {
116   delete fDsignals;
117   fDsignals=0;
118  }
119  if (fWaveforms)
120  {
121   delete fWaveforms;
122   fWaveforms=0;
123  }
124 }
125 ///////////////////////////////////////////////////////////////////////////
126 AliSignal::AliSignal(AliSignal& s) : TNamed(s),AliPosition(s),AliAttrib(s)
127 {
128 // Copy constructor
129  fSignals=0;
130  fDsignals=0;
131  fWaveforms=0;
132
133  Int_t n=s.GetNvalues();
134  Double_t val;
135  for (Int_t i=1; i<=n; i++)
136  {
137   val=s.GetSignal(i);
138   SetSignal(val,i);
139  } 
140
141  n=s.GetNerrors();
142  for (Int_t j=1; j<=n; j++)
143  {
144   val=s.GetSignalError(j);
145   SetSignalError(val,j);
146  }
147
148  n=s.GetNwaveforms();
149  for (Int_t k=1; k<=n; k++)
150  {
151   TH1F* hist=s.GetWaveform(k);
152   if (hist) SetWaveform(hist,k); 
153  }
154 }
155 ///////////////////////////////////////////////////////////////////////////
156 void AliSignal::Reset(Int_t mode)
157 {
158 // Reset all signal and position values and errors to 0.
159 //
160 // mode = 0 Reset position and all signal values and their errors to 0.
161 //          The waveform histograms are reset, but the calibration
162 //          constants (i.e. gains and offsets) are kept.
163 //        1 Reset position and delete the signal and error storage arrays.
164 //          Also the waveform histograms, gains and offset arrays are deleted.
165 //
166 // The default when invoking Reset() corresponds to mode=0.
167 //
168 // The usage of mode=0 allows to re-use the allocated memory for new
169 // signal (and error) values. This behaviour is preferable (i.e. faster)
170 // in case the various signals always contain the same number of values
171 // and have the same calibration constants.
172 // The usage of mode=1 is slower, but allows a more efficient memory
173 // occupation (and smaller output file size) in case the different
174 // signals have a variable number of values.
175 //
176 // For more specific actions see ResetPosition(), ResetSignals(),
177 // DeleteSignals(), ResetGain(), ResetOffset() and DeleteCalibrations().
178 //
179
180  if (mode<0 || mode>1)
181  {
182   cout << " *AliSignal::Reset* Invalid argument mode = " << mode << endl;
183   cout << " Default mode=0 will be used." << endl;
184   mode=0;
185  }
186
187  ResetPosition();
188  if (!mode)
189  {
190   ResetSignals();
191  }
192  else
193  {
194   DeleteSignals();
195   DeleteCalibrations();
196  }
197 }
198 ///////////////////////////////////////////////////////////////////////////
199 void AliSignal::ResetSignals(Int_t mode)
200 {
201 // Reset various signal data according to user selection.
202 //
203 // mode = 0 Reset all signal values and their errors to 0.
204 //        1 Reset only signal values
205 //        2 Reset only signal errors
206 //
207 // The default when invoking ResetSignals() corresponds to mode=0.
208 //
209 // Irrespective of the mode, the waveform histograms are reset.
210
211  if (mode<0 || mode>2)
212  {
213   cout << " *AliSignal::ResetSignals* Invalid argument mode = " << mode << endl;
214   cout << " Default mode=0 will be used." << endl;
215   mode=0;
216  }
217
218  if (fSignals && (mode==0 || mode==1))
219  {
220   for (Int_t i=0; i<fSignals->GetSize(); i++)
221   {
222    fSignals->AddAt(0,i);
223   }
224  }
225
226  if (fDsignals && (mode==0 || mode==2))
227  {
228   for (Int_t j=0; j<fDsignals->GetSize(); j++)
229   {
230    fDsignals->AddAt(0,j);
231   }
232  }
233
234  ResetWaveform(0);
235 }
236 ///////////////////////////////////////////////////////////////////////////
237 void AliSignal::DeleteSignals(Int_t mode)
238 {
239 // Delete storage arrays of various signal data according to user selection.
240 //
241 // mode = 0 Delete arrays of both signal values and their errors.
242 //        1 Delete only signal values array
243 //        2 Delete only signal errors array
244 //
245 // The default when invoking DeleteSignals() corresponds to mode=0.
246 //
247 // Irrespective of the mode, the waveform histograms are deleted.
248
249  if (mode<0 || mode>2)
250  {
251   cout << " *AliSignal::DeleteSignals* Invalid argument mode = " << mode << endl;
252   cout << " Default mode=0 will be used." << endl;
253   mode=0;
254  }
255
256  if (fSignals && (mode==0 || mode==1))
257  {
258   delete fSignals;
259   fSignals=0;
260  }
261
262  if (fDsignals && (mode==0 || mode==2))
263  {
264   delete fDsignals;
265   fDsignals=0;
266  }
267
268  DeleteWaveform(0);
269 }
270 ///////////////////////////////////////////////////////////////////////////
271 void AliSignal::SetSignal(Double_t sig,Int_t j)
272 {
273 // Store value in the j-th (default j=1) signal slot.
274 // Note : The first signal slot is at j=1.
275 // In case the value of the index j exceeds the maximum number of reserved
276 // slots for signal values, the number of reserved slots for the
277 // signal values is increased automatically.
278
279  if (!fSignals)
280  {
281   fSignals=new TArrayF(j);
282   ResetSignals(1);
283  }
284
285  Int_t size=fSignals->GetSize();
286
287  if (j>size)
288  {
289   fSignals->Set(j);
290  }
291
292  fSignals->AddAt(float(sig),j-1);
293 }
294 ///////////////////////////////////////////////////////////////////////////
295 void AliSignal::AddSignal(Double_t sig,Int_t j)
296 {
297 // Add value to the j-th (default j=1) signal slot.
298 // Note : The first signal slot is at j=1.
299 // In case the value of the index j exceeds the maximum number of reserved
300 // slots for signal values, the number of reserved slots for the
301 // signal values is increased automatically.
302
303  if (!fSignals)
304  {
305   fSignals=new TArrayF(j);
306   ResetSignals(1);
307  }
308
309  Int_t size=fSignals->GetSize();
310
311  if (j>size)
312  {
313   fSignals->Set(j);
314  }
315
316  Float_t sum=(fSignals->At(j-1))+sig;
317  fSignals->AddAt(sum,j-1);
318 }
319 ///////////////////////////////////////////////////////////////////////////
320 Float_t AliSignal::GetSignal(Int_t j,Int_t mode)
321 {
322 // Provide value of the j-th (default j=1) signal slot.
323 // Note : The first signal slot is at j=1.
324 // In case no signal is present or the argument j is invalid, 0 is returned.
325 // The parameter "mode" allows for automatic gain etc... correction of the signal.
326 //
327 // mode = 0 : Just the j-th signal is returned.
328 //        1 : The j-th signal is corrected for the gain, offset, dead flag etc...
329 //            In case the gain value was not set, gain=1 will be assumed.
330 //            In case the gain value was 0, a signal value of 0 is returned.
331 //            In case the offset value was not set, offset=0 will be assumed.
332 //            In case the j-th slot was marked dead, 0 is returned.
333 //
334 // The corrected signal (sigc) is determined as follows :
335 //
336 //              sigc=(signal/gain)-offset 
337 //
338 // The default is mode=0.
339
340  Float_t sig=0;
341  Float_t gain=1;
342  Float_t offset=0;
343  if (fSignals)
344  {
345   if (j>0 && j<=(fSignals->GetSize()))
346   {
347    sig=fSignals->At(j-1);
348
349    if (mode==0) return sig;
350
351    // Correct the signal for the gain, offset, dead flag etc...
352    if (GetDeadValue(j)) return 0;
353
354    if (GetGainFlag(j)) gain=GetGain(j);
355    if (GetOffsetFlag(j)) offset=GetOffset(j);
356
357    if (fabs(gain)>0.)
358    {
359     sig=(sig/gain)-offset;
360    }
361    else
362    {
363     sig=0;
364    }
365   }
366   else
367   {
368    cout << " *AliSignal::GetSignal* Index j = " << j << " invalid." << endl;
369   } 
370  }
371  return sig;
372 }
373 ///////////////////////////////////////////////////////////////////////////
374 void AliSignal::SetSignalError(Double_t dsig,Int_t j)
375 {
376 // Store error for the j-th (default j=1) signal slot.
377 // Note : The first signal slot is at j=1.
378 // In case the value of the index j exceeds the maximum number of reserved
379 // slots for signal error values, the number of reserved slots for the
380 // signal errors is increased automatically.
381
382  if (!fDsignals)
383  {
384   fDsignals=new TArrayF(j);
385   ResetSignals(2);
386  }
387
388  Int_t size=fDsignals->GetSize();
389
390  if (j>size)
391  {
392   fDsignals->Set(j);
393  }
394
395  fDsignals->AddAt(float(dsig),j-1);
396 }
397 ///////////////////////////////////////////////////////////////////////////
398 Float_t AliSignal::GetSignalError(Int_t j)
399 {
400 // Provide error of the j-th (default j=1) signal slot.
401 // Note : The first signal slot is at j=1.
402 // In case no signal is present or the argument j is invalid, 0 is returned.
403  Float_t err=0;
404  if (fDsignals)
405  {
406   if (j>0 && j<=(fDsignals->GetSize()))
407   {
408    err=fDsignals->At(j-1);
409   }
410   else
411   {
412    cout << " *AliSignal::GetSignalError* Index j = " << j << " invalid." << endl;
413   } 
414  }
415  return err;
416 }
417 ///////////////////////////////////////////////////////////////////////////
418 void AliSignal::Data(TString f)
419 {
420 // Provide all signal information within the coordinate frame f.
421
422  cout << " *" << ClassName() << "::Data* Name : " << GetName()
423       << " Title : " << GetTitle() << endl;
424  cout << " Position";
425  Ali3Vector::Data(f);
426
427  List(-1);
428
429 ///////////////////////////////////////////////////////////////////////////
430 void AliSignal::List(Int_t j)
431 {
432 // Provide signal information for the j-th slot.
433 // The first slot is at j=1.
434 // In case j=0 (default) the data of all slots will be listed.
435 // In case j=-1 the data of all slots will be listed, but the header
436 // information will be suppressed.
437
438  if (j<-1) 
439  {
440   cout << " *AliSignal::List* Invalid argument j = " << j << endl;
441   return;
442  }
443
444  if (j != -1) cout << " *" << ClassName() << "::List* Name : " << GetName()
445                    << " Title : " << GetTitle() << endl;
446
447  Int_t nvalues=GetNvalues();
448  Int_t nerrors=GetNerrors();
449  Int_t nwforms=GetNwaveforms();
450  Int_t n=nvalues;
451  if (nerrors>n) n=nerrors;
452  if (nwforms>n) n=nwforms;
453
454  if (j<=0)
455  {
456   for (Int_t i=1; i<=n; i++)
457   {
458    cout << "   Signal";
459    if (i<=nvalues) cout << " value : " << GetSignal(i);
460    if (i<=nerrors) cout << " error : " << GetSignalError(i);
461    AliAttrib::List(i);
462    cout << endl;
463    if (GetWaveform(i)) cout << "    Waveform : " << GetWaveform(i)->ClassName()
464                             << " " << GetWaveform(i)->GetTitle() << endl;
465   }
466  }
467  else
468  {
469   if (j<=n)
470   {
471    cout << "   Signal";
472    if (j<=nvalues) cout << " value : " << GetSignal(j);
473    if (j<=nerrors) cout << " error : " << GetSignalError(j);
474    AliAttrib::List(j);
475    cout << endl;
476    if (GetWaveform(j)) cout << "    Waveform : " << GetWaveform(j)->ClassName()
477                             << " " << GetWaveform(j)->GetTitle() << endl;
478   }
479  }
480
481 ///////////////////////////////////////////////////////////////////////////
482 Int_t AliSignal::GetNvalues()
483 {
484 // Provide the number of values for this signal.
485  Int_t n=0;
486  if (fSignals) n=fSignals->GetSize();
487  return n;
488 }
489 ///////////////////////////////////////////////////////////////////////////
490 Int_t AliSignal::GetNerrors()
491 {
492 // Provide the number specified errors on the values for this signal.
493  Int_t n=0;
494  if (fDsignals) n=fDsignals->GetSize();
495  return n;
496 }
497 ///////////////////////////////////////////////////////////////////////////
498 Int_t AliSignal::GetNwaveforms()
499 {
500 // Provide the number specified waveforms for this signal.
501  Int_t n=0;
502  if (fWaveforms) n=fWaveforms->GetSize();
503  return n;
504 }
505 ///////////////////////////////////////////////////////////////////////////
506 TH1F* AliSignal::GetWaveform(Int_t j)
507 {
508 // Provide pointer to the j-th waveform histogram.
509  TH1F* waveform=0;
510  if (j <= GetNwaveforms()) waveform=(TH1F*)fWaveforms->At(j-1);
511  return waveform;
512 }
513 ///////////////////////////////////////////////////////////////////////////
514 void AliSignal::SetWaveform(TH1F* waveform,Int_t j)
515 {
516 // Set the 1D waveform histogram corresponding to the j-th signal value.
517 //
518 // Notes :
519 //  The waveform of the first signal value is at j=1.
520 //  j=1 is the default value.
521 //
522 // In case the value of the index j exceeds the maximum number of reserved
523 // slots for the waveforms, the number of reserved slots for the waveforms
524 // is increased automatically.
525 //
526 // In case the histo pointer argument has the same value as the current waveform
527 // histogram pointer value, no action is taken since the user has already
528 // modified the actual histogram.
529 //
530 // In case the histo pointer argument is zero, the current waveform histogram
531 // is deleted and the pointer set to zero.
532 //
533 // In all other cases the current waveform histogram is deleted and a new
534 // copy of the input histogram is created which becomes the current waveform
535 // histogram.
536
537  if (!fWaveforms)
538  {
539   fWaveforms=new TObjArray(j);
540   fWaveforms->SetOwner();
541  }
542
543  if (j > fWaveforms->GetSize()) fWaveforms->Expand(j);
544
545  TH1F* hcur=(TH1F*)fWaveforms->At(j-1);
546  if (waveform != hcur)
547  {
548   if (hcur)
549   {
550    fWaveforms->Remove(hcur);
551    delete hcur;
552    hcur=0;
553   }
554   if (waveform)
555   {
556    hcur=new TH1F(*waveform);
557    fWaveforms->AddAt(hcur,j-1);
558   }
559  } 
560 }
561 ///////////////////////////////////////////////////////////////////////////
562 void AliSignal::ResetWaveform(Int_t j)
563 {
564 // Reset the waveform of the j-th (default j=1) signal value.
565 // This memberfunction invokes TH1F::Reset() for the corresponding waveform(s).
566 // To actually delete the histograms from memory, use DeleteWaveform().
567 // Notes : The first signal value is at j=1.
568 //         j=0 ==> All waveforms will be reset.
569  
570  if (!fWaveforms) return;
571
572  Int_t size=fWaveforms->GetSize();
573
574  if ((j>=0) && (j<=size))
575  {
576   if (j)
577   {
578    TH1F* hwave=(TH1F*)fWaveforms->At(j-1);
579    if (hwave) hwave->Reset();
580   }
581   else
582   {
583    for (Int_t i=0; i<size; i++)
584    {
585     TH1F* hwave=(TH1F*)fWaveforms->At(i);
586     if (hwave) hwave->Reset();
587    }
588   }
589  }
590  else
591  {
592   cout << " *AliSignal::ResetWaveform* Index j = " << j << " invalid." << endl;
593   return;
594  }
595 }
596 ///////////////////////////////////////////////////////////////////////////
597 void AliSignal::DeleteWaveform(Int_t j)
598 {
599 // Delete the waveform of the j-th (default j=1) signal value.
600 // Notes : The first signal value is at j=1.
601 //         j=0 ==> All waveforms will be deleted.
602  
603  if (!fWaveforms) return;
604
605  Int_t size=fWaveforms->GetSize();
606
607  if ((j>=0) && (j<=size))
608  {
609   if (j)
610   {
611    TH1F* hwave=(TH1F*)fWaveforms->At(j-1);
612    if (hwave)
613    {
614     fWaveforms->Remove(hwave);
615     delete hwave;
616    }
617   }
618   else
619   {
620    delete fWaveforms;
621    fWaveforms=0;
622   }
623  }
624  else
625  {
626   cout << " *AliSignal::DeleteWaveform* Index j = " << j << " invalid." << endl;
627   return;
628  }
629 }
630 ///////////////////////////////////////////////////////////////////////////
631 TObject* AliSignal::Clone(char* name)
632 {
633 // Make a deep copy of the current object and provide the pointer to the copy.
634 // This memberfunction enables automatic creation of new objects of the
635 // correct type depending on the object type, a feature which may be very useful
636 // for containers when adding objects in case the container owns the objects.
637 // This feature allows e.g. AliTrack to store either AliSignal objects or
638 // objects derived from AliSignal via the AddSignal memberfunction, provided
639 // these derived classes also have a proper Clone memberfunction. 
640
641  AliSignal* sig=new AliSignal(*this);
642  if (name)
643  {
644   if (strlen(name)) sig->SetName(name);
645  }
646  return sig;
647 }
648 ///////////////////////////////////////////////////////////////////////////