Newer
Older
TB_Chris / TbAlgorithms / src / .svn / text-base / TbDUTMonitor.cpp.svn-base
  1. // AIDA
  2. #include "AIDA/IAxis.h"
  3.  
  4. // Gaudi
  5. #include "GaudiKernel/PhysicalConstants.h"
  6. #include "GaudiUtils/HistoLabels.h"
  7.  
  8. // Tb/TbEvent
  9. #include "Event/TbTrack.h"
  10. #include "Event/TbCluster.h"
  11.  
  12. // Tb/TbKernel
  13. #include "TbKernel/TbConstants.h"
  14. #include "TbKernel/TbModule.h"
  15.  
  16. // Local
  17. #include "TbDUTMonitor.h"
  18.  
  19. using namespace Gaudi::Utils::Histos;
  20.  
  21. DECLARE_ALGORITHM_FACTORY(TbDUTMonitor)
  22.  
  23. //=============================================================================
  24. // Standard constructor
  25. //=============================================================================
  26. TbDUTMonitor::TbDUTMonitor(const std::string& name, ISvcLocator* pSvcLocator)
  27. : TbAlgorithm(name, pSvcLocator),
  28. m_parResXY("", -0.2, 0.2, 200),
  29. m_parXY("", -20, 20, 500),
  30. m_parScanX("", 0, 0, 1),
  31. m_parScanY("", 0, 0, 1),
  32. m_parResTime("", -100., 100., 2000) {
  33.  
  34. declareProperty("TrackLocation",
  35. m_trackLocation = LHCb::TbTrackLocation::Default);
  36. declareProperty("DUTs", m_duts);
  37.  
  38. declareProperty("ScanX", m_parScanX);
  39. declareProperty("ScanY", m_parScanY);
  40. }
  41.  
  42. //=============================================================================
  43. // Initialization
  44. //=============================================================================
  45. StatusCode TbDUTMonitor::initialize() {
  46.  
  47. // Initialise the base class.
  48. StatusCode sc = TbAlgorithm::initialize();
  49. if (sc.isFailure()) return sc;
  50.  
  51. // Setup the histograms.
  52. const unsigned int nDuts = m_duts.size();
  53. for (unsigned int i = 0; i < nDuts; ++i) {
  54. m_index[m_duts[i]] = i;
  55. const std::string plane = std::to_string(m_duts[i]);
  56. const std::string title = geomSvc()->modules().at(m_duts[i])->id();
  57.  
  58. unsigned int bins = m_parResXY.bins();
  59. double low = m_parResXY.lowEdge();
  60. double high = m_parResXY.highEdge();
  61. std::string name = "ResidualsLocalX/Plane" + plane;
  62. m_hResLocalX.push_back(book1D(name, title, low, high, bins));
  63. name = "ResidualsLocalY/Plane" + plane;
  64. m_hResLocalY.push_back(book1D(name, title, low, high, bins));
  65. name = "ResidualsGlobalX/Plane" + plane;
  66. m_hResGlobalX.push_back(book1D(name, title, low, high, bins));
  67. name = "ResidualsGlobalY/Plane" + plane;
  68. m_hResGlobalY.push_back(book1D(name, title, low, high, bins));
  69. setAxisLabels(m_hResLocalX[i], "#it{x} - #it{x}_{track} [mm]", "entries");
  70. setAxisLabels(m_hResLocalY[i], "#it{y} - #it{y}_{track} [mm]", "entries");
  71.  
  72. unsigned int binsXY = m_parXY.bins();
  73. double lowXY = m_parXY.lowEdge();
  74. double highXY = m_parXY.highEdge();
  75.  
  76. m_hUnbiasedResGlobalXvsGlobalX.push_back(
  77. book2D("GlobalResXvsGlobalX/Plane" + plane, title, lowXY, highXY,
  78. binsXY, low, high, bins));
  79.  
  80. m_hUnbiasedResGlobalYvsGlobalY.push_back(
  81. book2D("GlobalResYvsGlobalY/Plane" + plane, title, lowXY, highXY,
  82. binsXY, low, high, bins));
  83.  
  84. m_hUnbiasedResGlobalXvsTrackChi2.push_back(
  85. book2D("GlobalResXvsTrackChi2/Plane" + plane, title, 0, 100, 1000, low,
  86. high, bins));
  87. m_hUnbiasedResGlobalYvsTrackChi2.push_back(
  88. book2D("GlobalResYvsTrackChi2/Plane" + plane, title, 0, 100, 1000, low,
  89. high, bins));
  90.  
  91. m_hUnbiasedResGlobalXvsTrackTx.push_back(
  92. book2D("GlobalResXvsTrackTx/Plane" + plane, title, -0.002, 0.002, 100,
  93. low, high, bins));
  94. m_hUnbiasedResGlobalXvsTrackTy.push_back(
  95. book2D("GlobalResXvsTrackTy/Plane" + plane, title, -0.002, 0.002, 100,
  96. low, high, bins));
  97. m_hUnbiasedResGlobalYvsTrackTx.push_back(
  98. book2D("GlobalResYvsTrackTx/Plane" + plane, title, -0.002, 0.002, 100,
  99. low, high, bins));
  100. m_hUnbiasedResGlobalYvsTrackTy.push_back(
  101. book2D("GlobalResYvsTrackTy/Plane" + plane, title, -0.002, 0.002, 100,
  102. low, high, bins));
  103.  
  104. m_hUnbiasedResGlobalXvsPixelX.push_back(
  105. book2D("UnbiasedResGlobalXvsPixelX/Plane" + plane, title, -1.0, 1.0,
  106. 200, low, high, bins));
  107.  
  108. m_hUnbiasedResGlobalXvsPixelY.push_back(
  109. book2D("UnbiasedResGlobalXvsPixelY/Plane" + plane, title, -1.0, 1.0,
  110. 200, low, high, bins));
  111.  
  112. m_hUnbiasedResGlobalYvsPixelX.push_back(
  113. book2D("UnbiasedResGlobalYvsPixelX/Plane" + plane, title, -1.0, 1.0,
  114. 200, low, high, bins));
  115.  
  116. m_hUnbiasedResGlobalYvsPixelY.push_back(
  117. book2D("UnbiasedResGlobalYvsPixelY/Plane" + plane, title, -1.0, 1.0,
  118. 200, low, high, bins));
  119.  
  120. m_hUnbiasedResGlobalXvsClusterSize.push_back(
  121. book2D("GlobalResXvsClusterSize/Plane" + plane, title, 0.5, 10.5, 10,
  122. low, high, bins));
  123. m_hUnbiasedResGlobalYvsClusterSize.push_back(
  124. book2D("GlobalResYvsClusterSize/Plane" + plane, title, 0.5, 10.5, 10,
  125. low, high, bins));
  126.  
  127. // m_UnbiasedResGlobalXvshUnbiasedResGlobalY
  128. m_UnbiasedResGlobalXvshUnbiasedResGlobalY.push_back(
  129. book2D("UnbiasedResGlobalXvshUnbiasedResGlobalY/Plane" + plane, title,
  130. low, high, bins, low, high, bins));
  131.  
  132. bins = m_parResTime.bins();
  133. low = m_parResTime.lowEdge();
  134. high = m_parResTime.highEdge();
  135. name = "ResidualsTime/Plane" + plane;
  136. m_hResTime.push_back(book1D(name, title, low, high, bins));
  137. setAxisLabels(m_hResTime[i], "#it{t} - #it{t}_{track} [ns]", "entries");
  138. }
  139. std::vector<std::string> labels = {"X", "Y", "Z", "rotX", "rotY", "rotZ"};
  140. unsigned int binsXY = m_parXY.bins();
  141. double lowXY = m_parXY.lowEdge();
  142. double highXY = m_parXY.highEdge();
  143. unsigned int bins = m_parResXY.bins();
  144. double low = m_parResXY.lowEdge();
  145. double high = m_parResXY.highEdge();
  146.  
  147. if (nDuts == 0) return sc;
  148. TbModule* mod = geomSvc()->module(m_duts[0]);
  149. std::vector<bool> xaxis(6, 0);
  150. std::vector<bool> yaxis(6, 0);
  151. std::vector<std::string>::iterator xL =
  152. std::find(labels.begin(), labels.end(), m_parScanX.title());
  153. std::vector<std::string>::iterator yL =
  154. std::find(labels.begin(), labels.end(), m_parScanY.title());
  155. if (xL != labels.end()) xaxis[xL - labels.begin()] = 1;
  156. if (yL != labels.end()) yaxis[yL - labels.begin()] = 1;
  157.  
  158. if (xL != labels.end()) {
  159. for (int i = 0; i < m_parScanX.bins(); ++i) {
  160. const double px = m_parScanX.lowEdge() +
  161. i * (m_parScanX.highEdge() - m_parScanX.lowEdge()) /
  162. m_parScanX.bins();
  163. std::string label =
  164. "Scan #Delta" + m_parScanX.title() + " = " + std::to_string(px);
  165. if (yL == labels.end()) {
  166. info() << "Booking scan " << *xL << " = " << px << endmsg;
  167. m_hScanXvsX.push_back(book2D("XvsX/Scan" + std::to_string(i), label,
  168. lowXY, highXY, binsXY, low, high, bins));
  169. m_hScanYvsY.push_back(book2D("YvsY/Scan" + std::to_string(i), label,
  170. lowXY, highXY, binsXY, low, high, bins));
  171. m_testModules.push_back(new TbModule());
  172. (*m_testModules.rbegin())->setAlignment(
  173. mod->x() + px * xaxis[0], mod->y() + px * xaxis[1],
  174. mod->z() + px * xaxis[2], mod->rotX() + px * xaxis[3],
  175. mod->rotY() + px * xaxis[4], mod->rotZ() + px * xaxis[5], 0, 0, 0,
  176. 0, 0, 0);
  177. } else {
  178. for (int j = 0; j < m_parScanY.bins(); ++j) {
  179.  
  180. const double py = m_parScanY.lowEdge() +
  181. j * (m_parScanY.highEdge() - m_parScanY.lowEdge()) /
  182. m_parScanY.bins();
  183.  
  184. info() << "Booking scan " << *xL << " = " << px << ", " << *yL
  185. << " = " << py << endmsg;
  186.  
  187. std::string l =
  188. label + ", " + m_parScanY.title() + " = " + std::to_string(py);
  189. m_hScanXvsX.push_back(
  190. book2D("XvsX/Scan" + std::to_string(i * m_parScanY.bins() + j), l,
  191. lowXY, highXY, binsXY, low, high, bins));
  192. m_hScanYvsY.push_back(
  193. book2D("YvsY/Scan" + std::to_string(i * m_parScanY.bins() + j), l,
  194. lowXY, highXY, binsXY, low, high, bins));
  195. m_testModules.push_back(new TbModule());
  196.  
  197. (*m_testModules.rbegin())->setAlignment(
  198. mod->x() + px * xaxis[0] + py * yaxis[0],
  199. mod->y() + px * xaxis[1] + py * yaxis[1],
  200. mod->z() + px * xaxis[2] + py * yaxis[2],
  201. mod->rotX() + px * xaxis[3] + py * yaxis[3],
  202. mod->rotY() + px * xaxis[4] + py * yaxis[4],
  203. mod->rotZ() + px * xaxis[5] + py * yaxis[5], 0, 0, 0, 0, 0, 0);
  204. }
  205. }
  206. }
  207. }
  208.  
  209. return StatusCode::SUCCESS;
  210. }
  211.  
  212. //=============================================================================
  213. // Main execution
  214. //=============================================================================
  215. StatusCode TbDUTMonitor::execute() {
  216.  
  217. // Grab the tracks.
  218. const LHCb::TbTracks* tracks = getIfExists<LHCb::TbTracks>(m_trackLocation);
  219. if (!tracks) {
  220. return Error("No tracks in " + m_trackLocation);
  221. }
  222.  
  223. // Loop over the tracks.
  224. for (const LHCb::TbTrack* track : *tracks) {
  225. auto clusters = track->associatedClusters();
  226. for (auto it = clusters.cbegin(), end = clusters.cend(); it != end; ++it) {
  227. const unsigned int plane = (*it)->plane();
  228. if (m_index.count(plane) < 1) continue;
  229. const Gaudi::XYZPoint pGlobal = geomSvc()->intercept(track, plane);
  230. const auto pLocal = geomSvc()->globalToLocal(pGlobal, plane);
  231. const unsigned int i = m_index[plane];
  232. const double dt = (*it)->htime() - track->htime();
  233. const double dx = (*it)->xloc() - pLocal.x();
  234. const double dy = (*it)->yloc() - pLocal.y();
  235. m_hResLocalX[i]->fill(dx);
  236. m_hResLocalY[i]->fill(dy);
  237.  
  238. const double dxug = (*it)->x() - pGlobal.x();
  239. const double dyug = (*it)->y() - pGlobal.y();
  240. m_hResGlobalX[i]->fill(dxug);
  241. m_hResGlobalY[i]->fill(dyug);
  242. m_hUnbiasedResGlobalXvsGlobalX[i]->fill((*it)->x(), dxug);
  243. m_hUnbiasedResGlobalYvsGlobalY[i]->fill((*it)->y(), dyug);
  244. m_hResTime[i]->fill(dt);
  245.  
  246. m_hUnbiasedResGlobalXvsTrackChi2[i]->fill(track->chi2PerNdof(), dxug);
  247. m_hUnbiasedResGlobalYvsTrackChi2[i]->fill(track->chi2PerNdof(), dyug);
  248.  
  249. m_UnbiasedResGlobalXvshUnbiasedResGlobalY[i]->fill(dxug, dyug);
  250.  
  251. m_hUnbiasedResGlobalXvsTrackTx[i]->fill(track->firstState().tx(), dxug);
  252. m_hUnbiasedResGlobalXvsTrackTy[i]->fill(track->firstState().ty(), dxug);
  253.  
  254. m_hUnbiasedResGlobalYvsTrackTx[i]->fill(track->firstState().tx(), dyug);
  255. m_hUnbiasedResGlobalYvsTrackTy[i]->fill(track->firstState().ty(), dyug);
  256.  
  257. m_hUnbiasedResGlobalXvsClusterSize[i]->fill((*it)->hits().size(), dxug);
  258. m_hUnbiasedResGlobalYvsClusterSize[i]->fill((*it)->hits().size(), dyug);
  259.  
  260. unsigned int row, col;
  261.  
  262. geomSvc()->pointToPixel(pLocal.x(), pLocal.y(), 4, col, row);
  263. double x0, y0;
  264. geomSvc()->pixelToPoint(col, row, 4, x0, y0);
  265.  
  266. const double pixelX = (pLocal.x() - x0) / 0.055;
  267. const double pixelY = (pLocal.y() - y0) / 0.055;
  268.  
  269. m_hUnbiasedResGlobalXvsPixelX[i]->fill(pixelX, dxug);
  270. m_hUnbiasedResGlobalXvsPixelY[i]->fill(pixelY, dxug);
  271. m_hUnbiasedResGlobalYvsPixelX[i]->fill(pixelX, dyug);
  272. m_hUnbiasedResGlobalYvsPixelY[i]->fill(pixelY, dyug);
  273.  
  274. for (int j = 0; j < m_testModules.size(); ++j) {
  275.  
  276. const auto& p = track->firstState().position();
  277. const auto& t = track->firstState().slopes();
  278. const Gaudi::XYZVector n = m_testModules[j]->normal();
  279. const double s = n.Dot(m_testModules[j]->centre() - p) / n.Dot(t);
  280. const auto intercept = p + s * t;
  281. const auto cl = m_testModules[j]->transform() *
  282. Gaudi::XYZPoint((*it)->xloc(), (*it)->yloc(), 0);
  283. const double dxug = cl.x() - intercept.x();
  284. const double dyug = cl.y() - intercept.y();
  285. m_hScanXvsX[j]->fill(cl.x(), dxug);
  286. m_hScanYvsY[j]->fill(cl.y(), dyug);
  287. }
  288. }
  289. }
  290.  
  291. return StatusCode::SUCCESS;
  292. }