Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:27:43

0001 #include "DetGroupMerger.h"
0002 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0003 
0004 using namespace std;
0005 
0006 void DetGroupMerger::orderAndMergeTwoLevels(
0007     vector<DetGroup>&& one, vector<DetGroup>&& two, std::vector<DetGroup>& result, int firstIndex, int firstCrossed) {
0008   if (one.empty() && two.empty())
0009     return;
0010 
0011   if (one.empty()) {
0012     result = std::move(two);
0013     if (firstIndex == firstCrossed)
0014       incrementAndDoubleSize(result);
0015     else
0016       doubleIndexSize(result);
0017   } else if (two.empty()) {
0018     result = std::move(one);
0019     if (firstIndex == firstCrossed)
0020       doubleIndexSize(result);
0021     else
0022       incrementAndDoubleSize(result);
0023   } else {  // both are not empty
0024     if (firstIndex == firstCrossed)
0025       mergeTwoLevels(std::move(one), std::move(two), result);
0026     else
0027       mergeTwoLevels(std::move(two), std::move(one), result);
0028   }
0029 }
0030 
0031 void DetGroupMerger::mergeTwoLevels(vector<DetGroup>&& one, vector<DetGroup>&& two, std::vector<DetGroup>& result) {
0032   result.reserve(one.size() + two.size());
0033 
0034   int indSize1 = one.front().indexSize();
0035   int indSize2 = two.front().indexSize();
0036 
0037   for (auto&& dg : one) {
0038     result.push_back(std::move(dg));
0039     result.back().setIndexSize(indSize1 + indSize2);
0040   }
0041   for (auto&& dg : two) {
0042     result.push_back(std::move(dg));
0043     result.back().incrementIndex(indSize1);
0044   }
0045 }
0046 
0047 void DetGroupMerger::addSameLevel(vector<DetGroup>&& gvec, vector<DetGroup>& result) {
0048   for (auto&& ig : gvec) {
0049     int gSize = ig.indexSize();
0050     int index = ig.index();  // at which level it should be inserted
0051     bool found = false;
0052     for (vector<DetGroup>::iterator ires = result.begin(); ires != result.end(); ires++) {
0053       int resSize = ires->indexSize();
0054       if (gSize != resSize) {
0055         LogDebug("TkDetLayers") << "DetGroupMerger::addSameLevel called with groups of different index sizes";
0056         // throw something appropriate...or handle it properly (may happen in petals?)
0057       }
0058 
0059       int resIndex = ires->index();
0060       if (index == resIndex) {
0061         ires->insert(ires->end(), ig.begin(), ig.end());  // insert in group with same index
0062         found = true;
0063         break;
0064       } else if (index < resIndex) {
0065         // result has no group at index level yet
0066         result.insert(ires, ig);  // insert a new group, invalidates the iterator ires
0067         found = true;
0068         break;
0069       }
0070     }  // end of loop over result groups
0071     if (!found)
0072       result.insert(result.end(), ig);  // in case the ig index is bigger than any in result
0073   }
0074 }
0075 
0076 void DetGroupMerger::doubleIndexSize(vector<DetGroup>& vec) {
0077   int indSize = vec.front().indexSize();
0078   for (vector<DetGroup>::iterator i = vec.begin(); i != vec.end(); i++) {
0079     i->setIndexSize(2 * indSize);
0080   }
0081 }
0082 
0083 void DetGroupMerger::incrementAndDoubleSize(vector<DetGroup>& vec) {
0084   int indSize = vec.front().indexSize();
0085   for (vector<DetGroup>::iterator i = vec.begin(); i != vec.end(); i++) {
0086     i->incrementIndex(indSize);
0087   }
0088 }