Coding convetion violations fixed.
[u/mrichter/AliRoot.git] / VZERO / AliVZEROTriggerSimulator.cxx
1 /**************************************************************************\r
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *\r
3  *                                                                        *\r
4  * Author: The ALICE Off-line Project.                                    *\r
5  * Contributors are mentioned in the code where appropriate.              *\r
6  *                                                                        *\r
7  * Permission to use, copy, modify and distribute this software and its   *\r
8  * documentation strictly for non-commercial purposes is hereby granted   *\r
9  * without fee, provided that the above copyright notice appears in all   *\r
10  * copies and that both the copyright notice and this permission notice   *\r
11  * appear in the supporting documentation. The authors make no claims     *\r
12  * about the suitability of this software for any purpose. It is          *\r
13  * provided "as is" without express or implied warranty.                  *\r
14  **************************************************************************/\r
15 // \r
16 // Class AliVZEROTriggerSimulator\r
17 // ------------------------------\r
18 //  Simulate the VZERO Trigger response\r
19 // Use FEE parameters stored in Database\r
20 // Can work on real data or in simulation\r
21 //\r
22 \r
23 #include <TTree.h>\r
24 #include <TClonesArray.h>\r
25 #include <TParameter.h>\r
26 \r
27 #include "AliLog.h"\r
28 #include "AliCDBManager.h"\r
29 #include "AliCDBEntry.h"\r
30 #include "AliCDBStorage.h"\r
31 #include "AliCDBId.h"\r
32 #include "AliVZEROTriggerData.h"\r
33 #include "AliVZEROLogicalSignal.h"\r
34 #include "AliVZEROTriggerSimulator.h"\r
35 #include "AliVZEROdigit.h"\r
36 #include "AliVZEROCalibData.h"\r
37 #include "AliVZEROConst.h"\r
38 \r
39 ClassImp(AliVZEROTriggerSimulator)\r
40 \r
41 //_____________________________________________________________________________\r
42 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(TTree * digitsTree, TClonesArray* digits) : \r
43 TObject(),fTriggerData(NULL),fDigitsTree(digitsTree),fDigits(digits),fTriggerWord(0)\r
44 {\r
45         // constructor\r
46         fTriggerData = LoadTriggerData();\r
47         \r
48         for(int i=0;i<64;i++) {\r
49                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
50                 fCharges[i] = 0.;\r
51         }\r
52         GenerateBBWindows();\r
53         GenerateBGWindows();\r
54         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
55                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
56                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
57                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
58                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
59         }\r
60 }\r
61 //_____________________________________________________________________________\r
62 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator() : \r
63 TObject(),fTriggerData(NULL),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
64 {\r
65         // Default constructor\r
66         fTriggerData = LoadTriggerData();\r
67 \r
68         for(int i=0;i<64;i++) {\r
69                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
70                 fCharges[i] = 0;\r
71         }\r
72         GenerateBBWindows();\r
73         GenerateBGWindows();\r
74         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
75                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
76                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
77                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
78                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
79         }\r
80 }\r
81 \r
82 //_____________________________________________________________________________\r
83 AliVZEROTriggerSimulator::~AliVZEROTriggerSimulator(){\r
84 // Destructor\r
85   for (Int_t i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
86     delete fBBGate[i];\r
87     delete fBGGate[i];\r
88     delete fBBLatch[i];\r
89     delete fBBReset[i];\r
90     delete fBGLatch[i];\r
91     delete fBGReset[i];\r
92   }\r
93 }\r
94 \r
95 //_____________________________________________________________________________\r
96 void AliVZEROTriggerSimulator::GenerateBBWindows() \r
97 {\r
98         // Generates the BB observation window\r
99         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
100                 AliVZEROLogicalSignal clk1BB(fTriggerData->GetClk1Win1(i),fTriggerData->GetDelayClk1Win1(i));\r
101                 AliVZEROLogicalSignal clk2BB(fTriggerData->GetClk2Win1(i),fTriggerData->GetDelayClk2Win1(i));\r
102                 fBBGate[i] = new AliVZEROLogicalSignal(clk1BB & clk2BB);\r
103         }\r
104 }\r
105 //_____________________________________________________________________________\r
106 void AliVZEROTriggerSimulator::GenerateBGWindows() \r
107 {\r
108         // Generates the BG observation window\r
109         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
110                 AliVZEROLogicalSignal clk1BG(fTriggerData->GetClk1Win2(i),fTriggerData->GetDelayClk1Win2(i));\r
111                 AliVZEROLogicalSignal clk2BG(fTriggerData->GetClk2Win2(i),fTriggerData->GetDelayClk2Win2(i));\r
112                 fBGGate[i] = new AliVZEROLogicalSignal(clk1BG & clk2BG);\r
113         }\r
114 }\r
115 \r
116 //_____________________________________________________________________________\r
117 AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const \r
118 {\r
119         // Gets Trigger object for VZERO set\r
120         AliDebug(1,"Loading Trigger parameters");\r
121         AliCDBManager *man = AliCDBManager::Instance();\r
122         \r
123         \r
124         AliCDBEntry *entry=0;\r
125         \r
126         entry = man->Get("VZERO/Trigger/Data");\r
127         if(!entry){\r
128                 AliWarning("Load of calibration data from default storage failed!");\r
129                 AliWarning("Calibration data will be loaded from local storage ($ALICE_ROOT)");\r
130                 \r
131                 man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");\r
132                 entry = man->Get("VZERO/Trigger/Data",0);\r
133         }\r
134         \r
135         // Retrieval of data in directory VZERO/Calib/Trigger:\r
136         \r
137         AliVZEROTriggerData *triggerData = NULL;\r
138         \r
139         if (entry) triggerData = (AliVZEROTriggerData*) entry->GetObject();\r
140         if (!triggerData)  AliError("No Trigger data from database !");\r
141         \r
142         return triggerData;\r
143 }\r
144 \r
145 //_____________________________________________________________________________\r
146 void AliVZEROTriggerSimulator::Run() {\r
147         //AliInfo("Generating VZERO Triggers");\r
148         \r
149         // Loop over VZERO entries\r
150         Int_t nEntries = (Int_t)fDigitsTree->GetEntries();\r
151         for (Int_t ievt=0; ievt<nEntries; ievt++) {\r
152                 fDigitsTree->GetEvent(ievt);\r
153                 \r
154                 Int_t nDigits = fDigits->GetEntriesFast();\r
155                 \r
156                 for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {\r
157                         AliVZEROdigit* digit = (AliVZEROdigit*)fDigits->At(iDigit);\r
158                         \r
159                         Int_t integrator = digit->Integrator();\r
160                         Int_t pmNumber   = digit->PMNumber();\r
161                         Int_t board   = AliVZEROCalibData::GetBoardNumber(pmNumber);\r
162                         Int_t channel = AliVZEROCalibData::GetFEEChannelNumber(pmNumber);\r
163                         if (board < 0 || channel < 0) continue;\r
164                         \r
165                         if(fTriggerData->GetEnableCharge(board,channel)) {\r
166                                 fCharges[pmNumber] = digit->ChargeADC(AliVZEROdigit::kNClocks/2);\r
167                                 if(fTriggerData->GetPedestalSubtraction(board)) {\r
168                                         if(fCharges[pmNumber]>=(Float_t) fTriggerData->GetPedestalCut(integrator,board,channel)){ \r
169                                                 fCharges[pmNumber] -= (Float_t) fTriggerData->GetPedestal(integrator,board,channel);\r
170                                         } else {\r
171                                                 fCharges[pmNumber] = 0.;\r
172                                         }\r
173                                 }\r
174                         } else {\r
175                                 fCharges[pmNumber] = 0.;\r
176                         }\r
177                         \r
178                         Float_t time = digit->Time();\r
179                         time -= kClockOffset;\r
180 \r
181                         AliDebug(10,Form(" Digit: %f %d %d %d %d %d %d %d %d",digit->Time(),\r
182                                          digit->ChargeADC(8),digit->ChargeADC(9),digit->ChargeADC(10),\r
183                                          digit->ChargeADC(11),digit->ChargeADC(12),digit->ChargeADC(13),\r
184                                          digit->ChargeADC(14),digit->ChargeADC(15)));\r
185                         AliDebug(10,Form(" PM nb : %d ; TDC= %f(%f)  Enable Time %d charge %d inCoin %d charge %f",\r
186                                          pmNumber,time,digit->Time(),\r
187                                          fTriggerData->GetEnableTiming(board,channel),fTriggerData->GetEnableCharge(board,channel),\r
188                                          fBBGate[board]->IsInCoincidence(time),fCharges[pmNumber]));\r
189                         fBBFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBBGate[board]->IsInCoincidence(time);\r
190                         fBGFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBGGate[board]->IsInCoincidence(time);\r
191                         \r
192                 } // end of loop over digits\r
193         } // end of loop over events in digits tree\r
194         \r
195         Int_t nBBflagsV0A = 0;\r
196         Int_t nBBflagsV0C = 0;\r
197         Int_t nBGflagsV0A = 0;\r
198         Int_t nBGflagsV0C = 0;\r
199         Float_t chargeV0A   = 0.;\r
200         Float_t chargeV0C   = 0.;\r
201         Int_t aBBflagsV0A = 0;\r
202         Int_t aBBflagsV0C = 0;\r
203         Int_t aBGflagsV0A = 0;\r
204         Int_t aBGflagsV0C = 0;\r
205 \r
206         for(int i=0;i<64;i++) {\r
207                 if(i<32) {\r
208                         nBBflagsV0C += fBBFlags[i]; \r
209                         nBGflagsV0C += fBGFlags[i];\r
210                         chargeV0C += fCharges[i];\r
211                         if (fBBFlags[i]) aBBflagsV0C |= (1 << i);\r
212                         if (fBGFlags[i]) aBGflagsV0C |= (1 << i);\r
213                 } else {\r
214                         nBBflagsV0A += fBBFlags[i]; \r
215                         nBGflagsV0A += fBGFlags[i];\r
216                         chargeV0A += fCharges[i];\r
217                         if (fBBFlags[i]) aBBflagsV0A |= (1 << (i-32));\r
218                         if (fBGFlags[i]) aBGflagsV0A |= (1 << (i-32));\r
219                 }\r
220                 //AliInfo(Form("Ch %d BB=%d BG=%d",i,fBBFlags[i],fBGFlags[i] )); \r
221         }\r
222 \r
223         // Store the BB and BG flags in the digits tree (user info)\r
224         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0A",aBBflagsV0A));\r
225         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0C",aBBflagsV0C));\r
226         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0A",aBGflagsV0A));\r
227         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0C",aBGflagsV0C));\r
228         \r
229         // BBA\r
230         if(nBBflagsV0A>=fTriggerData->GetBBAThreshold())  SetBBA();\r
231         \r
232         // BBC\r
233         if(nBBflagsV0C>=fTriggerData->GetBBCThreshold())  SetBBC();\r
234 \r
235         // BBA_AND_BBC\r
236         if(GetBBA() && GetBBC())  SetBBAandBBC();\r
237         \r
238         // BBA_OR_BBC\r
239         if(GetBBA() || GetBBC()) SetBBAorBBC();\r
240 \r
241         // BGA\r
242         if(nBGflagsV0A>=fTriggerData->GetBGAThreshold()) SetBGA();\r
243 \r
244         // BGC\r
245         if(nBGflagsV0C>=fTriggerData->GetBGCThreshold()) SetBGC();\r
246         \r
247         // BGA_AND_BBC (Beam Gas from RB24 side)\r
248         if(nBBflagsV0C>=fTriggerData->GetBBCForBGThreshold() && GetBGA()) SetBGAandBBC();\r
249         \r
250         // BGC_AND_BBA (Beam Gas from RB26 side)\r
251         if(nBBflagsV0A>=fTriggerData->GetBBAForBGThreshold() && GetBGC()) SetBGCandBBA();\r
252 \r
253         // CTA1_AND_CTC1 (Centrality trigger 1)\r
254         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() && chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1andCTC1();\r
255 \r
256         // CTA1_OR_CTC1 (Centrality trigger 1)\r
257         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() || chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1orCTC1();\r
258         \r
259         // CTA2_AND_CTC2 (Centrality trigger 2)\r
260         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() && chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2andCTC2();\r
261         \r
262         // CTA2_OR_CTC2 (Centrality trigger 2)\r
263         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() || chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2orCTC2();\r
264         \r
265         // MTA_AND_MTC (Multiplicity Trigger)\r
266         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
267            && (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
268                 SetMTAandMTC();\r
269         \r
270         // MTA_OR_MTC (Multiplicity Trigger)\r
271         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
272            || (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
273                 SetMTAorMTC();\r
274         \r
275         // BGA_OR_BGC\r
276         if(GetBGA() || GetBGC()) SetBGAorBGC();\r
277         \r
278         // (BGA and BBC) or (BGC and BBA) (Beam Gas from one of the two sides)\r
279         if(GetBGAandBBC() || GetBGCandBBA()) SetBeamGas();\r
280 \r
281 //      AliInfo(Form("BB Flags : V0A = %d  V0C = %d ",nBBflagsV0A, nBBflagsV0C )); \r
282 //      AliInfo(Form("BG Flags : V0A = %d  V0C = %d ",nBGflagsV0A, nBGflagsV0C )); \r
283 //      AliInfo(Form("Charges  : V0A = %d  V0C = %d ",chargeV0A, chargeV0C )); \r
284         \r
285 }\r
286 \r
287 \r
288 \r