File indexing completed on 2026-05-23 08:11:51
0001 #ifndef __CHARGE_RECALC_H__
0002 #define __CHARGE_RECALC_H__
0003
0004 #include <fun4all/SubsysReco.h>
0005 #include <fun4all/Fun4AllReturnCodes.h>
0006
0007 #include <phool/PHCompositeNode.h>
0008 #include <phool/getClass.h>
0009
0010 #include <trackbase/ActsGeometry.h>
0011 #include <trackbase/TrkrClusterContainer.h>
0012
0013 #include <trackbase_historic/SvtxTrackMap.h>
0014 #include <trackbase_historic/SvtxTrack.h>
0015 #include <trackbase_historic/TrackSeed.h> // explicit (avoid relying on indirect includes)
0016
0017 #include <globalvertex/SvtxVertexMap.h>
0018 #include <globalvertex/SvtxVertex.h>
0019
0020 #include <cmath>
0021 #include <limits>
0022 #include <iostream>
0023
0024 static inline float WrapPhi(float x)
0025 {
0026 while (x > M_PI)
0027 x -= 2.0f * M_PI;
0028 while (x < -M_PI)
0029 x += 2.0f * M_PI;
0030 return x;
0031 }
0032
0033 static inline float DeltaPhi(float a, float b)
0034 {
0035 return WrapPhi(a - b);
0036 }
0037
0038 class ChargeRecalc : public SubsysReco
0039 {
0040 public:
0041 ChargeRecalc(const std::string &name = "ChargeRecalc")
0042 : SubsysReco(name) {}
0043 void setVerbosity(int v) { m_verb = v; }
0044
0045 int process_event(PHCompositeNode *topNode) override
0046 {
0047 auto clustermap = findNode::getClass<TrkrClusterContainer>(topNode, m_clusterContainerName);
0048 auto geometry = findNode::getClass<ActsGeometry>(topNode, m_actsgeometryName);
0049 auto vertexmap = findNode::getClass<SvtxVertexMap>(topNode, m_vertexMapName);
0050 auto trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_trackMapName);
0051 if (!trackmap)
0052 {
0053 std::cout << PHWHERE << "Missing trackmap, can't continue" << std::endl;
0054 return Fun4AllReturnCodes::EVENT_OK;
0055 }
0056 if (!clustermap)
0057 {
0058 std::cout << PHWHERE << "Missing clustermap, can't continue" << std::endl;
0059 return Fun4AllReturnCodes::EVENT_OK;
0060 }
0061 if (!geometry)
0062 {
0063 std::cout << PHWHERE << "Missing geometry, can't continue" << std::endl;
0064 return Fun4AllReturnCodes::EVENT_OK;
0065 }
0066
0067
0068
0069
0070
0071
0072 if (vertexmap && !vertexmap->empty())
0073 {
0074 std::cout<<"Ntrk: "<<trackmap->size()<<", Nvtx: "<<vertexmap->size()<<" "<<std::endl;
0075 int ivtx=0;
0076 for (const auto &[k, v] : *vertexmap)
0077 {
0078 if (v){
0079 std::cout<<"vtx : "<< ivtx <<" "
0080 << v->get_id() <<" "
0081 << v->get_x() <<" "
0082 << v->get_y() <<" "
0083 << v->get_z() <<" "
0084 << v->get_beam_crossing() <<std::endl;
0085 }
0086 ivtx++;
0087 }
0088
0089
0090
0091
0092
0093
0094
0095
0096 }
0097
0098
0099
0100
0101
0102
0103
0104 int n_changed = 0;
0105 int n_skipped = 0;
0106 int n_total = 0;
0107
0108 for (auto &iter : *trackmap)
0109 {
0110 SvtxTrack *track = iter.second;
0111 if (!track)
0112 continue;
0113 n_total++;
0114
0115 int vertexid = track->get_vertex_id();
0116 std::cout<<"vertexID : "<<vertexid<<std::endl;
0117
0118 TrackSeed *si_seed = track->get_silicon_seed();
0119 if (!si_seed)
0120 {
0121 n_skipped++;
0122 continue;
0123 }
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 float phi_rmin = 0.0f;
0172 float phi_rmax = 0.0f;
0173
0174 std::vector< Acts::Vector3 > vClus;
0175 for (auto key_iter = si_seed->begin_cluster_keys(); key_iter != si_seed->end_cluster_keys(); ++key_iter)
0176 {
0177 const auto &cluster_key = *key_iter;
0178 auto trkrCluster = clustermap->findCluster(cluster_key);
0179 if (!trkrCluster)
0180 continue;
0181
0182 int layer = TrkrDefs::getLayer(cluster_key);
0183 auto global = geometry->getGlobalPosition(cluster_key, trkrCluster);
0184
0185 vClus.push_back(global);
0186
0187 float clus_r = sqrt(global[0]*global[0] + global[1]*global[1]);
0188 std::cout<<"clus : "<<layer<<" "<<global[0]<<" "<<global[1]<<" "<<clus_r<<std::endl;
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206 }
0207
0208
0209
0210
0211
0212
0213
0214
0215 if(vClus.size()<3) { std::cout<<"Ncluster is "<<vClus.size()<<" <= 3. skip"<<std::endl; continue; }
0216
0217 phi_rmin = atan2(vClus[1][1]-vClus[0][1], vClus[1][0]-vClus[0][0]);
0218 phi_rmax = atan2(vClus[vClus.size()-1][1]-vClus[0][1], vClus[vClus.size()-1][0]-vClus[0][0]);
0219
0220 const float dphi = DeltaPhi(phi_rmax, phi_rmin);
0221 const int q_new = (dphi > 0.0f) ? -1 : +1;
0222 const int q_old = track->get_charge();
0223
0224 if (q_new != q_old)
0225 {
0226 if (m_verb > 2)
0227 {
0228 std::cout << "[ChargeRecalc] trkid=" << track->get_id()
0229 << " before=" << track->get_charge();
0230 }
0231 track->set_charge(q_new);
0232 si_seed->set_qOverR(q_new * fabs(si_seed->get_qOverR()));
0233 if (m_verb > 2)
0234 {
0235 std::cout << " after=" << track->get_charge() << std::endl;
0236 }
0237 n_changed++;
0238 }
0239 }
0240 if (m_verb > 1)
0241 {
0242 std::cout << "[ChargeRecalc] total=" << n_total
0243 << " changed=" << n_changed
0244 << " skipped=" << n_skipped
0245 << std::endl;
0246 }
0247 return Fun4AllReturnCodes::EVENT_OK;
0248 }
0249
0250 private:
0251
0252 std::string m_clusterContainerName = "TRKR_CLUSTER";
0253 std::string m_actsgeometryName = "ActsGeometry";
0254 std::string m_trackMapName = "SvtxTrackMap";
0255 std::string m_vertexMapName = "SvtxVertexMap";
0256
0257 int m_verb = 0;
0258 };
0259
0260 #endif