Protection against wrong trigger bc mask pattern
[u/mrichter/AliRoot.git] / STEER / AliTriggerBCMask.cxx
CommitLineData
202e63df 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///////////////////////////////////////////////////////////////////////////////
17//
18// This class which defines the trigger bunch-crossing mask
19//
20//
21///////////////////////////////////////////////////////////////////////////////
51f6d619 22#include <Riostream.h>
710035a8 23#include <TObjArray.h>
24#include <TObjString.h>
202e63df 25
26#include "AliTriggerBCMask.h"
710035a8 27#include "AliLog.h"
202e63df 28
29ClassImp(AliTriggerBCMask)
30
31//_____________________________________________________________________________
32AliTriggerBCMask::AliTriggerBCMask():
33 TNamed()
34{
35 // Default constructor
36 for (Int_t i = 0; i < kNBytesPerBCMask; i++) fBCMask[i] = 0;
37}
38
39//_____________________________________________________________________________
51f6d619 40AliTriggerBCMask::AliTriggerBCMask( TString & name ):
202e63df 41 TNamed( name, name )
42{
43 // Constructor
51f6d619 44 for (Int_t i = 0; i < kNBytesPerBCMask; i++) fBCMask[i] = 255;
45}
46
47//_____________________________________________________________________________
48AliTriggerBCMask::AliTriggerBCMask( TString & name, TString & mask ):
49 TNamed( name, mask )
50{
51 // Constructor
52 CreateMask(mask);
202e63df 53}
54//_____________________________________________________________________________
55AliTriggerBCMask::~AliTriggerBCMask()
56{
57 // Destructor
58}
59//_____________________________________________________________________________
60AliTriggerBCMask::AliTriggerBCMask( const AliTriggerBCMask& mask ):
51f6d619 61 TNamed( mask )
202e63df 62{
63 // Copy constructor
64 for (Int_t i = 0; i < kNBytesPerBCMask; i++) fBCMask[i] = mask.fBCMask[i];
65}
66
67//______________________________________________________________________________
68AliTriggerBCMask& AliTriggerBCMask::operator=(const AliTriggerBCMask& mask)
69{
70 // AliTriggerBCMask assignment operator.
71
72 if (this != &mask) {
73 TNamed::operator=(mask);
74 for (Int_t i = 0; i < kNBytesPerBCMask; i++) fBCMask[i] = mask.fBCMask[i];
75 }
76 return *this;
77}
78
79//_____________________________________________________________________________
51f6d619 80Bool_t AliTriggerBCMask::GetMask( UShort_t index) const
202e63df 81{
82 // Return true or false whenever the mask is active
83 // for the bunch-crossing # = index
84 UShort_t position = index/8;
85 if (position >= kNBytesPerBCMask) return kFALSE;
86 UChar_t offset = index%8;
87 return (fBCMask[position] & (0x1 << offset));
88}
51f6d619 89
90//_____________________________________________________________________________
710035a8 91void AliTriggerBCMask::Print( const Option_t* opt) const
51f6d619 92{
93 // Print
94 cout << "Trigger bunch-crossing mask:" << endl;
95 cout << " Name: " << GetName() << endl;
96 cout << " Mask: " << GetTitle() << endl;
710035a8 97
98 if (strcmp(opt,"bits") == 0) {
99 cout << " Bits: " << endl;
100 for (UShort_t i = 0; i < kNBits; i++) {
101 if (GetMask(i)) {
102 cout << "1";
103 }
104 else {
105 cout << "0";
106 }
107 }
108 cout << endl;
109 }
110}
111
112//_____________________________________________________________________________
113Bool_t AliTriggerBCMask::CreateMask(TString &mask)
114{
115 // (re)creates the bc mask bit pattern
116 // according to the bc string.
117 // The string has the following syntax:
118 // "25L 25(2H2LH 3(23HL))"
119 // - H/h -> 1 L/l -> 0
120 // - spaces, new lines are white characters
121 // The method returns kTRUE in case of successful
122 // parsing and kFALSE otherwise.
123
124 for (Int_t i = 0; i < kNBytesPerBCMask; i++) fBCMask[i] = 255;
125
126 mask.ReplaceAll("("," ( ");
127 mask.ReplaceAll(")"," ) ");
128 mask.ReplaceAll("H"," H ");
129 mask.ReplaceAll("h"," H ");
130 mask.ReplaceAll("L"," L ");
131 mask.ReplaceAll("l"," L ");
132 TObjArray *tokens = mask.Tokenize(" \t");
133 if (tokens->GetEntriesFast() == 0) {
134 delete tokens;
135 return kTRUE;
136 }
137
138 TBits bits(kNBits);
139 Int_t index = 0, ibit = 0, level = 0;
140 if ((!Bcm2Bits(tokens,index,bits,ibit,level)) ||
141 (index != tokens->GetEntriesFast())) {
142 AliError("Invalid bunch-crossing mask syntax. Empty mask produced.");
143 delete tokens;
144 return kFALSE;
145 }
146
147 delete tokens;
148
149 if (ibit != kNBits) {
b7fd161d 150 AliError(Form("Incomplete bunch-crossing mask. Only the first %d bits are filled.",ibit));
151 return kFALSE;
710035a8 152 }
153
154 bits.Get(fBCMask);
155
156 return kTRUE;
157}
158
159Bool_t AliTriggerBCMask::Bcm2Bits(TObjArray *tokens, Int_t &index, TBits &bits, Int_t &ibit, Int_t &level) const
160{
161
162 level++;
163 Int_t repetion = 1;
164
165 while(1) {
166 if (index == tokens->GetEntriesFast()) {
167 if (level > 1) {
168 AliError("Missing )");
169 return kFALSE;
170 }
171 break;
172 }
173 TString st = ((TObjString*)tokens->At(index))->String();
174 if (st.CompareTo("H") == 0) {
175 for (Int_t i = 0; i < repetion; i++) bits.SetBitNumber(ibit++,kTRUE);
176 repetion = 1;
177 index++;
178 }
179 else if (st.CompareTo("L") == 0) {
180 for (Int_t i = 0; i < repetion; i++) bits.SetBitNumber(ibit++,kFALSE);
181 repetion = 1;
182 index++;
183 }
184 else if (st.IsDigit()) {
185 repetion = st.Atoi();
186 index++;
187 }
188 else if (st.CompareTo("(") == 0) {
189 index++;
190 Int_t ibit1 = ibit;
191 if (!Bcm2Bits(tokens,index,bits,ibit,level)) {
192 return kFALSE;
193 }
194 Int_t ibit2 = ibit;
195 for (Int_t i = 0; i < (repetion-1); i++) {
196 for (Int_t j = ibit1; j < ibit2; j++) {
197 bits.SetBitNumber(ibit++,bits.TestBitNumber(j));
198 }
199 }
200 repetion = 1;
201 }
202 else if (st.CompareTo(")") == 0) {
203 index++;
204 if (level <= 1) {
205 AliError("Incorrectly placed )");
206 return kFALSE;
207 }
208 break;
209 }
210 else {
211 AliError(Form("Incorrect BC mask field: %s",st.Data()));
212 return kFALSE;
213 }
214 }
215 level--;
216 return kTRUE;
217
51f6d619 218}
710035a8 219