CbmRoot
PairAnalysisMixingHandler.cxx
Go to the documentation of this file.
1 // //
3 // //
4 // Authors:
5 // * Copyright(c) 1998-2009, ALICE Experiment at CERN, All rights reserved. *
6 // Julian Book <Julian.Book@cern.ch>
7 /*
8 
9  Event mixing handler, that creates event pools of size fDepth. Events
10  are ring-buffered and mixing is done for events that are similar according
11  to the binnnings and variables specific via:
12 
13  AddVariable(PairAnalysisVarManager::ValueTypes type, TVectorD* const bins)
14 
15  You can specify which type of event mixing should be done (+-/-+,++,--) using:
16 
17  SetMixType(EMixType type)
18 
19 */
20 // //
22 
23 #include <TAxis.h>
24 #include <TH1.h>
25 #include <TVectorD.h>
26 
27 #include "PairAnalysisTrack.h"
28 
29 #include "PairAnalysis.h"
30 #include "PairAnalysisMixedEvent.h"
31 
33 
35 
37  : PairAnalysisMixingHandler("ME", "ME") {
38  //
39  // Default Constructor
40  //
41 }
42 
43 //______________________________________________
45  const char* title)
46  : TNamed(name, title), fArrPools("TClonesArray"), fAxes(kMaxCuts) {
47  //
48  // Named Constructor
49  //
50  for (Int_t i = 0; i < kMaxCuts; ++i) {
51  fEventCuts[i] = 0;
52  }
53  fAxes.SetOwner(kTRUE);
54 }
55 
56 //______________________________________________
58  //
59  // Default Destructor
60  //
61  fAxes.Delete();
62  if (fPID) delete fPID;
63 }
64 
65 //________________________________________________________________
68  TVectorD* const bins) {
69  //
70  // Add a variable to the mixing handler with arbitrary binning 'bins'
71  //
72 
73  // limit number of variables to kMaxCuts
74  if (fAxes.GetEntriesFast() >= kMaxCuts) return;
75 
76  Int_t size = fAxes.GetEntriesFast();
77  fEventCuts[size] = (UShort_t) type;
78  fAxes.Add(bins);
79 }
80 
81 //______________________________________________
83  PairAnalysis* papa) {
84  //
85  // fill event buffers and perform mixing if the pool depth is reached
86  //
87 
88  //check if there are tracks available
89  if (papa->GetTrackArray(0)->GetEntriesFast() == 0
90  && papa->GetTrackArray(1)->GetEntriesFast() == 0)
91  return;
92 
93  TString dim;
94  Int_t bin = FindBin(PairAnalysisVarManager::GetData(), &dim);
95 
96  //add mixing bin to event data
98 
99  if (bin < 0) {
100  Error("Fill", "Bin outside range: %s", dim.Data());
101  return;
102  }
103 
104  // get mixing pool, create it if it does not yet exist.
105  TClonesArray* poolp = static_cast<TClonesArray*>(fArrPools.At(bin));
106  // do mixing
107  if (poolp) {
108  TClonesArray& pool = *poolp;
109  DoMixing(pool, papa);
110  }
111 
112  Int_t index1 = 0;
113 
114  if (!poolp) {
115  Info("Fill", "New pool at %d (%s)", bin, dim.Data());
116  poolp = new (fArrPools[bin]) TClonesArray("PairAnalysisMixedEvent", fDepth);
117  poolp->SetUniqueID(0);
118  } else {
119  // one count further in the ring buffer
120  index1 = (poolp->GetUniqueID() + 1) % fDepth;
121  }
122 
123  TClonesArray& pool = *poolp;
124 
125  PairAnalysisMixedEvent* event =
126  static_cast<PairAnalysisMixedEvent*>(pool.At(index1));
127  if (!event) {
128  // Info("Fill",Form("new event at %d: %d",bin,index1));
129  event = new (pool[index1]) PairAnalysisMixedEvent();
130  // Int_t size = TMath::Max(papa->GetTrackArray(0)->GetEntriesFast(),papa->GetTrackArray(1)->GetEntriesFast()));
131  event->Set();
132  event->SetProcessID(fPID);
133  } else {
134  // Info("Fill",Form("use event at %d: %d",bin,index1));
135  }
136 
137  // event->SetProcessID(fPID);
138  event->SetTracks(*papa->GetTrackArray(0), *papa->GetTrackArray(1));
139  event->SetEventData(PairAnalysisVarManager::GetData());
140 
141  //set current event position in ring buffer
142  pool.SetUniqueID(index1);
143 }
144 
145 //______________________________________________
146 void PairAnalysisMixingHandler::DoMixing(TClonesArray& pool,
147  PairAnalysis* papa) {
148  //
149  // perform the mixing
150  //
151 
152  // we need at least one event for mixing
153  if (pool.GetEntriesFast() < 1) return;
154  // printf("entries: %d\n",pool.GetEntriesFast()); //buffer track arrays and copy them back afterwards
155 
156  TObjArray arrTrDummy[4];
157  for (Int_t i = 0; i < 4; ++i)
158  arrTrDummy[i] = papa->fTracks[i];
159 
160  //buffer also global event data
161  Double_t* values = PairAnalysisVarManager::GetData();
162 
163  // The event data should alread be filled, since
164  // all events are in the same mixing bin anyhow...
165  TObject* o = 0x0;
166  TIter ev1P(&arrTrDummy[0]);
167  TIter ev1N(&arrTrDummy[1]);
168 
169  // for (Int_t i2=i1+1; i2<pool.GetEntriesFast(); ++i2){
170  for (Int_t i1 = 0; i1 < pool.GetEntriesFast(); ++i1) {
171  const PairAnalysisMixedEvent* ev2 =
172  static_cast<PairAnalysisMixedEvent*>(pool.At(i1));
173  // don't mix with itself
174  if (!ev2) continue;
175 
176  //clear arryas
177  papa->fTracks[0].Clear();
178  papa->fTracks[1].Clear();
179  papa->fTracks[2].Clear();
180  papa->fTracks[3].Clear();
181 
182  //setup track arrays
183  ev1P.Reset();
184  ev1N.Reset();
185  TIter ev2P(ev2->GetTrackArrayP());
186  TIter ev2N(ev2->GetTrackArrayN());
187 
188  //mixing of ev1- ev2 (pair type4). This is common for all mixing types
189  while ((o = ev1N()))
190  papa->fTracks[1].Add(o);
191  while ((o = ev2P()))
192  papa->fTracks[2].Add(o);
193  papa->FillPairArrays(1, 2);
194 
195  if (fMixType == kAll || fMixType == kOSandLS) {
196  // all 4 pair arrays will be filled
197  while ((o = ev1P()))
198  papa->fTracks[0].Add(o);
199  while ((o = ev2N()))
200  papa->fTracks[3].Add(o);
201  papa->FillPairArrays(0, 2);
202  papa->FillPairArrays(1, 3);
203  if (fMixType == kAll) papa->FillPairArrays(0, 3);
204  }
205 
206  if (fMixType == kOSonly || fMixType == kOSandLS) {
207  //use the pair type of ev1- ev1 also for ev1 ev1-
208  papa->fTracks[1].Clear();
209  papa->fTracks[2].Clear();
210  while ((o = ev1P()))
211  papa->fTracks[1].Add(o);
212  while ((o = ev2N()))
213  papa->fTracks[2].Add(o);
214  papa->FillPairArrays(1, 2);
215  }
216  }
217 
218  //copy back the tracks
219  for (Int_t i = 0; i < 4; ++i) {
220  papa->fTracks[i].Clear();
221  papa->fTracks[i] = arrTrDummy[i];
222  }
223 
224  //set back global event values
226 }
227 
228 //______________________________________________
230  //
231  // initialise event buffers
232  //
233  Int_t size = GetNumberOfBins();
234 
235  Info("Init", "Creating a pool array with size %d", size);
236  if (papa && papa->DoEventProcess()) fArrPools.Expand(size);
237 
238  TString values;
239  for (Int_t i = 0; i < fAxes.GetEntriesFast(); ++i) {
240  TVectorD* bins = static_cast<TVectorD*>(fAxes.At(i));
241  Int_t nRows = bins->GetNrows();
242  values += Form("%s: ", PairAnalysisVarManager::GetValueName(fEventCuts[i]));
243  for (Int_t irow = 0; irow < nRows; ++irow) {
244  values += Form("%.2f, ", (*bins)[irow]);
245  }
246  }
247 
248  if (!fPID) { fPID = TProcessID::AddProcessID(); }
249 
250  Info("Init", "%s", values.Data());
251 }
252 
253 //______________________________________________
255  //
256  // return the number of bins this mixing handler has
257  //
258  Int_t size = 1;
259  for (Int_t i = 0; i < fAxes.GetEntriesFast(); ++i)
260  size *= ((static_cast<TVectorD*>(fAxes.At(i)))->GetNrows() - 1);
261  return size;
262 }
263 
264 //______________________________________________
265 Int_t PairAnalysisMixingHandler::FindBin(const Double_t values[],
266  TString* dim) {
267  //
268  // bin bin in mixing stack described by 'values'
269  // if the values are outside the binning range -1 is returned
270  // if dim is non NULL debug info will be stored in the variable
271  //
272 
273  if (fAxes.GetEntriesFast() == 0) {
274  if (dim) (*dim) = "single bin";
275  return 0;
276  }
277  if (dim) (*dim) = "";
278  Int_t sizeAdd = 1;
279  Int_t bin = 0;
280  for (Int_t i = 0; i < fAxes.GetEntriesFast(); ++i) {
281  Double_t val = values[fEventCuts[i]];
282  TVectorD* bins = static_cast<TVectorD*>(fAxes.At(i));
283  Int_t nRows = bins->GetNrows();
284  if ((val < (*bins)[0]) || (val > (*bins)[nRows - 1])) { return -1; }
285 
286  Int_t pos = TMath::BinarySearch(nRows, bins->GetMatrixArray(), val);
287  bin += sizeAdd * pos;
288  if (dim)
289  (*dim) += Form("%s: %f (%d); ",
291  val,
292  pos);
293  sizeAdd *= (nRows - 1);
294  }
295 
296  return bin;
297 }
PairAnalysisMixingHandler::Init
void Init(const PairAnalysis *papa=0x0)
Definition: PairAnalysisMixingHandler.cxx:229
PairAnalysisMixingHandler::FindBin
Int_t FindBin(const Double_t values[], TString *dim=0x0)
Definition: PairAnalysisMixingHandler.cxx:265
PairAnalysisMixingHandler::fAxes
TObjArray fAxes
Definition: PairAnalysisMixingHandler.h:54
PairAnalysisMixingHandler::kAll
@ kAll
Definition: PairAnalysisMixingHandler.h:29
PairAnalysisMixingHandler::Fill
void Fill(const PairAnalysisEvent *ev, PairAnalysis *papa)
Definition: PairAnalysisMixingHandler.cxx:82
PairAnalysisMixingHandler::GetNumberOfBins
Int_t GetNumberOfBins() const
Definition: PairAnalysisMixingHandler.cxx:254
i
int i
Definition: L1/vectors/P4_F32vec4.h:25
PairAnalysisMixingHandler::~PairAnalysisMixingHandler
virtual ~PairAnalysisMixingHandler()
Definition: PairAnalysisMixingHandler.cxx:57
PairAnalysisVarManager::SetEventData
static void SetEventData(const Double_t data[PairAnalysisVarManager::kNMaxValuesMC])
Definition: PairAnalysisVarManager.h:1542
PairAnalysisVarManager::GetValueName
static const char * GetValueName(Int_t i)
Definition: PairAnalysisVarManager.h:377
PairAnalysis::FillPairArrays
void FillPairArrays(Int_t arr1, Int_t arr2)
Definition: PairAnalysis.cxx:1356
PairAnalysisMixingHandler::DoMixing
void DoMixing(TClonesArray &pool, PairAnalysis *papa)
internal PID for references to buffered objects
Definition: PairAnalysisMixingHandler.cxx:146
PairAnalysis.h
PairAnalysisMixingHandler::fPID
TProcessID * fPID
Definition: PairAnalysisMixingHandler.h:56
PairAnalysisMixingHandler::fEventCuts
UShort_t fEventCuts[kMaxCuts]
Definition: PairAnalysisMixingHandler.h:53
PairAnalysisMixingHandler::fDepth
UShort_t fDepth
Definition: PairAnalysisMixingHandler.h:51
PairAnalysisMixingHandler.h
PairAnalysisMixingHandler::kOSonly
@ kOSonly
Definition: PairAnalysisMixingHandler.h:29
PairAnalysisMixingHandler
Definition: PairAnalysisMixingHandler.h:26
PairAnalysisMixedEvent::Set
void Set(Int_t size=100)
Definition: PairAnalysisMixedEvent.cxx:117
PairAnalysisMixedEvent
Definition: PairAnalysisMixedEvent.h:24
PairAnalysisVarManager::SetValue
static void SetValue(ValueTypes var, Double_t val)
Definition: PairAnalysisVarManager.h:372
PairAnalysisTrack.h
PairAnalysisEvent
Definition: PairAnalysisEvent.h:25
PairAnalysisVarManager::kMixingBin
@ kMixingBin
Definition: PairAnalysisVarManager.h:261
PairAnalysisMixingHandler::fMixType
EMixType fMixType
Definition: PairAnalysisMixingHandler.h:55
PairAnalysisMixingHandler::PairAnalysisMixingHandler
PairAnalysisMixingHandler()
PairAnalysisMixedEvent::GetTrackArrayP
const TClonesArray * GetTrackArrayP() const
Definition: PairAnalysisMixedEvent.h:37
PairAnalysis::fTracks
TObjArray fTracks[4]
Definition: PairAnalysis.h:212
PairAnalysisMixingHandler::kMaxCuts
@ kMaxCuts
Definition: PairAnalysisMixingHandler.h:28
PairAnalysis
Definition: PairAnalysis.h:22
PairAnalysisMixedEvent.h
pos
TVector3 pos
Definition: CbmMvdSensorDigiToHitTask.cxx:60
PairAnalysisMixedEvent::GetTrackArrayN
const TClonesArray * GetTrackArrayN() const
Definition: PairAnalysisMixedEvent.h:38
PairAnalysisVarManager::GetData
static Double_t * GetData()
Definition: PairAnalysisVarManager.h:386
PairAnalysisVarManager::ValueTypes
ValueTypes
Definition: PairAnalysisVarManager.h:71
PairAnalysisMixingHandler::kOSandLS
@ kOSandLS
Definition: PairAnalysisMixingHandler.h:29
PairAnalysis::DoEventProcess
Bool_t DoEventProcess() const
Definition: PairAnalysis.h:173
PairAnalysis::GetTrackArray
const TObjArray * GetTrackArray(Int_t i) const
Definition: PairAnalysis.h:78
PairAnalysisMixingHandler::fArrPools
TClonesArray fArrPools
Definition: PairAnalysisMixingHandler.h:52
ClassImp
ClassImp(PairAnalysisMixingHandler) PairAnalysisMixingHandler
Definition: PairAnalysisMixingHandler.cxx:34
PairAnalysisMixingHandler::AddVariable
void AddVariable(PairAnalysisVarManager::ValueTypes type, TVectorD *const bins)
Definition: PairAnalysisMixingHandler.cxx:66