Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:21:59

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