BmnRoot
Loading...
Searching...
No Matches
CbmStsIdealMatchHits.cxx
Go to the documentation of this file.
1//* $Id: */
2
3// -------------------------------------------------------------------------
4// ----- CbmStsIdealMatchHits source file -----
5// ----- Created 27/11/06 by V. Friese -----
6// -------------------------------------------------------------------------
7#include <iostream>
8#include <iomanip>
9
10// --- Includes from ROOT
11#include "TClonesArray.h"
12
13// --- Includes from base
14#include "FairRootManager.h"
15#include "FairRunAna.h"
16#include "FairRuntimeDb.h"
17
18// --- Includes from STS
19#include "CbmGeoStsPar.h"
20#include "CbmStsDigiPar.h"
21#include "CbmStsDigiScheme.h"
22#include "CbmStsDigiMatch.h"
23#include "CbmStsHit.h"
25#include "CbmStsPoint.h"
26#include "CbmStsSector.h"
27#include "CbmStsStation.h"
28
29#include "TMath.h"
30
31using std::cout;
32using std::endl;
33using std::setw;
34using std::left;
35using std::right;
36using std::fixed;
37using std::setprecision;
38
39
40// ----- Default constructor -------------------------------------------
42 : FairTask("STSMatchHits", 1),
43 fGeoPar(NULL),
44 fDigiPar(NULL),
45 fPoints(NULL),
46 fDigis(NULL),
47 fDigiMatches(NULL),
48 fHits(NULL),
49 fDigiScheme(NULL),
50 fTimer(),
51 fNEvents(0),
52 fNEventsFailed(0),
53 fTime(0.),
54 fNHits(0.),
55 fNMatched(0.),
56 fNDistant(0.),
57 fNBackgrd(0.),
58 fCandMap(),
59 fIter()
60{
61 fDigiScheme = new CbmStsDigiScheme();
62}
63// -------------------------------------------------------------------------
64
65
66
67// ----- Standard constructor ------------------------------------------
69 : FairTask("STSMatchHits", iVerbose),
70 fGeoPar(NULL),
71 fDigiPar(NULL),
72 fPoints(NULL),
73 fDigis(NULL),
74 fDigiMatches(NULL),
75 fHits(NULL),
76 fDigiScheme(NULL),
77 fTimer(),
78 fNEvents(0),
79 fNEventsFailed(0),
80 fTime(0.),
81 fNHits(0.),
82 fNMatched(0.),
83 fNDistant(0.),
84 fNBackgrd(0.),
85 fCandMap(),
86 fIter()
87{
88 fDigiScheme = new CbmStsDigiScheme();
89}
90// -------------------------------------------------------------------------
91
92
93
94// ----- Constructor with name -----------------------------------------
95CbmStsIdealMatchHits::CbmStsIdealMatchHits(const char* name, Int_t iVerbose)
96 : FairTask(name, iVerbose),
97 fGeoPar(NULL),
98 fDigiPar(NULL),
99 fPoints(NULL),
100 fDigis(NULL),
101 fDigiMatches(NULL),
102 fHits(NULL),
103 fDigiScheme(NULL),
104 fTimer(),
105 fNEvents(0),
106 fNEventsFailed(0),
107 fTime(0.),
108 fNHits(0.),
109 fNMatched(0.),
110 fNDistant(0.),
111 fNBackgrd(0.),
112 fCandMap(),
113 fIter()
114{
115 fDigiScheme = new CbmStsDigiScheme();
116}
117// -------------------------------------------------------------------------
118
119
120
121// ----- Destructor ----------------------------------------------------
123 if ( fGeoPar ) delete fGeoPar;
124 if ( fDigiPar ) delete fDigiPar;
125 if ( fDigiScheme ) delete fDigiScheme;
126}
127// -------------------------------------------------------------------------
128
129
130
131// ----- Public method Exec --------------------------------------------
132void CbmStsIdealMatchHits::Exec(Option_t* opt) {
133
134 // Timer
135 fTimer.Start();
136 Bool_t warn = kFALSE;
137
138 // Counters
139 Int_t nHits = fHits->GetEntriesFast();
140 Int_t nNoDigi = 0;
141 Int_t nBackgrd = 0;
142 Int_t nDistant = 0;
143 Int_t nMatched = 0;
144
145 // Loop over all StsHits
146 for (Int_t iHit=0; iHit<nHits; iHit++) {
147 CbmStsHit* hit = (CbmStsHit*) fHits->At(iHit);
148 if ( ! hit ) {
149 cout << "-W- " << GetName() << "::Exec: Empty hit at index "
150 << iHit << endl;
151 warn = kTRUE;
152 continue;
153 }
154
155 // Determine sector type and channel numbers
156 Int_t iStation = hit->GetStationNr();
157 Int_t iSector = hit->GetSectorNr();
158 CbmStsStation* station = fDigiScheme->GetStationByNr(iStation);
159 CbmStsSector* sector = fDigiScheme->GetSector(iStation, iSector);
160 Int_t iType = sector->GetType();
161 CbmStsDigiMatch* dMatchF = NULL;
162 CbmStsDigiMatch* dMatchB = NULL;
163
164
165 // Hit coordinates and errors
166 Double_t xH = hit->GetX();
167 Double_t yH = hit->GetY();
168 Double_t dX = hit->GetDx();
169 Double_t dY = hit->GetDy();
170
171 // Get front side DigiMatch corresponding to hit
172 Int_t iMatchF = hit->GetDigi(0);
173 if ( iMatchF >= 0 )
174 dMatchF = (CbmStsDigiMatch*) fDigiMatches->At(iMatchF);
175 if ( ! dMatchF ) {
176 cout << "-E- " << GetName() << "::Exec: "
177 << "No DigiMatchF for hit " << iHit << endl;
178 hit->SetRefIndex(-1);
179 nNoDigi++;
180 warn = kTRUE;
181 continue;
182 }
183
184 // Get back side DigiMatch of hit (for strip sensors only)
185 if ( iType != 1 ) {
186 Int_t iMatchB = hit->GetDigi(1);
187 if ( iMatchB >= 0 )
188 dMatchB = (CbmStsDigiMatch*) fDigiMatches->At(iMatchB);
189 if ( ! dMatchB ) {
190 cout << "-E- " << GetName() << "::Exec: "
191 << "No DigiMatchB for hit " << iHit << endl;
192 hit->SetRefIndex(-1);
193 nNoDigi++;
194 warn = kTRUE;
195 continue;
196 }
197 }
198
199 // Map candidate points to distance to hit
200 fCandMap.clear();
201 Int_t nPointsF = 0;
202 Int_t nPointsB = 0;
203
204 if ( iType == 1 ) {
205 // Case pixel: Candidates are all points of the digi.
206 for (Int_t jMatchF=0; jMatchF<3; jMatchF++) {
207 Int_t iPointF = dMatchF->GetRefIndex(jMatchF);
208 if ( iPointF < 0 ) continue;
209 nPointsF++;
210 // Calculate distance to hit
211 CbmStsPoint* point = (CbmStsPoint*) fPoints->At(iPointF);
212 if ( ! point ) {
213 cout << "-E- " << GetName() << "::Exec: "
214 << "No StsPoint (" << iPointF << ") for pixel hit "
215 << iHit << endl;
216 warn = kTRUE;
217 continue;
218 }
219 // point coordinates in station midplane
220 Double_t xP = point->GetX(station->GetZ());
221 Double_t yP = point->GetY(station->GetZ());
222 Double_t dist = TMath::Sqrt( (xP-xH)*(xP-xH) + (yP-yH)*(yP-yH) );
223 fCandMap[dist] = iPointF;
224 } // front digi loop
225 } // pixel sensor
226
227 else if ( iType == 2 || iType == 3 ) {
228 // Case strip: Candidates are points corresponding to front
229 // and back side digis
230 for ( Int_t jMatchF=0; jMatchF<3; jMatchF++) {
231 Int_t iPointF = dMatchF->GetRefIndex(jMatchF);
232 if ( iPointF < 0 ) continue;
233 nPointsF++;
234 for ( Int_t jMatchB=0; jMatchB<3; jMatchB++) {
235 Int_t iPointB = dMatchB->GetRefIndex(jMatchB);
236 if ( iPointB < 0 ) continue;
237 if ( jMatchF == 0 ) nPointsB++;
238 if ( iPointB != iPointF ) continue; // chance combination
239 // Calculate distance to hit
240 CbmStsPoint* point = (CbmStsPoint*) fPoints->At(iPointF);
241 if ( ! point ) {
242 cout << "-E- " << GetName() << "::Exec: "
243 << "No StsPoint (" << iPointF << ") for strip hit "
244 << iHit << endl;
245 warn = kTRUE;
246 continue;
247 }
248 // point coordinates in station midplane
249 Double_t xP = point->GetX(station->GetZ());
250 Double_t yP = point->GetY(station->GetZ());
251 Double_t dist = TMath::Sqrt( (xP-xH)*(xP-xH) +
252 (yP-yH)*(yP-yH) );
253 fCandMap[dist] = iPointF;
254 } // back digi loop
255 } // front digi loop
256 } // strip sensor
257
258 else {
259 // Unknown sensor type
260 cout << "-E- " << GetName() << "::Exec: Unknwon sensor type "
261 << iType << endl;
262 Fatal("Exec", "Unknwon sensor type");
263 }
264
265 if ( fVerbose > 1 ) cout << "-I- " << GetName() << ": Hit "
266 << iHit << ", type " << iType
267 << ", points " << nPointsF << " / "
268 << nPointsB << ", candidates "
269 << fCandMap.size();
270
271
272 // Check for at least one candidate point. Else background hit.
273 // This can happen for noise digis or for fake combinations
274 // of strip digis.
275 if ( fCandMap.empty() ) {
276 hit->SetRefIndex(-1);
277 if (fVerbose>1) cout << ", background " << endl;
278 nBackgrd++;
279 continue;
280 }
281
282 // Select closest point from candidates.
283 Double_t distMin = 999999.;
284 Int_t iPoint = -1;
285 for (fIter=fCandMap.begin(); fIter!=fCandMap.end(); fIter++) {
286 if ( (*fIter).first < distMin ) {
287 distMin = (*fIter).first;
288 iPoint = (*fIter).second;
289 }
290 }
291 if ( iPoint < 0 ) {
292 cout << "-E- " << GetName() << "::Exec: "
293 << "No closest point found in candidate map!" << endl;
294 Fatal("Exec", "No closest point");
295 }
296 CbmStsPoint* point = (CbmStsPoint*) fPoints->At(iPoint);
297 if (fVerbose>1) cout << ", matched to " << iPoint << ", distance "
298 << distMin << " cm";
299
300 // Check whether closest point is more than 5 sigma away from hit.
301 // This should not happen in case of pixel or strip OSU sensors,
302 // but can happen for strip GSI sensors due to multiple
303 // crossings of front and back side strips.
304 Double_t xP = point->GetX(station->GetZ());
305 Double_t yP = point->GetY(station->GetZ());
306 if ( TMath::Abs(xP-xH) > 5. * dX ||
307 TMath::Abs(yP-yH) > 5. * dY ) {
308 hit->SetRefIndex(-2);
309 nDistant++;
310 if (fVerbose>1) cout << ", distant" << endl;
311 if ( iType == 1 || iType == 2) {
312 cout << "-W- " << fName << "::Exec: "
313 << "Distant hit in pixel / strip MSU station" << endl;
314 cout << "Hit " << iHit << " coordinates " << xH
315 << ", " << yH << endl;
316 cout << "Matched point " << iPoint << ", distance "
317 << distMin << endl;
318 cout << "Distance y " << TMath::Abs(xP-xH) << " Error " << dX << endl;
319 cout << "Distance y " << TMath::Abs(yP-yH) << " Error " << dY << endl;
320 }
321
322
323 continue;
324 }
325
326 // Match closest StsPoint to hit
327 hit->SetRefIndex(iPoint);
328 nMatched++;
329 if (fVerbose>1) cout << ", good match" << endl;
330
331 // Check whether hit and point are in the same station.
332 // Else something has really gone wrong.
333 if ( TMath::Abs(hit->GetZ() - point->GetZ()) > 1. ) {
334 cout << "-E- " << GetName() << "::Exec: Hit " << iHit
335 << " is at z = " << hit->GetZ() << " cm, but matched point "
336 << iPoint << " is at z = " << point->GetZ() << "cm " << endl;
337 Fatal("Exec", "Different stations for hit and point");
338 }
339
340 } // Loop over StsHits
341
342 // Event statistics
343 fTimer.Stop();
344 if ( fVerbose ) {
345 if ( warn ) cout << "- ";
346 else cout << "+ ";
347 cout << setw(15) << left << fName << ": " << setprecision(4) << setw(8)
348 << fixed << right << fTimer.RealTime()
349 << " s, hits " << nHits << ", matched " << nMatched << ", distant "
350 << nDistant << ", background " << setw(6) << nBackgrd << endl;
351 }
352
353 // Run statistics
354 if ( warn) fNEventsFailed++;
355 else {
356 fNEvents++;
357 fTime += fTimer.RealTime();
358 fNHits += Double_t(nHits);
359 fNMatched += Double_t(nMatched);
360 fNDistant += Double_t(nDistant);
361 fNBackgrd += Double_t(nBackgrd);
362 }
363
364}
365// -------------------------------------------------------------------------
366
367
368
369// ----- Private method SetParContainers -------------------------------
370void CbmStsIdealMatchHits::SetParContainers() {
371
372 // Get run and runtime database
373 FairRunAna* run = FairRunAna::Instance();
374 if ( ! run ) Fatal("SetParContainers", "No analysis run");
375 FairRuntimeDb* db = run->GetRuntimeDb();
376 if ( ! db ) Fatal("SetParContainers", "No runtime database");
377
378 // Get STS geometry and digitisation parameter container
379 fGeoPar = (CbmGeoStsPar*) db->getContainer("CbmGeoStsPar");
380 fDigiPar = (CbmStsDigiPar*) db->getContainer("CbmStsDigiPar");
381
382}
383// -------------------------------------------------------------------------
384
385
386
387// ----- Private method Init -------------------------------------------
388InitStatus CbmStsIdealMatchHits::Init() {
389
390 // Reset counters
391 fNEvents = fNEventsFailed = 0;
392 fTime = fNHits = fNMatched = fNDistant = fNBackgrd = 0.;
393
394 // Get input arrays
395 FairRootManager* ioman = FairRootManager::Instance();
396 if ( ! ioman ) Fatal("Init", "No FairRootManager");
397 fPoints = (TClonesArray*) ioman->GetObject("StsPoint");
398 if ( ! fPoints ) {
399 //cout << "-E- " << GetName() << "::Init: No STSPoint array!" << endl;
400 return kERROR;
401 }
402 fDigis = (TClonesArray*) ioman->GetObject("StsDigi");
403 if ( ! fDigis ) {
404 cout << "-E- " << GetName() << "::Init: No StsDigi array!" << endl;
405 return kERROR;
406 }
407 fDigiMatches = (TClonesArray*) ioman->GetObject("StsDigiMatch");
408 if ( ! fDigiMatches ) {
409 cout << "-E- " << GetName() << "::Init: No StsDigiMatch array!"
410 << endl;
411 return kERROR;
412 }
413 fHits = (TClonesArray*) ioman->GetObject("StsHit");
414 if ( ! fHits ) {
415 cout << "-E- " << GetName() << "::Init: No StsHit array!" << endl;
416 return kERROR;
417 }
418
419 // Build digitisation scheme
420 if ( fDigiScheme->Init(fGeoPar, fDigiPar) ) {
421 if (fVerbose == 1 || fVerbose == 2) fDigiScheme->Print(kFALSE);
422 else if (fVerbose > 2) fDigiScheme->Print(kTRUE);
423 cout << "-I- " << fName << "::Init: "
424 << "STS digitisation scheme succesfully initialised" << endl;
425 cout << " Stations: " << fDigiScheme->GetNStations()
426 << ", Sectors: " << fDigiScheme->GetNSectors() << ", Channels: "
427 << fDigiScheme->GetNChannels() << endl;
428 return kSUCCESS;
429 }
430
431 return kERROR;
432
433}
434// -------------------------------------------------------------------------
435
436
437
438// ----- Private method ReInit -----------------------------------------
439InitStatus CbmStsIdealMatchHits::ReInit() {
440
441 // Clear digitisation scheme
442 fDigiScheme->Clear();
443
444 // Build new digitisation scheme
445 if ( fDigiScheme->Init(fGeoPar, fDigiPar) ) return kSUCCESS;
446
447 return kERROR;
448
449}
450// -------------------------------------------------------------------------
451
452
453
454// ----- Private method Finish -----------------------------------------
455void CbmStsIdealMatchHits::Finish() {
456
457 cout << endl;
458 cout << "============================================================"
459 << endl;
460 cout << "===== " << GetName() << ": Run summary " << endl;
461 cout << "===== " << endl;
462 cout << "===== Good events : " << setw(6) << fNEvents << endl;
463 cout << "===== Failed events : " << setw(6) << fNEventsFailed << endl;
464 cout << "===== Average time : " << setprecision(4) << setw(8) << right
465 << fTime / Double_t(fNEvents) << " s" << endl;
466 cout << "===== " << endl;
467 cout << "===== Hits per event : " << fixed << setprecision(0)
468 << fNHits / Double_t(fNEvents) << endl;
469 cout << setprecision(2);
470 cout << "===== Matched hits : " << fixed << setw(6) << right
471 << fNMatched / fNHits * 100. << " %" << endl;
472 cout << "===== Distant hits : " << fixed << setw(6) << right
473 << fNDistant / fNHits * 100. << " %" << endl;
474 cout << "===== Background hits : " << fixed << setw(6) << right
475 << fNBackgrd / fNHits * 100. << " % " << endl;
476 cout << "============================================================"
477 << endl;
478
479}
480// -------------------------------------------------------------------------
vector< Double_t > dist(vector< Double_t > qp, Double_t mu)
Definition BmnMath.cxx:869
BmnSsdDigitizeParameters * fDigiPar
Digitisation parameters.
Int_t GetRefIndex(Int_t i=0) const
void Print(Bool_t kLong=kFALSE)
CbmStsStation * GetStationByNr(Int_t stationNr)
CbmStsSector * GetSector(Int_t stationNr, Int_t sectorNr)
virtual Int_t GetStationNr() const
Definition CbmStsHit.h:66
Int_t GetSectorNr() const
Definition CbmStsHit.h:68
Int_t GetDigi(Int_t iSide) const
virtual void Exec(Option_t *opt)
Double_t GetY(Double_t z) const
Double_t GetX(Double_t z) const
Int_t GetType() const
Double_t GetZ(Int_t it=0)
-clang-format