Macro to calculate the needed HV values as a function of the required MIP signal.
[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 #include "AliCTPTimeParams.h"\r
39 \r
40 ClassImp(AliVZEROTriggerSimulator)\r
41 \r
42 //_____________________________________________________________________________\r
43 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(TTree * digitsTree, TClonesArray* digits) : \r
44 TObject(),fTriggerData(NULL),fDigitsTree(digitsTree),fDigits(digits),fTriggerWord(0)\r
45 {\r
46         // constructor\r
47         fTriggerData = LoadTriggerData();\r
48         LoadClockOffset();\r
49         \r
50         for(int i=0;i<64;i++) {\r
51                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
52                 fCharges[i] = 0.;\r
53         }\r
54         GenerateBBWindows();\r
55         GenerateBGWindows();\r
56         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
57                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
58                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
59                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
60                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
61         }\r
62 }\r
63 //_____________________________________________________________________________\r
64 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator() : \r
65 TObject(),fTriggerData(NULL),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
66 {\r
67         // Default constructor\r
68         fTriggerData = LoadTriggerData();\r
69         LoadClockOffset();\r
70 \r
71         for(int i=0;i<64;i++) {\r
72                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
73                 fCharges[i] = 0;\r
74         }\r
75         GenerateBBWindows();\r
76         GenerateBGWindows();\r
77         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
78                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
79                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
80                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
81                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
82         }\r
83 }\r
84 \r
85 //_____________________________________________________________________________\r
86 AliVZEROTriggerSimulator::~AliVZEROTriggerSimulator(){\r
87 // Destructor\r
88   for (Int_t i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
89     delete fBBGate[i];\r
90     delete fBGGate[i];\r
91     delete fBBLatch[i];\r
92     delete fBBReset[i];\r
93     delete fBGLatch[i];\r
94     delete fBGReset[i];\r
95   }\r
96 }\r
97 \r
98 //_____________________________________________________________________________\r
99 void AliVZEROTriggerSimulator::GenerateBBWindows() \r
100 {\r
101         // Generates the BB observation window\r
102         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
103                 AliVZEROLogicalSignal clk1BB(fTriggerData->GetClk1Win1(i),fTriggerData->GetDelayClk1Win1(i));\r
104                 AliVZEROLogicalSignal clk2BB(fTriggerData->GetClk2Win1(i),fTriggerData->GetDelayClk2Win1(i));\r
105                 fBBGate[i] = new AliVZEROLogicalSignal(clk1BB & clk2BB);\r
106         }\r
107 }\r
108 //_____________________________________________________________________________\r
109 void AliVZEROTriggerSimulator::GenerateBGWindows() \r
110 {\r
111         // Generates the BG observation window\r
112         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
113                 AliVZEROLogicalSignal clk1BG(fTriggerData->GetClk1Win2(i),fTriggerData->GetDelayClk1Win2(i));\r
114                 AliVZEROLogicalSignal clk2BG(fTriggerData->GetClk2Win2(i),fTriggerData->GetDelayClk2Win2(i));\r
115                 fBGGate[i] = new AliVZEROLogicalSignal(clk1BG & clk2BG);\r
116         }\r
117 }\r
118 \r
119 //_____________________________________________________________________________\r
120 AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const \r
121 {\r
122         // Gets Trigger object for VZERO set\r
123         AliDebug(1,"Loading Trigger parameters");\r
124         AliCDBManager *man = AliCDBManager::Instance();\r
125         \r
126         \r
127         AliCDBEntry *entry=0;\r
128         \r
129         entry = man->Get("VZERO/Trigger/Data");\r
130         if(!entry){\r
131                 AliFatal("Load of trigger calibration data from default storage failed!");\r
132                 return NULL;\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 //_____________________________________________________________________________\r
147 void AliVZEROTriggerSimulator::LoadClockOffset()\r
148 {\r
149   // This method is used in order to\r
150   // retrieve the TDC clock offset including\r
151   // roll-over, trig count and CTP L0->L1 delay\r
152 \r
153   AliCDBEntry *entry0 = AliCDBManager::Instance()->Get("VZERO/Calib/Data");\r
154   if (!entry0) {\r
155     AliFatal("V0 Calib object is not found in OCDB !");\r
156     return;\r
157   }\r
158   AliVZEROCalibData *calibdata = (AliVZEROCalibData*) entry0->GetObject();\r
159 \r
160   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");\r
161   if (!entry) {\r
162     AliFatal("CTP timing parameters are not found in OCDB !");\r
163     return;\r
164   }\r
165   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();\r
166   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;\r
167 \r
168   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");\r
169   if (!entry1) {\r
170     AliFatal("CTP time-alignment is not found in OCDB !");\r
171     return;\r
172   }\r
173   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();\r
174   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);\r
175 \r
176   for(Int_t board = 0; board < AliVZEROTriggerData::kNCIUBoards; ++board) {\r
177     fClockOffset[board] = (((Float_t)calibdata->GetRollOver(board)-\r
178                             (Float_t)calibdata->GetTriggerCountOffset(board))*25.0-\r
179                            l1Delay+\r
180                            kV0Offset);\r
181     AliDebug(1,Form("Board %d Offset %f",board,fClockOffset[board]));\r
182   }\r
183 }\r
184 \r
185 //_____________________________________________________________________________\r
186 void AliVZEROTriggerSimulator::Run() {\r
187         //AliInfo("Generating VZERO Triggers");\r
188         \r
189         // Loop over VZERO entries\r
190         Int_t nEntries = (Int_t)fDigitsTree->GetEntries();\r
191         for (Int_t ievt=0; ievt<nEntries; ievt++) {\r
192                 fDigitsTree->GetEvent(ievt);\r
193                 \r
194                 Int_t nDigits = fDigits->GetEntriesFast();\r
195                 \r
196                 for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {\r
197                         AliVZEROdigit* digit = (AliVZEROdigit*)fDigits->At(iDigit);\r
198                         \r
199                         Int_t integrator = digit->Integrator();\r
200                         Int_t pmNumber   = digit->PMNumber();\r
201                         Int_t board   = AliVZEROCalibData::GetBoardNumber(pmNumber);\r
202                         Int_t channel = AliVZEROCalibData::GetFEEChannelNumber(pmNumber);\r
203                         if (board < 0 || channel < 0) continue;\r
204                         \r
205                         if(fTriggerData->GetEnableCharge(board,channel)) {\r
206                                 fCharges[pmNumber] = digit->ChargeADC(AliVZEROdigit::kNClocks/2);\r
207                                 if(fTriggerData->GetPedestalSubtraction(board)) {\r
208                                         if(fCharges[pmNumber]>=(Float_t) fTriggerData->GetPedestalCut(integrator,board,channel)){ \r
209                                                 fCharges[pmNumber] -= (Float_t) fTriggerData->GetPedestal(integrator,board,channel);\r
210                                         } else {\r
211                                                 fCharges[pmNumber] = 0.;\r
212                                         }\r
213                                 }\r
214                         } else {\r
215                                 fCharges[pmNumber] = 0.;\r
216                         }\r
217                         \r
218                         Float_t time = digit->Time();\r
219                         time -= fClockOffset[board];\r
220 \r
221                         AliDebug(10,Form(" Digit: %f %d %d %d %d %d %d %d %d",digit->Time(),\r
222                                          digit->ChargeADC(8),digit->ChargeADC(9),digit->ChargeADC(10),\r
223                                          digit->ChargeADC(11),digit->ChargeADC(12),digit->ChargeADC(13),\r
224                                          digit->ChargeADC(14),digit->ChargeADC(15)));\r
225                         AliDebug(10,Form(" PM nb : %d ; TDC= %f(%f)  Enable Time %d charge %d inCoin %d charge %f",\r
226                                          pmNumber,time,digit->Time(),\r
227                                          fTriggerData->GetEnableTiming(board,channel),fTriggerData->GetEnableCharge(board,channel),\r
228                                          fBBGate[board]->IsInCoincidence(time),fCharges[pmNumber]));\r
229                         fBBFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBBGate[board]->IsInCoincidence(time);\r
230                         fBGFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBGGate[board]->IsInCoincidence(time);\r
231                         \r
232                 } // end of loop over digits\r
233         } // end of loop over events in digits tree\r
234         \r
235         Int_t nBBflagsV0A = 0;\r
236         Int_t nBBflagsV0C = 0;\r
237         Int_t nBGflagsV0A = 0;\r
238         Int_t nBGflagsV0C = 0;\r
239         Float_t chargeV0A   = 0.;\r
240         Float_t chargeV0C   = 0.;\r
241         Int_t aBBflagsV0A = 0;\r
242         Int_t aBBflagsV0C = 0;\r
243         Int_t aBGflagsV0A = 0;\r
244         Int_t aBGflagsV0C = 0;\r
245 \r
246         for(int i=0;i<64;i++) {\r
247                 if(i<32) {\r
248                         nBBflagsV0C += fBBFlags[i]; \r
249                         nBGflagsV0C += fBGFlags[i];\r
250                         chargeV0C += fCharges[i];\r
251                         if (fBBFlags[i]) aBBflagsV0C |= (1 << i);\r
252                         if (fBGFlags[i]) aBGflagsV0C |= (1 << i);\r
253                 } else {\r
254                         nBBflagsV0A += fBBFlags[i]; \r
255                         nBGflagsV0A += fBGFlags[i];\r
256                         chargeV0A += fCharges[i];\r
257                         if (fBBFlags[i]) aBBflagsV0A |= (1 << (i-32));\r
258                         if (fBGFlags[i]) aBGflagsV0A |= (1 << (i-32));\r
259                 }\r
260                 //AliInfo(Form("Ch %d BB=%d BG=%d",i,fBBFlags[i],fBGFlags[i] )); \r
261         }\r
262 \r
263         // Store the BB and BG flags in the digits tree (user info)\r
264         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0A",aBBflagsV0A));\r
265         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BBflagsV0C",aBBflagsV0C));\r
266         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0A",aBGflagsV0A));\r
267         fDigitsTree->GetUserInfo()->Add(new TParameter<int>("BGflagsV0C",aBGflagsV0C));\r
268         \r
269         // BBA\r
270         if(nBBflagsV0A>=fTriggerData->GetBBAThreshold())  SetBBA();\r
271         \r
272         // BBC\r
273         if(nBBflagsV0C>=fTriggerData->GetBBCThreshold())  SetBBC();\r
274 \r
275         // BBA_AND_BBC\r
276         if(GetBBA() && GetBBC())  SetBBAandBBC();\r
277         \r
278         // BBA_OR_BBC\r
279         if(GetBBA() || GetBBC()) SetBBAorBBC();\r
280 \r
281         // BGA\r
282         if(nBGflagsV0A>=fTriggerData->GetBGAThreshold()) SetBGA();\r
283 \r
284         // BGC\r
285         if(nBGflagsV0C>=fTriggerData->GetBGCThreshold()) SetBGC();\r
286         \r
287         // BGA_AND_BBC (Beam Gas from RB24 side)\r
288         if(nBBflagsV0C>=fTriggerData->GetBBCForBGThreshold() && GetBGA()) SetBGAandBBC();\r
289         \r
290         // BGC_AND_BBA (Beam Gas from RB26 side)\r
291         if(nBBflagsV0A>=fTriggerData->GetBBAForBGThreshold() && GetBGC()) SetBGCandBBA();\r
292 \r
293         // CTA1_AND_CTC1 (Centrality trigger 1)\r
294         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() && chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1andCTC1();\r
295 \r
296         // CTA1_OR_CTC1 (Centrality trigger 1)\r
297         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() || chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1orCTC1();\r
298         \r
299         // CTA2_AND_CTC2 (Centrality trigger 2)\r
300         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() && chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2andCTC2();\r
301         \r
302         // CTA2_OR_CTC2 (Centrality trigger 2)\r
303         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() || chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2orCTC2();\r
304         \r
305         // MTA_AND_MTC (Multiplicity Trigger)\r
306         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
307            && (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
308                 SetMTAandMTC();\r
309         \r
310         // MTA_OR_MTC (Multiplicity Trigger)\r
311         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
312            || (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
313                 SetMTAorMTC();\r
314         \r
315         // BGA_OR_BGC\r
316         if(GetBGA() || GetBGC()) SetBGAorBGC();\r
317         \r
318         // (BGA and BBC) or (BGC and BBA) (Beam Gas from one of the two sides)\r
319         if(GetBGAandBBC() || GetBGCandBBA()) SetBeamGas();\r
320 \r
321 //      AliInfo(Form("BB Flags : V0A = %d  V0C = %d ",nBBflagsV0A, nBBflagsV0C )); \r
322 //      AliInfo(Form("BG Flags : V0A = %d  V0C = %d ",nBGflagsV0A, nBGflagsV0C )); \r
323 //      AliInfo(Form("Charges  : V0A = %d  V0C = %d ",chargeV0A, chargeV0C )); \r
324         \r
325 }\r
326 \r
327 \r
328 \r