TrackerMapTool

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
// -*- C++ -*-
//
// Package:    TrackerMapTool
// Class:      TrackerMapTool
//
/**\class TrackerMapTool TrackerMapTool.cc 

 Description: <one line class summary>

 Implementation:
     <Notes on implementation>
*/
//
// Original Author:  Filippo Ambroglini
//         Created:  Tue Jul 26 08:47:57 CEST 2005
//
//

// system include files
#include <memory>

#include <vector>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/MakerMacros.h"

#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
#include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
#include "Geometry/TrackerNumberingBuilder/interface/GeometricDet.h"
#include "Geometry/CommonTopologies/interface/PixelTopology.h"
#include "Geometry/CommonTopologies/interface/StripTopology.h"
#include "Geometry/CommonDetUnit/interface/PixelGeomDetType.h"
#include "Geometry/TrackerGeometryBuilder/interface/StripGeomDetType.h"

#include "Geometry/CommonDetUnit/interface/PixelGeomDetUnit.h"
#include "DataFormats/GeometrySurface/interface/BoundSurface.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
//
//
// class decleration
//

class TrackerMapTool : public edm::one::EDAnalyzer<> {
public:
  explicit TrackerMapTool(const edm::ParameterSet&);

  void analyze(edm::Event const& iEvent, edm::EventSetup const&) override;

private:
  edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> pDDToken_;
};

//
// constructors and destructor
//
TrackerMapTool::TrackerMapTool(const edm::ParameterSet& iConfig) : pDDToken_(esConsumes()) {}

//
// member functions
//

