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