Bug 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 \r
17 //  Simulate the VZERO Trigger response\r
18 // Use FEE parameters stored in Database\r
19 // Can work on real data or in simulation\r
20 #include "TTree.h"\r
21 #include "TClonesArray.h"\r
22 \r
23 #include "AliLog.h"\r
24 #include "AliCDBManager.h"\r
25 #include "AliCDBEntry.h"\r
26 #include "AliCDBStorage.h"\r
27 #include "AliCDBId.h"\r
28 #include "AliVZEROTriggerData.h"\r
29 #include "AliVZEROLogicalSignal.h"\r
30 #include "AliVZEROTriggerSimulator.h"\r
31 #include "AliVZEROdigit.h"\r
32 \r
33 ClassImp(AliVZEROTriggerSimulator)\r
34 \r
35 //________________________________________________________________\r
36 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(const AliVZEROTriggerSimulator& triggersim) : \r
37 TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
38 \r
39 {\r
40         // copy constructor\r
41         \r
42         AliInfo("Not implemented");\r
43 }\r
44 \r
45 //_____________________________________________________________________________\r
46 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator(TTree * digitsTree, TClonesArray* digits) : \r
47 TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(digitsTree),fDigits(digits),fTriggerWord(0)\r
48 {\r
49         for(int i=0;i<64;i++) {\r
50                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
51                 fCharges[i] = 0;\r
52         }\r
53         GenerateBBWindows();\r
54         GenerateBGWindows();\r
55         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
56                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
57                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
58                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
59                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
60         }\r
61 }\r
62 //_____________________________________________________________________________\r
63 AliVZEROTriggerSimulator::AliVZEROTriggerSimulator() : \r
64 TObject(),fTriggerData(LoadTriggerData()),fDigitsTree(NULL),fDigits(NULL),fTriggerWord(0)\r
65 {\r
66         for(int i=0;i<64;i++) {\r
67                 fBBFlags[i] = fBGFlags[i] = kFALSE;\r
68                 fCharges[i] = 0;\r
69         }\r
70         GenerateBBWindows();\r
71         GenerateBGWindows();\r
72         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
73                 fBBLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin1(i),0); \r
74                 fBGLatch[i] = new AliVZEROLogicalSignal(fTriggerData->GetLatchWin2(i),0); \r
75                 fBBReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin1(i),0);\r
76                 fBGReset[i] = new AliVZEROLogicalSignal(fTriggerData->GetResetWin2(i),0);               \r
77         }\r
78 }\r
79 \r
80 //_____________________________________________________________________________\r
81 AliVZEROTriggerSimulator::~AliVZEROTriggerSimulator(){\r
82         if(fBBGate) delete [] fBBGate;\r
83         if(fBGGate) delete [] fBGGate;\r
84         if(fBBLatch) delete [] fBBLatch;\r
85         if(fBBReset) delete [] fBBReset;\r
86         if(fBGLatch) delete [] fBGLatch;\r
87         if(fBGReset) delete [] fBGReset;\r
88 }\r
89 \r
90 //_____________________________________________________________________________\r
91 void AliVZEROTriggerSimulator::GenerateBBWindows() \r
92 {\r
93         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
94                 AliVZEROLogicalSignal Clk1BB(fTriggerData->GetClk1Win1(i),fTriggerData->GetDelayClk1Win1(i));\r
95                 AliVZEROLogicalSignal Clk2BB(fTriggerData->GetClk2Win1(i),fTriggerData->GetDelayClk2Win1(i));\r
96                 fBBGate[i] = new AliVZEROLogicalSignal(Clk1BB & Clk2BB);\r
97         }\r
98 }\r
99 //_____________________________________________________________________________\r
100 void AliVZEROTriggerSimulator::GenerateBGWindows() \r
101 {\r
102         for (int i=0; i<AliVZEROTriggerData::kNCIUBoards; i++) {\r
103                 AliVZEROLogicalSignal Clk1BG(fTriggerData->GetClk1Win2(i),fTriggerData->GetDelayClk1Win2(i));\r
104                 AliVZEROLogicalSignal Clk2BG(fTriggerData->GetClk2Win2(i),fTriggerData->GetDelayClk2Win2(i));\r
105                 fBGGate[i] = new AliVZEROLogicalSignal(Clk1BG & Clk2BG);\r
106         }\r
107 }\r
108 \r
109 //_____________________________________________________________________________\r
110 AliVZEROTriggerData * AliVZEROTriggerSimulator::LoadTriggerData() const \r
111 {\r
112         // Gets Trigger object for VZERO set\r
113         AliInfo("Loading Trigger parameters");\r
114         AliCDBManager *man = AliCDBManager::Instance();\r
115         \r
116         \r
117         AliCDBEntry *entry=0;\r
118         \r
119         entry = man->Get("VZERO/Trigger/Data");\r
120         if(!entry){\r
121                 AliWarning("Load of calibration data from default storage failed!");\r
122                 AliWarning("Calibration data will be loaded from local storage ($ALICE_ROOT)");\r
123                 \r
124                 man->SetDefaultStorage("local://$ALICE_ROOT/OCDB");\r
125                 entry = man->Get("VZERO/Trigger/Data",0);\r
126         }\r
127         \r
128         // Retrieval of data in directory VZERO/Calib/Trigger:\r
129         \r
130         AliVZEROTriggerData *triggerData = NULL;\r
131         \r
132         if (entry) triggerData = (AliVZEROTriggerData*) entry->GetObject();\r
133         if (!triggerData)  AliError("No Trigger data from database !");\r
134         \r
135         return triggerData;\r
136 }\r
137 \r
138 //_____________________________________________________________________________\r
139 void AliVZEROTriggerSimulator::Run() {\r
140         //AliInfo("Generating VZERO Triggers");\r
141         \r
142         // Loop over VZERO entries\r
143         Int_t nEntries = (Int_t)fDigitsTree->GetEntries();\r
144         for (Int_t ievt=0; ievt<nEntries; ievt++) {\r
145                 fDigitsTree->GetEvent(ievt);\r
146                 \r
147                 Int_t nDigits = fDigits->GetEntriesFast();\r
148                 \r
149                 for (Int_t iDigit=0; iDigit<nDigits; iDigit++) {\r
150                         AliVZEROdigit* digit = (AliVZEROdigit*)fDigits->At(iDigit);\r
151                         \r
152                         Int_t integrator = digit->Integrator();\r
153                         Int_t pmNumber   = digit->PMNumber();\r
154                         Int_t board   = pmNumber / 8;\r
155                         Int_t channel = pmNumber % 8;\r
156                         \r
157                         if(fTriggerData->GetEnableCharge(board,channel)) {\r
158                                 fCharges[pmNumber] = digit->ADC();\r
159                                 if(fTriggerData->GetPedestalSubtraction(board)) \r
160                                         if(fCharges[pmNumber]>=fTriggerData->GetPedestalCut(integrator,board,channel)){ \r
161                                                 fCharges[pmNumber] -= fTriggerData->GetPedestal(integrator,board,channel);\r
162                                         } else {\r
163                                                 fCharges[pmNumber] = 0;\r
164                                         }\r
165                         } else {\r
166                                 fCharges[pmNumber] = 0;\r
167                         }\r
168                         \r
169                         Float_t time = (Float_t)digit->Time() / 10.; // digit->Time() in bin of 100 picoseconds. Divide by 10 to have it in nanoseconds\r
170                         time += fTriggerData->GetDelayHit(board,channel);\r
171                         \r
172                         AliInfo(Form(" PM nb : %d ; ADC= %d ; TDC= %f  Enable Time %d charge %d",pmNumber,digit->ADC(),time,fTriggerData->GetEnableTiming(board,channel),fTriggerData->GetEnableCharge(board,channel)));\r
173                         fBBFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBBGate[board]->IsInCoincidence(time);\r
174                         fBGFlags[pmNumber] = fTriggerData->GetEnableTiming(board,channel) && fBGGate[board]->IsInCoincidence(time);\r
175                         \r
176                 } // end of loop over digits\r
177         } // end of loop over events in digits tree\r
178         \r
179         Int_t nBBflagsV0A = 0;\r
180         Int_t nBBflagsV0C = 0;\r
181         Int_t nBGflagsV0A = 0;\r
182         Int_t nBGflagsV0C = 0;\r
183         Int_t chargeV0A   = 0;\r
184         Int_t chargeV0C   = 0;\r
185 \r
186         for(int i=0;i<64;i++) {\r
187                 if(i<32) {\r
188                         nBBflagsV0C += fBBFlags[i]; \r
189                         nBGflagsV0C += fBGFlags[i];\r
190                         chargeV0C += fCharges[i];\r
191                 } else {\r
192                         nBBflagsV0A += fBBFlags[i]; \r
193                         nBGflagsV0A += fBGFlags[i];\r
194                         chargeV0A += fCharges[i];\r
195                 }\r
196                 //AliInfo(Form("Ch %d BB=%d BG=%d",i,fBBFlags[i],fBGFlags[i] )); \r
197         }\r
198         \r
199         // BBA\r
200         if(nBBflagsV0A>=fTriggerData->GetBBAThreshold())  SetBBA();\r
201         \r
202         // BBC\r
203         if(nBBflagsV0C>=fTriggerData->GetBBCThreshold())  SetBBC();\r
204 \r
205         // BBA_AND_BBC\r
206         if(GetBBA() && GetBBC())  SetBBAandBBC();\r
207         \r
208         // BBA_OR_BBC\r
209         if(GetBBA() || GetBBC()) SetBBAorBBC();\r
210 \r
211         // BGA\r
212         if(nBGflagsV0A>=fTriggerData->GetBGAThreshold()) SetBGA();\r
213 \r
214         // BGC\r
215         if(nBGflagsV0C>=fTriggerData->GetBGCThreshold()) SetBGC();\r
216         \r
217         // BGA_AND_BBC (Beam Gas from RB24 side)\r
218         if(nBBflagsV0C>=fTriggerData->GetBBCForBGThreshold() && GetBGA()) SetBGAandBBC();\r
219         \r
220         // BGC_AND_BBA (Beam Gas from RB26 side)\r
221         if(nBBflagsV0A>=fTriggerData->GetBBAForBGThreshold() && GetBGC()) SetBGCandBBA();\r
222 \r
223         // CTA1_AND_CTC1 (Centrality trigger 1)\r
224         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() && chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1andCTC1();\r
225 \r
226         // CTA1_OR_CTC1 (Centrality trigger 1)\r
227         if(chargeV0A>=fTriggerData->GetCentralityV0AThrLow() || chargeV0C>=fTriggerData->GetCentralityV0CThrLow()) SetCTA1orCTC1();\r
228         \r
229         // CTA2_AND_CTC2 (Centrality trigger 2)\r
230         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() && chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2andCTC2();\r
231         \r
232         // CTA2_OR_CTC2 (Centrality trigger 2)\r
233         if(chargeV0A>=fTriggerData->GetCentralityV0AThrHigh() || chargeV0C>=fTriggerData->GetCentralityV0CThrHigh()) SetCTA2orCTC2();\r
234         \r
235         // MTA_AND_MTC (Multiplicity Trigger)\r
236         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
237            && (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
238                 SetMTAandMTC();\r
239         \r
240         // MTA_OR_MTC (Multiplicity Trigger)\r
241         if((nBBflagsV0A<=fTriggerData->GetMultV0AThrHigh() && nBBflagsV0A>=fTriggerData->GetMultV0AThrLow())\r
242            || (nBBflagsV0C<=fTriggerData->GetMultV0CThrHigh() && nBBflagsV0C>=fTriggerData->GetMultV0CThrLow()) ) \r
243                 SetMTAorMTC();\r
244         \r
245         // BGA_OR_BGC\r
246         if(GetBGA() || GetBGC()) SetBGAorBGC();\r
247         \r
248         // (BGA and BBC) or (BGC and BBA) (Beam Gas from one of the two sides)\r
249         if(GetBGAandBBC() || GetBGCandBBA()) SetBeamGas();\r
250 \r
251         AliInfo(Form("BB Flags : V0A = %d  V0C = %d ",nBBflagsV0A, nBBflagsV0C )); \r
252         AliInfo(Form("BG Flags : V0A = %d  V0C = %d ",nBGflagsV0A, nBGflagsV0C )); \r
253         AliInfo(Form("Charges  : V0A = %d  V0C = %d ",chargeV0A, chargeV0C )); \r
254         \r
255 }\r
256 \r