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
#include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
#include "DataFormats/ParticleFlowReco/interface/PFCluster.h"
#include "DataFormats/ParticleFlowReco/interface/PFLayer.h"

#include <iomanip>
#include <sstream>

using namespace std;
using namespace reco;

void PFBlock::addElement(PFBlockElement* element) {
  element->setIndex(elements_.size());
  element->lock();
  elements_.push_back(element->clone());
}

void PFBlock::bookLinkData() {}

void PFBlock::setLink(unsigned i1, unsigned i2, double Dist, LinkData& linkData, LinkTest test) const {
  assert(test < LINKTEST_ALL);

  unsigned index = 0;
  bool ok = matrix2vector(i1, i2, index);

  if (ok) {
    //ignore the  -1, -1 pair
    if (Dist > -0.5) {
      Link& l = linkData[index];
      l.distance = Dist;
      l.test |= (1 << test);
    } else  //delete if existing
    {
      LinkData::iterator it = linkData.find(index);
      if (it != linkData.end())
        linkData.erase(it);
    }

  } else {
    assert(0);
  }
}

// void PFBlock::lock(unsigned i, LinkData& linkData ) const {

//   assert( linkData.size() == linkDataSize() );

//   for(unsigned j=0; j<elements_.size(); j++) {

//     if(i==j) continue;

//     unsigned index = 0;
//     bool ok =  matrix2vector(i,j, index);
//     if(ok)
//       linkData[index] = -1;
//     else
//       assert(0);
//   }
// }

void PFBlock::associatedElements(unsigned i,
                                 const LinkData& linkData,
                                 multimap<double, unsigned>& sortedAssociates,
                                 PFBlockElement::Type type,
                                 LinkTest test) const {
  sortedAssociates.clear();

  // i is too large
  if (i > elements_.size())
    return;
  // assert(i>=0); i >= 0, since i is unsigned

  for (unsigned ie = 0; ie < elements_.size(); ie++) {
    // considered element itself
    if (ie == i) {
      continue;
    }
    // not the right type
    if (type != PFBlockElement::NONE && elements_[ie].type() != type) {
      continue;
    }

    // Order the elements by increasing distance !

    unsigned index = 0;
    if (!matrix2vector(i, ie, index))
      continue;

    double c2 = -1;
    LinkData::const_iterator it = linkData.find(index);
    if (it != linkData.end() && (((1 << test) & it->second.test) != 0 || (test == LINKTEST_ALL)))
      c2 = it->second.distance;

    // not associated
    if (c2 < 0) {
      continue;
    }

    sortedAssociates.insert(pair<double, unsigned>(c2, ie));
  }
}

bool PFBlock::matrix2vector(unsigned iindex, unsigned jindex, unsigned& index) const {
  unsigned size = elements_.size();
  if (iindex == jindex || iindex >= size || jindex >= size) {
    return false;
  }

  if (iindex > jindex)
    std::swap(iindex, jindex);

  index = jindex - iindex - 1;

  if (iindex > 0) {
    index += iindex * size;
    unsigned missing = iindex * (iindex + 1) / 2;
    index -= missing;
  }

  return true;
}

double PFBlock::dist(unsigned ie1, unsigned ie2, const LinkData& linkData) const {
  double Dist = -1;

  unsigned index = 0;
  if (!matrix2vector(ie1, ie2, index))
    return Dist;
  LinkData::const_iterator it = linkData.find(index);
  if (it != linkData.end())
    Dist = it->second.distance;

  return Dist;
}

