File indexing completed on 2025-08-06 08:21:59
0001 #include "MvtxPrototype2Align.h"
0002
0003 #include <mvtx/MvtxDefs.h>
0004 #include "mvtx/SegmentationAlpide.h"
0005
0006 #include <trackbase/TrkrClusterContainer.h>
0007 #include <trackbase/TrkrClusterv1.h>
0008
0009
0010 #include <fun4all/Fun4AllReturnCodes.h>
0011 #include <phool/PHCompositeNode.h>
0012 #include <phool/PHIODataNode.h>
0013 #include <phool/PHNodeIterator.h>
0014 #include <phool/getClass.h>
0015 #include <phool/recoConsts.h>
0016
0017 #include <iostream>
0018 #include <fstream>
0019 #include <stdio.h>
0020
0021 using namespace std;
0022
0023 MvtxPrototype2Align::MvtxPrototype2Align(const string &name ) :
0024 SubsysReco(name),
0025 clusters_(NULL),
0026 fdir_(""),
0027 runnumber_(0),
0028 apff_(true),
0029 m_afname(""),
0030 m_is_global(true),
0031 _timer(PHTimeServer::get()->insert_new(name))
0032 {
0033
0034 }
0035
0036 int MvtxPrototype2Align::InitRun(PHCompositeNode* topNode)
0037 {
0038
0039 if ( apff_ )
0040 {
0041 recoConsts *rc = recoConsts::instance();
0042 runnumber_ = rc->get_IntFlag("RUNNUMBER");
0043 cout << PHWHERE << " Runnumber: " << runnumber_ << endl;
0044 ReadAlignmentParFile();
0045 }
0046
0047 return Fun4AllReturnCodes::EVENT_OK;
0048 }
0049
0050 int MvtxPrototype2Align::process_event(PHCompositeNode *topNode)
0051 {
0052
0053 _timer.get()->restart();
0054
0055
0056
0057
0058 clusters_ = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER");
0059 if (!clusters_)
0060 {
0061 cout << PHWHERE << "ERROR: Can't find node TRKR_CLUSTER" << endl;
0062 return Fun4AllReturnCodes::ABORTRUN;
0063 }
0064
0065
0066
0067
0068 TrkrClusterContainer::ConstRange clusrange = clusters_->getClusters();
0069 for ( TrkrClusterContainer::ConstIterator iter = clusrange.first;
0070 iter != clusrange.second;
0071 ++iter)
0072 {
0073
0074
0075 TrkrDefs::cluskey ckey = (iter->second)->getClusKey();
0076 TrkrDefs::hitsetkey hkey = TrkrDefs::getHitSetKeyFromClusKey(ckey);
0077 auto aligniter = alignmap_.find(hkey);
0078
0079 if ( aligniter != alignmap_.end() )
0080 {
0081
0082 TrkrClusterv1* clus = static_cast<TrkrClusterv1*>(clusters_->findCluster(ckey));
0083
0084 if ( Verbosity() > 1 )
0085 {
0086 cout << " applying alignment to " << endl;
0087 clus->identify();
0088 }
0089
0090
0091 clus->setX(clus->getX() - (aligniter->second).dx);
0092 clus->setY(clus->getY() - (aligniter->second).dy);
0093 clus->setZ(clus->getZ() - (aligniter->second).dz);
0094
0095 if ( Verbosity() > 1 )
0096 clus->identify();
0097 }
0098
0099 }
0100
0101
0102
0103
0104
0105 _timer.get()->stop();
0106 return Fun4AllReturnCodes::EVENT_OK;
0107 }
0108
0109
0110 void MvtxPrototype2Align::AddAlignmentPar(TrkrDefs::hitsetkey key, double dx, double dy, double dz)
0111 {
0112
0113
0114 auto iter = alignmap_.find(key);
0115 if ( iter != alignmap_.end() )
0116 {
0117 cout << PHWHERE << " WARNING: overwriting existing misalignment for"
0118 << " key:0x" << hex << key << dec << endl;
0119
0120 (iter->second).dx = dx;
0121 (iter->second).dy = dy;
0122 (iter->second).dz = dz;
0123 }
0124 else
0125 {
0126 AlignmentPar mis;
0127 mis.dx = dx;
0128 mis.dy = dy;
0129 mis.dz = dz;
0130
0131 alignmap_.insert(make_pair(key, mis));
0132
0133 if ( Verbosity() > 0 )
0134 {
0135 cout << PHWHERE << " Added alignment pars:"
0136 << " key:0x" << hex << key << dec
0137 << " dx:" << dx
0138 << " dy:" << dy
0139 << " dz:" << dz
0140 << endl;
0141 }
0142 }
0143
0144 }
0145
0146
0147
0148 void MvtxPrototype2Align::PrintAlignmentPars(std::ostream& os) const
0149 {
0150 os << "=============================================================" << endl;
0151 os << "== " << PHWHERE << "==" << endl;
0152 os << "=============================================================" << endl;
0153
0154 for ( auto iter = alignmap_.begin();
0155 iter != alignmap_.end();
0156 ++iter)
0157 {
0158 os << " key:0x" << hex << iter->first << dec
0159 << " dx:" << (iter->second).dx
0160 << " dy:" << (iter->second).dy
0161 << " dz:" << (iter->second).dz
0162 << endl;
0163 }
0164
0165 os << "=============================================================" << endl;
0166 }
0167
0168
0169 int MvtxPrototype2Align::ReadAlignmentParFile()
0170 {
0171 string flname(fdir_);
0172 if ( m_afname.empty() )
0173 {
0174 char fname[100];
0175 sprintf(fname, "beamcenter_%08d.txt", runnumber_);
0176 flname += fname;
0177 }
0178 else
0179 {
0180 flname += m_afname;
0181 }
0182 ifstream fin;
0183 fin.open(flname);
0184 if ( fin.is_open() )
0185 {
0186 cout << PHWHERE << " Reading alignment parameters from " << flname << endl;
0187
0188
0189 string line;
0190
0191
0192
0193
0194 while ( getline(fin, line) )
0195 {
0196 int lyr, stave;
0197 float bcx, bcy, bcz;
0198
0199 sscanf(line.c_str(), "%d %d %f %f %f", &lyr, &stave, &bcx, &bcy, &bcz);
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 for ( int chip = 0; chip < 9; ++chip )
0214 {
0215 AddAlignmentPar(
0216 MvtxDefs::genHitSetKey(lyr, 0, chip),
0217 double(bcx), double(bcy), double(bcz));
0218 }
0219 }
0220 }
0221 else
0222 {
0223 cout << PHWHERE << "ERROR: Unable to open file " << flname << endl;
0224 return 1;
0225 }
0226
0227 if ( Verbosity() > 0 )
0228 PrintAlignmentPars();
0229
0230 return 0;
0231 }