Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ChannelMerger.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ChannelMerger.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
14 
15 #include <array>
16 #include <utility>
17 
18 namespace ActsFatras {
19 
29 template <typename signal_t, size_t kSize>
30 const std::vector<Channel<signal_t, kSize>> mergeChannels(
31  const std::vector<Channel<signal_t, kSize>>& channels) {
33  using ChannelKey = std::array<unsigned int, kSize>;
34 
35  // Fill a channel map - use the channel identification
36  auto extractChannelKey = [&](const Channel& ch) -> ChannelKey {
37  ChannelKey cKey;
38  for (unsigned int ik = 0; ik < kSize; ++ik) {
39  cKey[ik] = ch.cellId[ik].first;
40  }
41  return cKey;
42  };
43 
44  std::map<ChannelKey, Channel> channelMap;
45  for (const auto& ch : channels) {
46  ChannelKey key = extractChannelKey(ch);
47  auto chItr = channelMap.find(key);
48  if (chItr != channelMap.end()) {
49  chItr->second.value += ch.value;
50  chItr->second.links.insert(ch.links.begin(), ch.links.end());
51  } else {
52  channelMap.insert(std::pair<ChannelKey, Channel>(key, ch));
53  }
54  }
55  // Unroll the channels after merging
56  std::vector<Channel> mergedChannels;
57  mergedChannels.reserve(channelMap.size());
58  for (auto& [key, value] : channelMap) {
59  mergedChannels.push_back(value);
60  }
61  return mergedChannels;
62 }
63 
64 } // namespace ActsFatras