File indexing completed on 2025-08-05 08:13:28
0001
0002
0003
0004
0005
0006
0007 #include "AnaMvtxPrototype1.h"
0008
0009 #include <trackbase/TrkrDefUtil.h>
0010 #include <trackbase/TrkrCluster.h>
0011 #include <trackbase/TrkrClusterContainer.h>
0012
0013 #include <mvtxprototype1/MvtxStandaloneTracking.h>
0014
0015 #include <phool/getClass.h>
0016 #include <phool/phool.h>
0017 #include <phool/PHCompositeNode.h>
0018 #include <phool/PHIODataNode.h>
0019 #include <phool/PHNodeIterator.h>
0020
0021 #include <fun4all/Fun4AllServer.h>
0022 #include <fun4all/Fun4AllReturnCodes.h>
0023 #include <fun4all/PHTFileServer.h>
0024
0025 #include <TTree.h>
0026 #include <TFile.h>
0027 #include <TLorentzVector.h>
0028 #include <TH1.h>
0029 #include <TH2.h>
0030 #include <TVectorD.h>
0031 #include <TMatrixD.h>
0032 #include <TDecompSVD.h>
0033
0034 #include <iostream>
0035 #include <map>
0036 #include <utility>
0037 #include <vector>
0038 #include <memory>
0039
0040
0041
0042 #define LogDebug(exp) std::cout<<"DEBUG: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
0043 #define LogError(exp) std::cout<<"ERROR: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
0044 #define LogWarning(exp) std::cout<<"WARNING: " <<__FILE__<<": "<<__LINE__<<": "<< exp <<"\n"
0045
0046
0047 AnaMvtxPrototype1::AnaMvtxPrototype1(const std::string &name,
0048 const std::string &ofName) :
0049 SubsysReco(name),
0050 clusters_(NULL),
0051 _foutname(ofName),
0052 _f(NULL),
0053 _ievent(0)
0054 {
0055
0056 hlayer = NULL;
0057 for (int il = 0; il < 4; il++)
0058 {
0059 hsize[il] = NULL;
0060 hsize_phi[il] = NULL;
0061 hsize_z[il] = NULL;
0062 hxz[il] = NULL;
0063 hdx[il] = NULL;
0064 hdz[il] = NULL;
0065 }
0066
0067 mvtxtracking_ = new MvtxStandaloneTracking();
0068 mvtxtracking_->SetWindowX(10);
0069 mvtxtracking_->SetWindowZ(10);
0070 mvtxtracking_->Verbosity(0);
0071 }
0072
0073 int AnaMvtxPrototype1::Init(PHCompositeNode *topNode)
0074 {
0075
0076
0077 _f = new TFile(_foutname.c_str(), "RECREATE");
0078
0079
0080 hlayer = new TH1D("hlayer", ";layer", 6, -0.5, 5.5);
0081
0082 for (int il = 0; il < 4; il++)
0083 {
0084 hsize[il] = new TH1D(Form("hsize_l%i", il),
0085 ";cluster size total [pixels]",
0086 100, -0.5, 99.5);
0087
0088 hsize_phi[il] = new TH1D(Form("hsize_phi_l%i", il),
0089 ";cluster size #phi [pixels]",
0090 100, -0.5, 99.5);
0091
0092 hsize_z[il] = new TH1D(Form("hsize_z_l%i", il),
0093 ";cluster size z [pixels]",
0094 100, -0.5, 100.5);
0095
0096 hxz[il] = new TH2D(Form("hxz_l%i", il),
0097 ";z;x",
0098 1024, -0.5, 1023.5,
0099 512, -0.5, 511.5);
0100
0101 hdx[il] = new TH1D(Form("hdx_l%i", il),
0102 "; dx [pixels]",
0103 2000, -500, 500);
0104
0105 hdz[il] = new TH1D(Form("hdz_l%i", il),
0106 "; dz [pixels]",
0107 2000, -500, 500);
0108 }
0109
0110
0111 htrk = new TH1D("htrk", ";trks / event", 16, -0.5, 15.5);
0112 htrk_cut = new TH1D("htrk_cut", ";trks / event", 16, -0.5, 15.5);
0113
0114 for (int il = 0; il < 4; il++)
0115 {
0116 htrk_dx[il] = new TH1D(Form("htrk_dx_l%i", il),
0117 ";track dx [pixels]",
0118 500, -25, 25);
0119
0120 htrk_dz[il] = new TH1D(Form("htrk_dz_l%i", il),
0121 ";track dz [pixels]",
0122 500, -25, 25);
0123
0124 htrk_cut_dx[il] = new TH1D(Form("htrk_cut_dx_l%i", il),
0125 ";track dx [pixels]",
0126 500, -25, 25);
0127
0128 htrk_cut_dz[il] = new TH1D(Form("htrk_cut_dz_l%i", il),
0129 ";track dz [pixels]",
0130 500, -25, 25);
0131
0132 }
0133 htrk_chi2xy = new TH1D("htrk_chi2xy",
0134 ";track chi2/ndf in x vs y",
0135 500, 0, 100);
0136 htrk_chi2zy = new TH1D("htrk_chi2zy",
0137 ";track chi2/ndf in z vs y",
0138 500, 0, 100);
0139
0140 htrk_cut_chi2xy = new TH1D("htrk_cut_chi2xy",
0141 ";track chi2/ndf in x vs y",
0142 500, 0, 100);
0143 htrk_cut_chi2zy = new TH1D("htrk_cut_chi2zy",
0144 ";track chi2/ndf in z vs y",
0145 500, 0, 100);
0146
0147 return 0;
0148
0149 }
0150
0151 int AnaMvtxPrototype1::InitRun(PHCompositeNode *topNode)
0152 {
0153
0154
0155 _ievent = -1;
0156
0157
0158 return 0;
0159
0160 }
0161
0162 int AnaMvtxPrototype1::process_event(PHCompositeNode *topNode)
0163 {
0164
0165 if ( verbosity > VERBOSITY_MORE )
0166 std::cout << "AnaMvtxPrototype1::process_event()" << std::endl;
0167
0168
0169 _ievent++;
0170
0171
0172
0173
0174 int retval = GetNodes(topNode);
0175 if ( retval != Fun4AllReturnCodes::EVENT_OK )
0176 return retval;
0177
0178 if ( verbosity > VERBOSITY_MORE )
0179 {
0180 std::cout << "-- evnt:" << _ievent << std::endl;
0181 std::cout << "-- Nclus:" << clusters_->size() << std::endl;
0182 }
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193 if ( verbosity > VERBOSITY_MORE )
0194 std::cout << "-- Looping over clusters" << std::endl;
0195
0196 LyrClusMap _mmap_lyr_clus;
0197
0198 TrkrDefUtil trkrutil;
0199
0200 TrkrClusterContainer::ConstRange clusrange = clusters_->GetClusters();
0201 for (TrkrClusterContainer::ConstIterator iter = clusrange.first;
0202 iter != clusrange.second;
0203 ++iter)
0204 {
0205 TrkrCluster *clus = iter->second;
0206
0207
0208 TrkrDefs::cluskey ckey = clus->GetClusKey();
0209
0210 int lyr = trkrutil.GetLayer(ckey);
0211
0212 hlayer->Fill(lyr);
0213
0214 if (lyr < 0 || lyr > 3)
0215 continue;
0216 hsize[lyr]->Fill(clus->GetAdc());
0217 hsize_phi[lyr]->Fill(clus->GetPhiSize());
0218 hsize_z[lyr]->Fill(clus->GetZSize());
0219 hxz[lyr]->Fill(clus->GetZ(), clus->GetX());
0220
0221
0222 _mmap_lyr_clus.insert(std::make_pair(lyr, clus));
0223 }
0224
0225 if ( verbosity > VERBOSITY_MORE )
0226 std::cout << "-- _mmap_lyr_clus.size():" << _mmap_lyr_clus.size() << std::endl;
0227
0228
0229
0230
0231
0232 if ( verbosity > VERBOSITY_MORE )
0233 std::cout << "-- Performing simple tracking" << std::endl;
0234
0235 static bool check_range = true;
0236 if ( check_range )
0237 {
0238 TrkrDefs::cluskey mvtxl = trkrutil.GetClusKeyLo(TrkrDefs::TRKRID::mvtx_id);
0239 TrkrDefs::cluskey mvtxh = trkrutil.GetClusKeyHi(TrkrDefs::TRKRID::mvtx_id);
0240
0241 std::cout << PHWHERE << " Mvtx key range: 0x" << std::hex << mvtxl << " - 0x" << mvtxh << std::dec << std::endl;
0242
0243 for (int i = 0; i < 4; i++)
0244 {
0245 TrkrDefs::cluskey lyrl = trkrutil.GetClusKeyLo(TrkrDefs::TRKRID::mvtx_id, i);
0246 TrkrDefs::cluskey lyrh = trkrutil.GetClusKeyHi(TrkrDefs::TRKRID::mvtx_id, i);
0247
0248 std::cout << PHWHERE << " Mvtx key range lyr " << i << ": 0x" << std::hex << lyrl << " - 0x" << lyrh << std::dec << std::endl;
0249 }
0250
0251 check_range = false;
0252 }
0253
0254
0255
0256 TrkrClusterContainer::ConstRange lyr0range = clusters_->GetClusters(TrkrDefs::TRKRID::mvtx_id, 0);
0257 for ( TrkrClusterContainer::ConstIterator lyr0_itr = lyr0range.first;
0258 lyr0_itr != lyr0range.second;
0259 ++lyr0_itr)
0260 {
0261 TrkrCluster* clus0 = lyr0_itr->second;
0262
0263 if ( !clus0 )
0264 {
0265 std::cout << "WARNING: clus0 not found" << std::endl;
0266 continue;
0267 }
0268
0269
0270 int lyr = trkrutil.GetLayer(clus0->GetClusKey());
0271 if ( lyr != 0 )
0272 {
0273 std::cout << PHWHERE << "ERRORL: Expected clusters from layer 0 only. Found lyr=" << lyr << std::endl;
0274 continue;
0275 }
0276
0277
0278
0279 auto misiter = _misalign.find(0);
0280
0281 double x0 = misiter != _misalign.end() ? clus0->GetX() + (misiter->second).dx : clus0->GetX();
0282 double y0 = misiter != _misalign.end() ? clus0->GetY() + (misiter->second).dy : clus0->GetY();
0283 double z0 = misiter != _misalign.end() ? clus0->GetZ() + (misiter->second).dz : clus0->GetZ();
0284
0285 if ( verbosity > VERBOSITY_MORE )
0286 {
0287 std::cout << "-- clus0:(" << x0 << ", " << y0 << ", " << z0 << ")" << std::endl;
0288 }
0289
0290
0291 TrkrClusterContainer::ConstRange lyr3range = clusters_->GetClusters(TrkrDefs::TRKRID::mvtx_id, 3);
0292 for ( TrkrClusterContainer::ConstIterator lyr3_itr = lyr3range.first;
0293 lyr3_itr != lyr3range.second;
0294 ++lyr3_itr)
0295 {
0296 TrkrCluster* clus3 = lyr3_itr->second;
0297
0298 if ( !clus3 )
0299 {
0300 std::cout << "WARNING: clus3 not found" << std::endl;
0301 continue;
0302 }
0303
0304 auto misiter = _misalign.find(3);
0305
0306 double x3 = misiter != _misalign.end() ? clus3->GetX() + (misiter->second).dx : clus3->GetX();
0307 double y3 = misiter != _misalign.end() ? clus3->GetY() + (misiter->second).dy : clus3->GetY();
0308 double z3 = misiter != _misalign.end() ? clus3->GetZ() + (misiter->second).dz : clus3->GetZ();
0309
0310
0311
0312 double mpr = CalcSlope(y0, x0, y3, x3);
0313 double bpr = CalcIntecept(y0, x0, mpr);
0314
0315
0316 double mzr = CalcSlope(y0, z0, y3, z3);
0317 double bzr = CalcIntecept(y0, z0, mzr);
0318
0319
0320 if ( verbosity > VERBOSITY_MORE )
0321 {
0322 std::cout << "-- clus3:(" << x3 << ", " << y3 << ", " << z3 << ")" << std::endl;
0323 std::cout << "-- mpr:" << mpr << " bpr:" << bpr << std::endl;
0324 std::cout << "-- mzr:" << mzr << " bzr:" << bzr << std::endl;
0325 }
0326
0327
0328
0329
0330 for (int ilyr = 1; ilyr <= 2; ilyr++)
0331 {
0332 TrkrClusterContainer::ConstRange lyrrange = clusters_->GetClusters(TrkrDefs::TRKRID::mvtx_id, ilyr);
0333 for ( TrkrClusterContainer::ConstIterator lyr_itr = lyrrange.first;
0334 lyr_itr != lyrrange.second;
0335 ++lyr_itr)
0336 {
0337 TrkrCluster* clus = lyr_itr->second;
0338
0339 if ( !clus )
0340 continue;
0341
0342 auto misiter = _misalign.find(ilyr);
0343
0344 double x = misiter != _misalign.end() ? clus->GetX() + (misiter->second).dx : clus->GetX();
0345 double y = misiter != _misalign.end() ? clus->GetY() + (misiter->second).dy : clus->GetY();
0346 double z = misiter != _misalign.end() ? clus->GetZ() + (misiter->second).dz : clus->GetZ();
0347
0348
0349
0350 double x_proj = CalcProjection(y, mpr, bpr);
0351 double z_proj = CalcProjection(y, mzr, bzr);
0352
0353
0354 double dx = (x_proj - x);
0355 double dz = (z_proj - z);
0356
0357 if ( !hdx[ilyr] || !hdz[ilyr] )
0358 {
0359 std::cout << "WARNING!! can't find hdphi or hdz for lyr:" << ilyr << std::endl;
0360 }
0361 else
0362 {
0363 hdx[ilyr]->Fill(dx);
0364 hdz[ilyr]->Fill(dz);
0365 }
0366
0367 if ( verbosity > VERBOSITY_MORE )
0368 {
0369 std::cout << "------------------" << std::endl;
0370 std::cout << "-- clus" << trkrutil.GetLayer(clus->GetClusKey()) << ":(" << x3 << ", " << y3 << ", " << z3 << ")" << std::endl;
0371 std::cout << "-- proj:(" << x_proj << ", " << z_proj << ")" << std::endl;
0372 std::cout << "-- dx [pixels]: " << dx << std::endl;
0373 std::cout << "-- dz [pixels]: " << dz << std::endl;
0374 std::cout << "------------------" << std::endl;
0375 }
0376
0377 }
0378 }
0379
0380 }
0381
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503 if ( verbosity > VERBOSITY_MORE )
0504 std::cout << "-- Done simple tracking" << std::endl;
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528 MvtxStandaloneTracking::MvtxTrackList tracklist;
0529
0530 std::vector<int> uselayers;
0531 uselayers.push_back(0);
0532 uselayers.push_back(1);
0533 uselayers.push_back(2);
0534 uselayers.push_back(3);
0535
0536 mvtxtracking_->RunTracking(topNode, tracklist, uselayers);
0537
0538 htrk->Fill(tracklist.size());
0539
0540 int ncut = 0;
0541 for ( unsigned int i = 0; i < tracklist.size(); i++)
0542 {
0543
0544 htrk_chi2xy->Fill(tracklist.at(i).chi2_xy);
0545 htrk_chi2zy->Fill(tracklist.at(i).chi2_zy);
0546
0547 if ( tracklist.at(i).chi2_xy < 5 && tracklist.at(i).chi2_zy < 5)
0548 {
0549 ncut++;
0550 htrk_cut_chi2xy->Fill(tracklist.at(i).chi2_xy);
0551 htrk_cut_chi2zy->Fill(tracklist.at(i).chi2_zy);
0552 }
0553
0554 for ( unsigned int j = 0; j < tracklist.at(i).ClusterList.size(); j++)
0555 {
0556 TrkrDefs::cluskey ckey = tracklist.at(i).ClusterList.at(j)->GetClusKey();
0557
0558 int lyr = trkrutil.GetLayer(ckey);
0559
0560 if ( lyr < 0 || lyr >= 4 )
0561 {
0562 std::cout << PHWHERE << " WARNING: bad layer from track cluster. lyr:" << lyr << std::endl;
0563 continue;
0564 }
0565
0566 htrk_dx[lyr]->Fill(tracklist.at(i).dx.at(j));
0567 htrk_dz[lyr]->Fill(tracklist.at(i).dz.at(j));
0568
0569 if ( tracklist.at(i).chi2_xy < 5 && tracklist.at(i).chi2_zy < 5)
0570 {
0571 htrk_cut_dx[lyr]->Fill(tracklist.at(i).dx.at(j));
0572 htrk_cut_dz[lyr]->Fill(tracklist.at(i).dz.at(j));
0573 }
0574 }
0575
0576
0577 }
0578
0579 htrk_cut->Fill(ncut);
0580
0581
0582
0583
0584
0585 return 0;
0586
0587 }
0588
0589 int AnaMvtxPrototype1::End(PHCompositeNode * topNode)
0590 {
0591
0592 PrintMisalign();
0593
0594 _f->Write();
0595 _f->Close();
0596
0597 return 0;
0598 }
0599
0600
0601
0602
0603 int AnaMvtxPrototype1::GetNodes(PHCompositeNode * topNode)
0604 {
0605
0606
0607 clusters_ = findNode::getClass<TrkrClusterContainer>(topNode, "TrkrClusterContainer");
0608 if (!clusters_ && _ievent < 2)
0609 {
0610 std::cout << PHWHERE << " TrkrClusterContainer node not found on node tree"
0611 << std::endl;
0612 return Fun4AllReturnCodes::ABORTEVENT;
0613 }
0614
0615 return Fun4AllReturnCodes::EVENT_OK;
0616
0617 }
0618
0619
0620
0621
0622 double
0623 AnaMvtxPrototype1::CalcSlope(double x0, double y0, double x1, double y1)
0624 {
0625 return (y1 - y0) / (x1 - x0);
0626 }
0627
0628
0629
0630
0631 double
0632 AnaMvtxPrototype1::CalcIntecept(double x0, double y0, double m)
0633 {
0634 return y0 - x0 * m;
0635 }
0636
0637
0638
0639
0640 double
0641 AnaMvtxPrototype1::CalcProjection(double x, double m, double b)
0642 {
0643 return m * x + b;
0644 }
0645
0646
0647
0648
0649 void
0650 AnaMvtxPrototype1::MisalignLayer(int lyr, double dx, double dy, double dz)
0651 {
0652 mis align;
0653 align.dx = dx;
0654 align.dy = dy;
0655 align.dz = dz;
0656
0657 _misalign.insert( std::make_pair(lyr, align) );
0658 }
0659
0660
0661
0662
0663 void
0664 AnaMvtxPrototype1::Misalign()
0665 {
0666
0667 if ( _misalign.size() < 1 )
0668 return;
0669
0670 TrkrClusterContainer::ConstRange clusrange = clusters_->GetClusters();
0671 for (TrkrClusterContainer::ConstIterator iter = clusrange.first;
0672 iter != clusrange.second;
0673 ++iter)
0674 {
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725
0726
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737 }
0738 }
0739
0740
0741
0742
0743 void
0744 AnaMvtxPrototype1::PrintMisalign()
0745 {
0746 std::cout << "================================" << std::endl;
0747 std::cout << "== " << PHWHERE << std::endl;
0748
0749 for (auto iter = _misalign.begin(); iter != _misalign.end(); ++iter)
0750 {
0751 std::cout << "== Lyr: " << iter->first
0752 << " dx[cm]:" << (iter->second).dx
0753 << " dy[cm]:" << (iter->second).dy
0754 << " dz[cm]:" << (iter->second).dz
0755 << std::endl;
0756 }
0757
0758 std::cout << "================================" << std::endl;
0759 }
0760