ostream& reco::operator<<(ostream& out, const reco::PFBlock& block) {
  if (!out)
    return out;
  const edm::OwnVector<reco::PFBlockElement>& elements = block.elements();
  out << "\t--- PFBlock ---  " << endl;
  out << "\tnumber of elements: " << elements.size() << endl;

  // Build element label (string) : elid from type, layer and occurence number
  // use stringstream instead of sprintf to concatenate string and integer into string

  vector<string> elid;
  string s;
  stringstream ss;
  int iel = 0;
  int iTK = 0;
  int iGSF = 0;
  int iBREM = 0;
  int iPS1 = 0;
  int iPS2 = 0;
  int iEE = 0;
  int iEB = 0;
  int iHE = 0;
  int iHB = 0;
  int iHFEM = 0;
  int iHFHAD = 0;
  int iSC = 0;
  int iHO = 0;

  // for each element in turn
  std::vector<bool> toPrint(elements.size(), static_cast<bool>(true));
  for (unsigned ie = 0; ie < elements.size(); ie++) {
    PFBlockElement::Type type = elements[ie].type();
    std::multimap<double, unsigned> ecalElems;
    switch (type) {
      case PFBlockElement::TRACK:
        iTK++;
        ss << "TK" << iTK;
        break;
      case PFBlockElement::GSF:
        iGSF++;
        ss << "GSF" << iGSF;
        break;
      case PFBlockElement::BREM:
        block.associatedElements(
            ie, block.linkData(), ecalElems, reco::PFBlockElement::ECAL, reco::PFBlock::LINKTEST_ALL);
        iBREM++;
        if (!ecalElems.empty()) {
          ss << "BR" << iBREM;
        } else {
          toPrint[ie] = false;
        }
        break;
      case PFBlockElement::SC:
        iSC++;
        ss << "SC" << iSC;
        break;
      default: {
        PFClusterRef clusterref = elements[ie].clusterRef();
        int layer = clusterref->layer();
        switch (layer) {
          case PFLayer::PS1:
            iPS1++;
            ss << "PV" << iPS1;
            break;
          case PFLayer::PS2:
            iPS2++;
            ss << "PH" << iPS2;
            break;
          case PFLayer::ECAL_ENDCAP:
            iEE++;
            ss << "EE" << iEE;
            break;
          case PFLayer::ECAL_BARREL:
            iEB++;
            ss << "EB" << iEB;
            break;
          case PFLayer::HCAL_ENDCAP:
            iHE++;
            ss << "HE" << iHE;
            break;
          case PFLayer::HCAL_BARREL1:
            iHB++;
            ss << "HB" << iHB;
            break;
          case PFLayer::HCAL_BARREL2:
            iHO++;
            ss << "HO" << iHO;
            break;
          case PFLayer::HF_EM:
            iHFEM++;
            ss << "FE" << iHFEM;
            break;
          case PFLayer::HF_HAD:
            iHFHAD++;
            ss << "FH" << iHFHAD;
            break;
          default:
            iel++;
            ss << "??" << iel;
            break;
        }
        break;
      }
    }
    s = ss.str();
    elid.push_back(s);
    // clear stringstream
    ss.str("");

    if (toPrint[ie])
      out << "\t" << s << " " << elements[ie] << endl;
  }

  out << endl;

  int width = 6;
  if (!block.linkData().empty()) {
    out << endl << "\tlink data (distance x 1000): " << endl;
    out << setiosflags(ios::right);
    out << "\t" << setw(width) << " ";
    for (unsigned ie = 0; ie < elid.size(); ie++)
      if (toPrint[ie])
        out << setw(width) << elid[ie];
    out << endl;
    out << setiosflags(ios::fixed);
    out << setprecision(1);

    for (unsigned i = 0; i < block.elements().size(); i++) {
      if (!toPrint[i])
        continue;
      out << "\t";
      out << setw(width) << elid[i];
      for (unsigned j = 0; j < block.elements().size(); j++) {
        if (!toPrint[j])
          continue;
        double Dist = block.dist(i, j, block.linkData());  //,PFBlock::LINKTEST_ALL);

        // out<<setw(width)<< Dist*1000.;
        if (Dist > -0.5)
          out << setw(width) << Dist * 1000.;
        else
          out << setw(width) << " ";
      }
      out << endl;
    }

    out << endl << "\tlink data (distance x 1000) for tracking links : " << endl;
    out << setiosflags(ios::right);
    out << "\t" << setw(width) << " ";
    for (unsigned ie = 0; ie < elid.size(); ie++)
      if (toPrint[ie] &&
          (block.elements()[ie].type() == PFBlockElement::TRACK || block.elements()[ie].type() == PFBlockElement::GSF))
        out << setw(width) << elid[ie];
    out << endl;
    out << setiosflags(ios::fixed);
    out << setprecision(1);

    for (unsigned i = 0; i < block.elements().size(); i++) {
      if (!toPrint[i] ||
          (block.elements()[i].type() != PFBlockElement::TRACK && block.elements()[i].type() != PFBlockElement::GSF))
        continue;
      out << "\t";
      out << setw(width) << elid[i];
      for (unsigned j = 0; j < block.elements().size(); j++) {
        if (!toPrint[j] ||
            (block.elements()[j].type() != PFBlockElement::TRACK && block.elements()[j].type() != PFBlockElement::GSF))
          continue;
        double Dist = block.dist(i, j, block.linkData());  //,PFBlock::LINKTEST_ALL);

        // out<<setw(width)<< Dist*1000.;
        if (Dist > -0.5)
          out << setw(width) << Dist * 1000.;
        else
          out << setw(width) << " ";
      }
      out << endl;
    }

    out << setprecision(3);
    out << resetiosflags(ios::right | ios::fixed);

  } else {
    out << "\tno links." << endl;
  }

  return out;
}

unsigned PFBlock::linkDataSize() const {
  unsigned n = elements_.size();

  // number of possible undirected links between n elements.
  // reflective links impossible.

  return n * (n - 1) / 2;
}