int layerno(int subdet, int leftright, int layer) {
  if (subdet == 6 && leftright == 1)
    return (10 - layer);
  if (subdet == 6 && leftright == 2)
    return (layer + 21);
  if (subdet == 4 && leftright == 1)
    return (4 - layer + 9);
  if (subdet == 4 && leftright == 2)
    return (layer + 18);
  if (subdet == 2 && leftright == 1)
    return (4 - layer + 12);
  if (subdet == 2 && leftright == 2)
    return (layer + 15);
  if (subdet == 1)
    return (layer + 30);
  if (subdet == 3)
    return (layer + 33);
  if (subdet == 5)
    return (layer + 37);
  // 2009-08-26 Michael Case: to get rid of a compiler warning about control reaching
  // the end of a non-void function I put return -1 here.  This changed the output
  // of the test so I changed it to return 0 to match the "before my changes" run
  // of trackerMap_cfg.py.
  return 0;  // this was added.  No checks have been mad where layerno is used.
}
// ------------ method called to produce the data  ------------
void TrackerMapTool::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
  edm::LogInfo("TrackerMapTool") << "Here I am";

  //
  // get the TrackerGeom
  //
  auto const& pDD = iSetup.getData(pDDToken_);
  edm::LogInfo("TrackerMapTool") << " Geometry node for TrackerGeom is  " << &pDD;
  edm::LogInfo("TrackerMapTool") << " I have " << pDD.dets().size() << " detectors";
  edm::LogInfo("TrackerMapTool") << " I have " << pDD.detTypes().size() << " types";
  int spicchif[] = {24, 24, 40, 56, 40, 56, 80};
  int spicchib[] = {20, 32, 44, 30, 38, 46, 56, 42, 48, 54, 60, 66, 74};

  int nlay = 0;
  int layer, subdet, leftright = 0, ringno, petalno, moduleno, isStereo, pixel_strip, barrel_forward;
  std::string name0, name1, name2, name3, name4, name5;
  int ring = 0;
  int nmod = 0;
  int ntotmod = 0;
  float r;
  int bar_fow = 1;
  float phi, phi1;
  float rmedioS[] = {0.27665, 0.3671, 0.4474, 0.5617, 0.6768, 0.8189, 0.9907};
  float rmedioP[] = {0.0623081, 0.074111, 0.0870344, 0.103416, 0.115766, 0.132728, 0.140506};
  std::string nameDet;
  int last_layer = 0;
  float width, length, thickness, widthAtHalfLength;
  std::ofstream output("tracker.dat", std::ios::out);

  auto begin = pDD.detUnits().begin();
  auto end = pDD.detUnits().end();

  for (; begin != end; ++begin) {
    ntotmod++;
    subdet = (*begin)->geographicalId().subdetId();
    if (subdet == 1 || subdet == 3 || subdet == 5) {  //barrel
      layer = ((*begin)->geographicalId().rawId() >> 16) & 0xF;
      leftright = 0;
      name0 = " ";
    } else {  //endcap
      leftright = ((*begin)->geographicalId().rawId() >> 23) & 0x3;
      name0 = "+z";
      if (leftright == 1)
        name0 = "-z";
      layer = ((*begin)->geographicalId().rawId() >> 16) & 0xF;
    }
    isStereo = (*begin)->geographicalId().rawId() & 0x3;
    pixel_strip = 2;
    if (subdet <= 2)
      pixel_strip = 1;
    barrel_forward = 2;
    if (subdet == 2 || subdet == 4 || subdet == 6) {
      if (leftright == 1)
        barrel_forward = 1;
      else
        barrel_forward = 3;
    }
    nlay = layerno(subdet, leftright, layer);
    ringno = 0;
    petalno = 0;
    moduleno = 0;
    name1 = "   ";
    if (subdet == 1) {
      nameDet = "BPIX";
      name1 = " ladder ";
      ringno = ((*begin)->geographicalId().rawId() >> 8) & 0xFF;
    }
    if (subdet == 2) {
      nameDet = "FPIX";
      name1 = " ring ";
      ringno = ((*begin)->geographicalId().rawId() >> 8) & 0xFF;
    }
    if (subdet == 3) {
      nameDet = "TIB";
      name1 = " string ";
      ringno = ((*begin)->geographicalId().rawId() >> 8) & 0x3F;
    }
    if (subdet == 5) {
      nameDet = "TOB";
      name1 = " rod ";
      ringno = ((*begin)->geographicalId().rawId() >> 8) & 0x7F;
    }
    if (subdet == 4) {
      nameDet = "TID";
      name1 = " ring ";
      ringno = ((*begin)->geographicalId().rawId() >> 8) & 0xFF;
    }
    if (subdet == 6) {
      nameDet = "TEC";
      name1 = " ring ";
    }
    name2 = " ";
    if (subdet == 6) {
      name2 = " petal ";
      petalno = ((*begin)->geographicalId().rawId() >> 8) & 0x7F;
    }
    if (subdet == 2) {
      name2 = " blade ";
      petalno = ((*begin)->geographicalId().rawId() >> 8) & 0x3F;
    }
    name3 = " ";
    if (subdet == 6) {
      name3 = " forward ";
      if ((((*begin)->geographicalId().rawId() >> 4) & 0x1) == 1)
        name3 = " backward ";
    }
    if (subdet == 2) {
      name3 = " forward ";
      if ((((*begin)->geographicalId().rawId() >> 4) & 0x1) == 1)
        name3 = " backward ";
    }
    name5 = " ";
    if (subdet == 6) {
      name5 = " forward ";
      if ((((*begin)->geographicalId().rawId() >> 15) & 0x1) == 1) {
        name5 = " backward ";
      }
    }
    if (subdet == 2) {
      name5 = " left ";
      if ((((*begin)->geographicalId().rawId() >> 14) & 0x1) == 1) {
        name5 = " right ";
      }
    }
    if (subdet == 3) {
      name2 = " neg ";
      if ((((*begin)->geographicalId().rawId() >> 15) & 0x1) == 1)
        name2 = " pos ";
    }
    if (subdet == 5) {
      name2 = " neg ";
      if ((((*begin)->geographicalId().rawId() >> 15) & 0x1) == 1)
        name2 = " pos ";
    }
    if (subdet == 3) {
      name3 = " internal ";
      if ((((*begin)->geographicalId().rawId() >> 14) & 0x1) == 1)
        name3 = " external ";
    }
    if (subdet == 4) {
      name3 = " forward ";
      if ((((*begin)->geographicalId().rawId() >> 7) & 0x1) == 1)
        name3 = " backward ";
    }
    if (subdet == 1) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x3F;
    }
    if (subdet == 3) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x3F;
    }
    if (subdet == 5) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x3F;
    }
    if (subdet == 4) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x1F;
    }
    if (subdet == 6) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x3;
    }
    if (subdet == 2) {
      moduleno = ((*begin)->geographicalId().rawId() >> 2) & 0x7;
    }
    length = (*begin)->surface().bounds().length() / 100.0;        // cm -> m
    width = (*begin)->surface().bounds().width() / 100.0;          // cm -> mo
    thickness = (*begin)->surface().bounds().thickness() / 100.0;  // cm -> m
    widthAtHalfLength = (*begin)->surface().bounds().widthAtHalfLength() / 100.0;

    if (nlay != last_layer) {
      ring = -1;
      last_layer = nlay;
    }
    bar_fow = 2;
    if (nlay < 16)
      bar_fow = 1;
    if (nlay > 15 && nlay < 31)
      bar_fow = 3;
    float posx = (*begin)->surface().position().x() / 100.0;  // cm -> m
    float posy = (*begin)->surface().position().y() / 100.0;  // cm -> m
    float posz = (*begin)->surface().position().z() / 100.0;  // cm -> m
    r = pow(((double)(posx * posx) + posy * posy), 0.5);
    phi1 = atan(posy / posx);
    phi = phi1;
    if (posy < 0. && posx > 0)
      phi = phi1 + 2. * M_PI;
    if (posx < 0.)
      phi = phi1 + M_PI;
    if (fabs(posy) < 0.000001 && posx > 0)
      phi = 0;
    if (fabs(posy) < 0.000001 && posx < 0)
      phi = M_PI;
    if (fabs(posx) < 0.000001 && posy > 0)
      phi = M_PI / 2.;
    if (fabs(posx) < 0.000001 && posy < 0)
      phi = M_PI + M_PI / 2.;

    if (bar_fow == 2) {  //barrel
      if (subdet == 1)
        ring = moduleno;
      if (subdet == 5) {
        ring = moduleno;
        if ((((*begin)->geographicalId().rawId() >> 15) & 0x1) == 1)
          ring = ring + 6;
      }
      if (subdet == 3) {
        ring = moduleno;
        if (layer == 2 || layer == 4) {
          if ((((*begin)->geographicalId().rawId() >> 14) & 0x1) == 1) {
            ring = ring * 2;
          } else {
            ring = ring * 2 - 1;
          }
        } else {
          if ((((*begin)->geographicalId().rawId() >> 14) & 0x1) == 1) {
            ring = ring * 2 - 1;
          } else {
            ring = ring * 2;
          }
        }
        if ((((*begin)->geographicalId().rawId() >> 15) & 0x1) == 1)
          ring = ring + 6;
      }

      nmod = (int)((phi / (2. * M_PI)) * spicchib[nlay - 31] + .1) + 1;
      if (nlay == 40)
        nmod = nmod - 1;
      if (subdet == 1)
        nmod = ringno;
    } else {  // endcap
      if (subdet == 4 || subdet == 6) {
        for (int i = 0; i < 7; i++) {
          if (fabs(r - rmedioS[i]) < 0.015) {
            ring = i + 1;
            break;
          }
        }
        nmod = (int)((phi / (2. * M_PI)) * spicchif[ring - 1] + .1) + 1;
      } else {
        for (int i = 0; i < 7; i++) {
          if (fabs(r - rmedioP[i]) < 0.0001) {
            ring = i + 1;
            break;
          }
        }
        nmod = (int)((phi / (2. * M_PI)) * 24 + .1) + 1;
      }
    }  //end of endcap part
    if (isStereo == 1)
      nmod = nmod + 100;
    name4 = " ";
    if (isStereo == 1)
      name4 = " stereo";
    std::ostringstream outs;
    if (subdet == 6)
      outs << nameDet << " " << name0 << " layer " << layer << name1 << ringno << name2 << petalno << name5
           << " module  " << moduleno << name3 << " " << name4;
    if (subdet == 2)
      outs << nameDet << " " << name0 << " disc " << layer << name1 << ring << name2 << petalno << " plaquette  "
           << moduleno << name5;
    if (subdet == 1 || subdet == 3 || subdet == 4 || subdet == 5)
      outs << nameDet << " " << name0 << " layer " << layer << name1 << ringno << name2 << name3 << " module  "
           << moduleno << " " << name4;
    char buffer[20];
    sprintf(buffer, "%X", (*begin)->geographicalId().rawId());
    int id = (*begin)->geographicalId().rawId();
    output << ntotmod << " " << pixel_strip << " " << barrel_forward << " " << nlay << " " << ring << " " << nmod << " "
           << posx << " " << posy << " " << posz << " " << length << " " << width << " " << thickness << " "
           << widthAtHalfLength << " "
           << " " << id << std::endl;
    output << outs.str() << std::endl;
  }
}
//define this as a plug-in
DEFINE_FWK_MODULE(TrackerMapTool);