Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MvtxAlign.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file MvtxAlign.C
1 #include "MvtxAlign.h"
2 
3 #include <mvtx/MvtxDefUtil.h>
4 
7 
8 
10 #include <phool/PHCompositeNode.h>
11 #include <phool/PHIODataNode.h>
12 #include <phool/PHNodeIterator.h>
13 #include <phool/getClass.h>
14 #include <phool/recoConsts.h>
15 
16 #include <iostream>
17 #include <fstream>
18 #include <stdio.h>
19 
20 using namespace std;
21 
22 MvtxAlign::MvtxAlign(const string &name ) :
23  SubsysReco(name),
24  clusters_(NULL),
25  fdir_(""),
26  runnumber_(0),
27  apff_(true),
28  _timer(PHTimeServer::get()->insert_new(name))
29 {
30 
31 }
32 
34 {
35  // get the run number
36  if ( apff_ )
37  {
39  runnumber_ = rc->get_IntFlag("RUNNUMBER");
40  cout << PHWHERE << " Runnumber: " << runnumber_ << endl;
42  }
43 
45 }
46 
48 {
49 
50  _timer.get()->restart();
51 
52  //------
53  //--- get cluster container
54  //------
55  clusters_ = findNode::getClass<TrkrClusterContainer>(topNode, "TrkrClusterContainer");
56  if (!clusters_)
57  {
58  cout << PHWHERE << "ERROR: Can't find node TrkrClusterContainer" << endl;
60  }
61 
62  //------
63  //-- shift clusters
64  //------
65  TrkrDefUtil util;
66 
67  TrkrClusterContainer::ConstRange clusrange = clusters_->GetClusters();
68  for ( TrkrClusterContainer::ConstIterator iter = clusrange.first;
69  iter != clusrange.second;
70  ++iter)
71  {
72 
73  // get the key and check if we need to realign this cluster
74  TrkrDefs::cluskey ckey = (iter->second)->GetClusKey();
75  TrkrDefs::hitsetkey hkey = util.GetHitSetKeyFromClusKey(ckey);
76  auto aligniter = alignmap_.find(hkey);
77 
78  if ( aligniter != alignmap_.end() )
79  {
80  // get a non-const pointer to the cluster
81  TrkrClusterv1* clus = static_cast<TrkrClusterv1*>(clusters_->FindCluster(ckey));
82 
83  if ( verbosity > 1 )
84  {
85  cout << " applying alignment to " << endl;
86  clus->identify();
87  }
88 
89  // apply the alignment
90  clus->SetX(clus->GetX() + (aligniter->second).dx);
91  clus->SetY(clus->GetY() + (aligniter->second).dy);
92  clus->SetZ(clus->GetZ() + (aligniter->second).dz);
93 
94  if ( verbosity > 1 )
95  clus->identify();
96  }
97 
98  }
99 
100 
101  //------
102  //--- done
103  //------
104  _timer.get()->stop();
106 }
107 
108 
109 void MvtxAlign::AddAlignmentPar(TrkrDefs::hitsetkey key, double dx, double dy, double dz)
110 {
111 
112  // check that the misalignment doesn't already exist
113  auto iter = alignmap_.find(key);
114  if ( iter != alignmap_.end() )
115  {
116  cout << PHWHERE << " WARNING: overwriting existing misalignment for"
117  << " key:0x" << hex << key << dec << endl;
118 
119  (iter->second).dx = dx;
120  (iter->second).dy = dy;
121  (iter->second).dz = dz;
122  }
123  else
124  {
126  mis.dx = dx;
127  mis.dy = dy;
128  mis.dz = dz;
129 
130  alignmap_.insert(make_pair(key, mis));
131 
132  if ( verbosity > 0 )
133  {
134  cout << PHWHERE << " Added alignment pars:"
135  << " key:0x" << hex << key << dec
136  << " dx:" << dx
137  << " dy:" << dy
138  << " dz:" << dz
139  << endl;
140  }
141  }
142 
143 }
144 
145 
146 
147 void MvtxAlign::PrintAlignmentPars(std::ostream& os) const
148 {
149  os << "=============================================================" << endl;
150  os << "== " << PHWHERE << "==" << endl;
151  os << "=============================================================" << endl;
152 
153  for ( auto iter = alignmap_.begin();
154  iter != alignmap_.end();
155  ++iter)
156  {
157  os << " key:0x" << hex << iter->first << dec
158  << " dx:" << (iter->second).dx
159  << " dy:" << (iter->second).dy
160  << " dz:" << (iter->second).dz
161  << endl;
162  }
163 
164  os << "=============================================================" << endl;
165 }
166 
167 
169 {
170 
171  char fname[500];
172  sprintf(fname, "%sbeamcenter_%08d.txt", fdir_.c_str(), runnumber_);
173  ifstream fin;
174  fin.open(fname);
175  if ( fin.is_open() )
176  {
177  cout << PHWHERE << " Reading alignment parameters from " << fname << endl;
178  MvtxDefUtil util;
179 
180  string line;
181  static bool is_first = true;
182  float bc0_x = 0;
183  float bc0_y = 0;
184  float bc0_z = 0;
185  while ( getline(fin, line) )
186  {
187  int lyr, stave, chip;
188  float bcx, bcy, bcz;
189 
190  sscanf(line.c_str(), "%d %d %d %f %f %f", &lyr, &stave, &chip, &bcx, &bcy, &bcz);
191 
192  if ( is_first )
193  {
194  bc0_x = bcx;
195  bc0_y = bcy;
196  bc0_z = bcz;
197  is_first = false;
198  }
199 
200  double dx = bc0_x - bcx;
201  double dy = bc0_y - bcy;
202  double dz = bc0_z - bcz;
203 
205  util.GenHitSetKey(lyr, stave, chip),
206  double(dx), double(dy), double(dz));
207  }
208  }
209  else
210  {
211  cout << PHWHERE << "ERROR: Unable to open file " << fname << endl;
212  return 1;
213  }
214 
215  if ( verbosity > 0 )
217 
218  return 0;
219 }
220