BmnRoot
Loading...
Searching...
No Matches
BmnCSCHitMaker.cxx
Go to the documentation of this file.
1#include "BmnCSCHitMaker.h"
2
3#include "BmnEventHeader.h"
4#include "FairRunAna.h"
5#include "TSystem.h"
6
7#include <TChain.h>
8#include <TRandom.h>
9#include <TStopwatch.h>
10#include <zlib.h>
11
12static Double_t workTime = 0.0;
13
15 : fHitMatching(kTRUE)
16{
17
18 fInputPointsBranchName = "CSCPoint";
19 fInputDigitsBranchName = "BmnCSCDigit";
20 fInputDigitMatchesBranchName = "BmnCSCDigitMatch";
21
22 fOutputHitsBranchName = "BmnCSCHit";
23
24 fVerbose = 1;
25 fField = NULL;
26
27 fCurrentConfig = BmnCSCConfiguration::None;
28 StationSet = nullptr;
29 TransfSet = nullptr;
30
31 fBmnCSCHitsArray = nullptr;
32 fBmnCSCUpperClustersArray = nullptr;
33 fBmnCSCLowerClustersArray = nullptr;
34}
35
36BmnCSCHitMaker::BmnCSCHitMaker(Int_t run_period, Int_t run_number, Bool_t isExp, TString alignFile, Bool_t isSrc)
37 : fHitMatching(kTRUE)
38{
39
40 fInputPointsBranchName = "CSCPoint";
41 fInputDigitsBranchName = (!isExp) ? "BmnCSCDigit" : "CSC";
42 fIsExp = isExp;
43 fIsSrc = isSrc;
44
45 fInputDigitMatchesBranchName = "BmnCSCDigitMatch";
46
47 fOutputHitsBranchName = "BmnCSCHit";
48
49 fVerbose = 1;
50 fField = NULL;
51
52 fCurrentConfig = BmnCSCConfiguration::None;
53 StationSet = nullptr;
54 TransfSet = nullptr;
55
56 fBmnCSCHitsArray = nullptr;
57 fBmnCSCUpperClustersArray = nullptr;
58 fBmnCSCLowerClustersArray = nullptr;
59
60 switch (run_period) {
61 case 7: // BM@N RUN-7 (and SRC)
63 if (run_number >= 2041 && run_number <= 3588) {
65 }
66 break;
67 case 8:
68 if (fIsSrc) {
69 fCurrentConfig = BmnCSCConfiguration::RunSRC2021;
70 } else {
71 fCurrentConfig = BmnCSCConfiguration::FullCSC_Run8;
72 }
73 break;
74 case 9:
75 fCurrentConfig = BmnCSCConfiguration::Run9;
76 break;
77
78 default:
79 fCurrentConfig = BmnCSCConfiguration::None;
80 }
81}
82
84{
85 if (StationSet) {
86 delete StationSet;
87 }
88
89 if (TransfSet) {
90 delete TransfSet;
91 }
92
93 if (fBmnCSCHitsArray) {
94 fBmnCSCHitsArray->Delete();
95 delete fBmnCSCHitsArray;
96 }
97 if (fBmnCSCUpperClustersArray) {
98 fBmnCSCUpperClustersArray->Delete();
99 delete fBmnCSCUpperClustersArray;
100 }
101 if (fBmnCSCLowerClustersArray) {
102 fBmnCSCLowerClustersArray->Delete();
103 delete fBmnCSCLowerClustersArray;
104 }
105}
106
107void BmnCSCHitMaker::LoadDetectorConfiguration()
108{
109 TString gPathCSCConfig = gSystem->Getenv("VMCWORKDIR");
110 gPathCSCConfig += "/parameters/csc/XMLConfigs/";
111
112 switch (fCurrentConfig) {
114 XMLConfigFile = gPathCSCConfig + "CSCRunSpring2018.xml";
115 if (fVerbose)
116 cout << " Current CSC Configuration : RunSpring2018"
117 << "\n";
118 break;
119
121 XMLConfigFile = gPathCSCConfig + "CSCRunSRCSpring2018.xml";
122 if (fVerbose)
123 cout << " Current CSC Configuration : RunSRCSpring2018"
124 << "\n";
125 break;
126
128 XMLConfigFile = gPathCSCConfig + "CSCRun8.xml";
129 if (fVerbose)
130 cout << " Current CSC Configuration : Run8"
131 << "\n";
132 break;
133
135 XMLConfigFile = gPathCSCConfig + "CSCRunSRC2021.xml";
136 if (fVerbose)
137 cout << " Current CSC Configuration : RunSRC2021"
138 << "\n";
139 break;
140
142 XMLConfigFile = gPathCSCConfig + "LargeCSCRun8.xml";
143 if (fVerbose)
144 cout << " Current CSC Configuration : LargeCSCRun8"
145 << "\n";
146 break;
147
149 XMLConfigFile = gPathCSCConfig + "FullCSCRun8.xml";
150 if (fVerbose)
151 cout << " Current CSC Configuration : FullCSCRun8 (4 small + 1 large chambers)"
152 << "\n";
153 break;
154
156 XMLConfigFile = gPathCSCConfig + "CSCRun9.xml";
157 if (fVerbose)
158 cout << " Current CSC Configuration : Run9"
159 << "\n";
160 break;
161
162 default:;
163 }
164
165 if (!gSystem->AccessPathName(XMLConfigFile)) { // returns FALSE if the file exists
166 if (fVerbose)
167 cout << " XMLConfigFile : " << XMLConfigFile << "\n";
168 StationSet = new BmnCSCStationSet(XMLConfigFile);
169 TransfSet = new BmnCSCTransform();
170 TransfSet->LoadFromXMLFile(XMLConfigFile);
171 } else {
172 Fatal("BmnCSCHitMaker::LoadDetectorConfiguration()", " !!! Current configuration is not set !!! ");
173 }
174}
175
177{
178
179 if (fVerbose > 1)
180 cout << "=================== BmnCSCHitMaker::Init() started ====================" << endl;
181
182 FairRootManager* ioman = FairRootManager::Instance();
183
184 fBmnCSCDigitsArray = (TClonesArray*)ioman->GetObject(fInputDigitsBranchName);
185 if (!fBmnCSCDigitsArray) {
186 cout << "BmnCSCHitMaker::Init(): branch " << fInputDigitsBranchName << " not found! Task will be deactivated"
187 << endl;
188 SetActive(kFALSE);
189 return kERROR;
190 }
191
192 fBmnCSCDigitMatchesArray = (TClonesArray*)ioman->GetObject(fInputDigitMatchesBranchName);
193
194 if (fVerbose > 1) {
195 if (fBmnCSCDigitMatchesArray)
196 cout << " Strip matching information exists!\n";
197 else
198 cout << " Strip matching information doesn`t exist!\n";
199 }
200
201 fBmnCSCHitsArray = new TClonesArray(fOutputHitsBranchName);
202 ioman->Register(fOutputHitsBranchName, "CSC", fBmnCSCHitsArray, kTRUE);
203 fBmnCSCUpperClustersArray = new TClonesArray("StripCluster");
204 ioman->Register("BmnCSCUpperCluster", "CSC", fBmnCSCUpperClustersArray, kTRUE);
205 fBmnCSCLowerClustersArray = new TClonesArray("StripCluster");
206 ioman->Register("BmnCSCLowerCluster", "CSC", fBmnCSCLowerClustersArray, kTRUE);
207
208 // Create CSC detector ------------------------------------------------------
209
210 LoadDetectorConfiguration();
211
212 fField = FairRunAna::Instance()->GetField();
213 if (!fField)
214 Fatal("Init", "No Magnetic Field found");
215
216 //--------------------------------------------------------------------------
217
218 if (fVerbose > 1)
219 cout << "=================== BmnCSCHitMaker::Init() finished ===================" << endl;
220
221 return kSUCCESS;
222}
223
224void BmnCSCHitMaker::Exec(Option_t* opt)
225{
226
227 TStopwatch sw;
228 sw.Start();
229
230 if (!IsActive())
231 return;
232
233 fBmnCSCHitsArray->Delete();
234 fBmnCSCUpperClustersArray->Delete();
235 fBmnCSCLowerClustersArray->Delete();
236
239
240 if (fVerbose > 1)
241 cout << "=================== BmnCSCHitMaker::Exec() started ====================" << endl;
242
243 if (fVerbose > 1)
244 cout << " BmnCSCHitMaker::Exec(), Number of BmnCSCDigits = " << fBmnCSCDigitsArray->GetEntriesFast() << "\n";
245
247
248 if (fVerbose > 1)
249 cout << "=================== BmnCSCHitMaker::Exec() finished ===================" << endl;
250
251 sw.Stop();
252 workTime += sw.RealTime();
253}
254
256{
257
258 // FairMCPoint* MCPoint;
259 BmnCSCDigit* digit;
260 BmnMatch* strip_match;
261
262 BmnCSCStation* station;
263 BmnCSCModule* module;
264
265 // Loading digits ---------------------------------------------------------------
266 Int_t AddedDigits = 0;
267 Int_t AddedStripDigitMatches = 0;
268
269 for (Int_t idigit = 0; idigit < fBmnCSCDigitsArray->GetEntriesFast(); idigit++) {
270 digit = (BmnCSCDigit*)fBmnCSCDigitsArray->At(idigit);
271 if (!digit->IsGoodDigit())
272 continue;
273 station = StationSet->GetStation(digit->GetStation());
274 module = station->GetModule(digit->GetModule());
275
276 if (module->SetStripSignalInLayer(digit->GetStripLayer(), digit->GetStripNumber(), digit->GetStripSignal()))
277 AddedDigits++;
278
279 if (fBmnCSCDigitMatchesArray) {
280 strip_match = (BmnMatch*)fBmnCSCDigitMatchesArray->At(idigit);
281 if (module->SetStripMatchInLayer(digit->GetStripLayer(), digit->GetStripNumber(), *strip_match))
282 AddedStripDigitMatches++;
283 }
284
285 // Add a digit number match to the current strip
286 BmnMatch stripDigitNumberMatch; // digit number information for the current strip
287 stripDigitNumberMatch.AddLink(1.0, idigit);
288 module->SetStripDigitNumberMatchInLayer(digit->GetStripLayer(), digit->GetStripNumber(), stripDigitNumberMatch);
289 }
290
291 if (fVerbose > 1)
292 cout << " Processed strip digits : " << AddedDigits << "\n";
293 if (fVerbose > 1 && fBmnCSCDigitMatchesArray)
294 cout << " Added strip digit matches : " << AddedStripDigitMatches << "\n";
295 //------------------------------------------------------------------------------
296
297 // Processing digits
298 StationSet->ProcessPointsInDetector();
299
300 Int_t NCalculatedPoints = StationSet->CountNProcessedPointsInDetector();
301 if (fVerbose > 1)
302 cout << " Calculated points : " << NCalculatedPoints << "\n";
303 if (fVerbose == 1)
304 cout << "BmnCSCHitMaker: " << NCalculatedPoints << " hits\n";
305
306 Int_t clear_matched_points_cnt = 0; // points with the only one match-index
307
308 map<Int_t, StripCluster> UniqueUpperClusters;
309 map<Int_t, StripCluster> UniqueLowerClusters;
310
311 for (Int_t iStation = 0; iStation < StationSet->GetNStations(); ++iStation) {
312 station = StationSet->GetStation(iStation);
313
314 for (Int_t iModule = 0; iModule < station->GetNModules(); ++iModule) {
315 module = station->GetModule(iModule);
316
317 for (Int_t iLayer = 0; iLayer < module->GetNStripLayers(); ++iLayer) {
318 BmnCSCLayer layer = module->GetStripLayer(iLayer);
319
320 for (size_t iCluster = 0; iCluster < layer.GetStripClusters().size(); ++iCluster) {
321 StripCluster cluster = layer.GetStripClusters()[iCluster];
322 cluster.SetModule(iModule);
323 cluster.SetStation(iStation);
324
325 // Saving all strip clusters into branches (each cluster have its own unique id in the current
326 // event)
327 if (cluster.GetType() == UpperStripLayer) {
328 new ((*fBmnCSCUpperClustersArray)[fBmnCSCUpperClustersArray->GetEntriesFast()])
329 StripCluster(cluster);
330 } else {
331 new ((*fBmnCSCLowerClustersArray)[fBmnCSCLowerClustersArray->GetEntriesFast()])
332 StripCluster(cluster);
333 }
334 }
335 }
336
337 Int_t NIntersectionPointsInModule = module->GetNIntersectionPoints();
338
339 // Int_t NPseudoIntersectionsInModule = module->GetNPseudoIntersections();
340 Int_t NPseudoIntersectionsInModule = 0; // !!! IGNORE PSEUDO INTERSECTIONS !!!
341
342 for (Int_t iPoint = 0; iPoint < NIntersectionPointsInModule + NPseudoIntersectionsInModule; ++iPoint) {
343
344 // Double_t sigL;
345 // Double_t sigU;
346 Double_t x, y, z;
347 Double_t x_err, y_err, z_err;
348
349 if (iPoint < NIntersectionPointsInModule) {
350 // sigL = module->GetIntersectionPoint_LowerLayerSripTotalSignal(iPoint);
351 // sigU = module->GetIntersectionPoint_UpperLayerSripTotalSignal(iPoint);
352 x = module->GetIntersectionPointX(iPoint);
353 y = module->GetIntersectionPointY(iPoint);
354 z = module->GetZPositionRegistered();
355 x_err = module->GetIntersectionPointXError(iPoint);
356 y_err = module->GetIntersectionPointYError(iPoint);
357 z_err = 0.0;
358 } else {
359 // sigL =
360 // module->GetPseudoIntersection_LowerLayerSripTotalSignal(iPoint-NIntersectionPointsInModule); sigU
361 // = module->GetPseudoIntersection_UpperLayerSripTotalSignal(iPoint-NIntersectionPointsInModule);
362 x = module->GetPseudoIntersectionX(iPoint - NIntersectionPointsInModule);
363 y = module->GetPseudoIntersectionY(iPoint - NIntersectionPointsInModule);
364 z = module->GetZPositionRegistered();
365 x_err = module->GetPseudoIntersectionXError(iPoint - NIntersectionPointsInModule);
366 y_err = module->GetPseudoIntersectionYError(iPoint - NIntersectionPointsInModule);
367 z_err = 0.0;
368 }
369
370 // Transform hit coordinates from local coordinate system of GEM-planes to global
371 if (TransfSet) {
372 Plane3D::Point glob_point = TransfSet->ApplyTransforms(Plane3D::Point(-x, y, z), iStation, iModule);
373 x = -glob_point.X();
374 y = glob_point.Y();
375 z = glob_point.Z();
376 }
377
378 Int_t RefMCIndex = -1;
379
380 // MC-matching for the current hit (define RefMCIndex)) ---------
381 BmnMatch mc_match_hit;
382 if (iPoint < NIntersectionPointsInModule) {
383 mc_match_hit = module->GetIntersectionPointMatch(iPoint);
384 } else {
385 mc_match_hit = module->GetPseudoIntersectionMatch(iPoint - NIntersectionPointsInModule);
386 }
387
388 Int_t most_probably_index = -1;
389 Double_t max_weight = 0;
390
391 Int_t n_links = mc_match_hit.GetNofLinks();
392 if (n_links == 1)
393 clear_matched_points_cnt++;
394 for (Int_t ilink = 0; ilink < n_links; ilink++) {
395 Int_t index = mc_match_hit.GetLink(ilink).GetIndex();
396 Double_t weight = mc_match_hit.GetLink(ilink).GetWeight();
397 if (weight > max_weight) {
398 max_weight = weight;
399 most_probably_index = index;
400 }
401 }
402
403 RefMCIndex = most_probably_index;
404 //--------------------------------------------------------------
405
406 // Add hit ------------------------------------------------------
407 x *= -1; // invert to global X //Temporary switched off
408
409 new ((*fBmnCSCHitsArray)[fBmnCSCHitsArray->GetEntriesFast()])
410 BmnCSCHit(0, TVector3(x, y, z), TVector3(x_err, y_err, z_err), RefMCIndex);
411
412 BmnCSCHit* hit = (BmnCSCHit*)fBmnCSCHitsArray->At(fBmnCSCHitsArray->GetEntriesFast() - 1);
413 hit->SetStation(iStation);
414 hit->SetModule(iModule);
415 hit->SetIndex(fBmnCSCHitsArray->GetEntriesFast() - 1);
416
417 if (iPoint < NIntersectionPointsInModule) {
419 module->GetIntersectionPointDigitNumberMatch(iPoint)); // digit number match for the hit
420 hit->SetUpperClusterIndex(module->GetUpperCluster(iPoint).GetUniqueID());
421 hit->SetLowerClusterIndex(module->GetLowerCluster(iPoint).GetUniqueID());
422 hit->SetPseudo(false);
423 } else {
424 hit->SetDigitNumberMatch(module->GetPseudoIntersectionDigitNumberMatch(
425 iPoint - NIntersectionPointsInModule)); // digit number match for the hit
427 module->GetUpperCluster_PseudoIntersections(iPoint - NIntersectionPointsInModule)
428 .GetUniqueID());
430 module->GetLowerCluster_PseudoIntersections(iPoint - NIntersectionPointsInModule)
431 .GetUniqueID());
432 hit->SetPseudo(true);
433 }
434
435 if (fVerbose) {
436 cout << " glob(x:y:z) = ( " << x << " : " << y << " : " << z << "\n";
437 cout << " hit(x:y:z) = ( " << hit->GetX() << " : " << hit->GetY() << " : " << hit->GetZ() << "\n";
438 cout << "\n";
439 }
440 /*
441 StripCluster ucls = module->GetUpperCluster(iPoint);
442 StripCluster lcls = module->GetLowerCluster(iPoint);
443 ucls.SetModule(iModule);
444 lcls.SetModule(iModule);
445 ucls.SetStation(iStation);
446 lcls.SetStation(iStation);
447 UniqueUpperClusters[ucls.GetUniqueID()] = ucls;
448 UniqueLowerClusters[lcls.GetUniqueID()] = lcls;
449 hit->SetUpperClusterIndex(ucls.GetUniqueID());
450 hit->SetLowerClusterIndex(lcls.GetUniqueID());
451 */
452 if (fHitMatching) {
453 // For future update
454
455 /*BmnMatch digiMatch;
456 Int_t idx0, idx1;
457 if(iPoint < NIntersectionPointsInModule) {
458 digiMatch = module->GetIntersectionPointDigitNumberMatch(iPoint);
459 idx0 = digiMatch.GetLink(0).GetIndex();
460 idx1 = digiMatch.GetLink(1).GetIndex();
461 }
462 else {
463 digiMatch = module->GetPseudoIntersectionDigitNumberMatch(iPoint-NIntersectionPointsInModule);
464 idx0 = digiMatch.GetLink(0).GetIndex();
465 idx1 = digiMatch.GetLink(0).GetIndex();
466 }
467 BmnMatch* digiMatch0 = (BmnMatch*)fBmnCSCDigitMatchesArray->At(idx0);
468 BmnMatch* digiMatch1 = (BmnMatch*)fBmnCSCDigitMatchesArray->At(idx1);
469
470 Bool_t hitOk = kFALSE;
471 for (Int_t ilink = 0; ilink < digiMatch0->GetNofLinks(); ilink++) {
472 Int_t iindex = digiMatch0->GetLink(ilink).GetIndex();
473 for (Int_t jlink = 0; jlink < digiMatch1->GetNofLinks(); jlink++) {
474 Int_t jindex = digiMatch1->GetLink(jlink).GetIndex();
475 if (iindex == jindex) {
476 hitOk = kTRUE;
477 break;
478 }
479 }
480 if (hitOk) break;
481 }
482
483 hit->SetType(hitOk);
484 if (!hitOk) hit->SetRefIndex(-1);*/
485 //--------------------------------------------------------------
486
487 // hit MC-matching ----------------------------------------------
488 FairRootManager::Instance()->SetUseFairLinks(kTRUE);
489 BmnMatch hitMatch;
490 if (iPoint < NIntersectionPointsInModule) {
491 hitMatch = module->GetIntersectionPointMatch(iPoint);
492 } else {
493 hitMatch = module->GetPseudoIntersectionMatch(iPoint - NIntersectionPointsInModule);
494 }
495
496 for (BmnLink lnk : hitMatch.GetLinks())
497 hit->AddLink(FairLink(-1, lnk.GetIndex(), lnk.GetWeight()));
498 FairRootManager::Instance()->SetUseFairLinks(kFALSE);
499 }
500 //--------------------------------------------------------------
501 }
502 }
503 }
504 /*
505 for (auto it : UniqueUpperClusters) {
506 for (Int_t i = 0; i < fBmnCSCHitsArray->GetEntriesFast(); i++) {
507 BmnCSCHit* hit = (BmnCSCHit*) fBmnCSCHitsArray->At(i);
508 if (hit->GetUpperClusterIndex() != it.first) continue;
509 hit->SetUpperClusterIndex(fBmnCSCUpperClustersArray->GetEntriesFast());
510 }
511 it.second.SetUniqueID(fBmnCSCUpperClustersArray->GetEntriesFast());
512 new ((*fBmnCSCUpperClustersArray)[fBmnCSCUpperClustersArray->GetEntriesFast()]) StripCluster(it.second);
513 }
514 for (auto it : UniqueLowerClusters) {
515 for (Int_t i = 0; i < fBmnCSCHitsArray->GetEntriesFast(); i++) {
516 BmnCSCHit* hit = (BmnCSCHit*) fBmnCSCHitsArray->At(i);
517 if (hit->GetLowerClusterIndex() != it.first) continue;
518 hit->SetLowerClusterIndex(fBmnCSCLowerClustersArray->GetEntriesFast());
519 }
520 it.second.SetUniqueID(fBmnCSCLowerClustersArray->GetEntriesFast());
521 new ((*fBmnCSCLowerClustersArray)[fBmnCSCLowerClustersArray->GetEntriesFast()]) StripCluster(it.second);
522 }
523 */
524 if (fVerbose > 1)
525 cout << " N clear matches with MC-points = " << clear_matched_points_cnt << "\n";
526 //------------------------------------------------------------------------------
527 StationSet->Reset();
528}
529
530void BmnCSCHitMaker::OnlineWrite(const std::unique_ptr<TTree>& resultTree)
531{
532 if (!IsActive())
533 return;
534
535 resultTree->Branch(fOutputHitsBranchName, &fBmnCSCHitsArray);
536 resultTree->Branch("BmnCSCUpperCluster", &fBmnCSCUpperClustersArray);
537 resultTree->Branch("BmnCSCLowerCluster", &fBmnCSCLowerClustersArray);
538 resultTree->Fill();
539}
540
542{
543 if (StationSet) {
544 delete StationSet;
545 StationSet = nullptr;
546 }
547
548 if (TransfSet) {
549 delete TransfSet;
550 TransfSet = nullptr;
551 }
552
553 printf("Work time of BmnCSCHitMaker: %4.2f sec.\n", workTime);
554}
@ UpperStripLayer
virtual InitStatus Init()
virtual void OnlineWrite(const std::unique_ptr< TTree > &resultTree)
Write task resul to tree.
virtual ~BmnCSCHitMaker()
virtual void Finish()
virtual void Exec(Option_t *opt)
static void SetLowerUniqueID(Int_t id)
static void SetUpperUniqueID(Int_t id)
vector< StripCluster > GetStripClusters()
BmnCSCStation * GetStation(Int_t station_num)
Int_t CountNProcessedPointsInDetector()
Int_t GetNModules()
Bool_t LoadFromXMLFile(TString xml_config_file)
Plane3D::Point ApplyTransforms(Plane3D::Point point, Int_t station, Int_t module)
void SetModule(Int_t mod)
Definition BmnHit.h:73
void SetUpperClusterIndex(Int_t idx)
Definition BmnHit.h:133
void SetDigitNumberMatch(BmnMatch match)
Definition BmnHit.h:121
void SetIndex(Int_t id)
Definition BmnHit.h:57
void SetPseudo(Bool_t pseudo)
Definition BmnHit.h:141
void SetStation(Short_t st)
Definition BmnHit.h:69
void SetLowerClusterIndex(Int_t idx)
Definition BmnHit.h:137
const BmnLink & GetLink(Int_t i) const
Definition BmnMatch.h:37
void AddLink(const BmnMatch &match)
Definition BmnMatch.cxx:43
const vector< BmnLink > & GetLinks() const
Definition BmnMatch.h:38
Int_t GetNofLinks() const
Definition BmnMatch.h:40
Bool_t IsGoodDigit()
Int_t GetStripNumber()
Int_t GetStripLayer()
Int_t GetStation()
Double_t GetStripSignal()
void SetStation(Int_t st)
Int_t GetType()
void SetModule(Int_t m)