add protection against truncated events + coverity - Rachid
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALRawDigit.cxx
CommitLineData
916f1e76 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 **************************************************************************/
de39a0ff 15/*
16
17
18
19
20 Author: R. GUERNANE LPSC Grenoble CNRS/IN2P3
21*/
916f1e76 22
23// --- ROOT system ---
24#include <Riostream.h>
25#include <TMath.h>
26
916f1e76 27// --- AliRoot header files ---
916f1e76 28#include "AliEMCALRawDigit.h"
de39a0ff 29#include "AliLog.h"
916f1e76 30
31ClassImp(AliEMCALRawDigit)
32
33//____________________________________________________________________________
34AliEMCALRawDigit::AliEMCALRawDigit() : TObject(),
35fId(-1),
36fNSamples(0),
de39a0ff 37fSamples(0x0),
38fAmplitude(0),
39fTime(0)
916f1e76 40{
41 // default ctor
42}
43
44//____________________________________________________________________________
45AliEMCALRawDigit::AliEMCALRawDigit(Int_t id, Int_t timeSamples[], Int_t nSamples) : TObject(),
46fId(id),
47fNSamples(nSamples),
de39a0ff 48fSamples(0x0),
49fAmplitude(0),
50fTime(0)
916f1e76 51{
52 //
de39a0ff 53 fSamples = new Int_t[fNSamples];
54 for (Int_t i = 0; i < fNSamples; i++) fSamples[i] = timeSamples[i];
916f1e76 55}
56
57//____________________________________________________________________________
58AliEMCALRawDigit::~AliEMCALRawDigit()
59{
de39a0ff 60 //dtor, delete array of time samples
b57cc9b9 61 if(fSamples) delete [] fSamples;
916f1e76 62}
63
64//____________________________________________________________________________
e0dc3f7d 65void AliEMCALRawDigit::Clear(Option_t *)
66{
de39a0ff 67 // clear, delete array of time samples
b57cc9b9 68 if(fSamples) delete [] fSamples;
e0dc3f7d 69}
70
71
72//____________________________________________________________________________
916f1e76 73Bool_t AliEMCALRawDigit::GetTimeSample(const Int_t iSample, Int_t& timeBin, Int_t& amp) const
74{
de39a0ff 75 //
916f1e76 76 if (iSample > fNSamples || iSample < 0) return kFALSE;
77
78 amp = fSamples[iSample] & 0xFFF;
79 timeBin = (fSamples[iSample] >> 12) & 0xFF;
80
81 return kTRUE;
82}
83
84//____________________________________________________________________________
de39a0ff 85void AliEMCALRawDigit::SetTimeSamples(const Int_t timeSamples[], const Int_t nSamples)
916f1e76 86{
de39a0ff 87 //
88 if (fSamples)
916f1e76 89 {
aa878a7b 90 AliDebug(1,"Samples already filled: delete first!");
de39a0ff 91 fNSamples = 0;
92 delete [] fSamples;
916f1e76 93 }
94
de39a0ff 95 fNSamples = nSamples;
96 fSamples = new Int_t[fNSamples];
97 for (Int_t i = 0; i < fNSamples; i++) fSamples[i] = timeSamples[i];
916f1e76 98}
99
100//____________________________________________________________________________
de39a0ff 101Bool_t AliEMCALRawDigit::GetMaximum(Int_t& amplitude, Int_t& time) const
102{
103 //
104 if (!fNSamples)
105 {
aa878a7b 106 AliDebug(1,"Digit has no time sample");
de39a0ff 107 return kFALSE;
108 }
109
110 amplitude = 0;
111 for (Int_t i = 0; i < fNSamples; i++)
112 {
113 Int_t t, a;
114 if (GetTimeSample(i, t, a))
115 {
116 if (a > amplitude)
117 {
118 amplitude = a;
119 time = t;
120 }
121 }
122 }
123
124 return kTRUE;
125}
126
127//____________________________________________________________________________
128Int_t AliEMCALRawDigit::Compare(const TObject * obj) const
916f1e76 129{
130 // Compares two digits with respect to its Id
131 // to sort according increasing Id
de39a0ff 132
133 Int_t rv=0;
134
135 AliEMCALRawDigit* digit = (AliEMCALRawDigit*)obj;
136
137 Int_t iddiff = fId - digit->GetId();
138
139 if (iddiff > 0)
140 rv = 1;
141 else if (iddiff < 0)
916f1e76 142 rv = -1;
143 else
de39a0ff 144 rv = 0;
145
916f1e76 146 return rv;
147}
148
149//____________________________________________________________________________
de39a0ff 150void AliEMCALRawDigit::Print(const Option_t* /*opt*/) const
916f1e76 151{
de39a0ff 152 //
153 printf("===\n| Digit id: %4d / %d Time Samples: \n",fId,fNSamples);
154 for (Int_t i=0; i < fNSamples; i++)
155 {
156 Int_t timeBin=-1, amp=0;
157 GetTimeSample(i, timeBin, amp);
158 printf("| (%d,%d) ",timeBin,amp);
159 }
160
161 printf("\n");
916f1e76 162}