CMS 3D CMS Logo

CleanAndMergeProducer.cc
Go to the documentation of this file.
1 // C/C++ headers
2 #include <iostream>
3 #include <vector>
4 #include <memory>
5 
6 // Framework
12 // Reconstruction Classes
21 
22 // Geometry
30 
31 // Class header file
35 
36 /*
37 CleanAndMergeProducer:
38 ^^^^^^^^^^^^^^^^^^^^^^
39 
40 Takes as input the cleaned and the uncleaned collection of SC
41 and produces an uncleaned collection of SC without the duplicates
42 and a collection of references to the cleaned collection of SC
43 
44 25 June 2010
45 Nikolaos Rompotis and Chris Seez - Imperial College London
46 many thanks to Shahram Rahatlou and Federico Ferri
47 
48 
49 */
50 
52  // get the parameters
53  // the cleaned collection:
54  cleanScToken_ = consumes<reco::SuperClusterCollection>(ps.getParameter<edm::InputTag>("cleanScInputTag"));
55  uncleanScToken_ = consumes<reco::SuperClusterCollection>(ps.getParameter<edm::InputTag>("uncleanScInputTag"));
56 
57  // the names of the products to be produced:
58  bcCollection_ = ps.getParameter<std::string>("bcCollection");
59  scCollection_ = ps.getParameter<std::string>("scCollection");
60  refScCollection_ = ps.getParameter<std::string>("refScCollection");
61 
62  std::map<std::string, double> providedParameters;
63  providedParameters.insert(std::make_pair("LogWeighted", ps.getParameter<bool>("posCalc_logweight")));
64  providedParameters.insert(std::make_pair("T0_barl", ps.getParameter<double>("posCalc_t0")));
65  providedParameters.insert(std::make_pair("W0", ps.getParameter<double>("posCalc_w0")));
66  providedParameters.insert(std::make_pair("X0", ps.getParameter<double>("posCalc_x0")));
67 
68  // the products:
69  produces<reco::BasicClusterCollection>(bcCollection_);
70  produces<reco::SuperClusterCollection>(scCollection_);
71  produces<reco::SuperClusterRefVector>(refScCollection_);
72 }
73 
75 
77  // get the input collections
78  // ______________________________________________________________________________________
79  //
80  // cluster collections:
81 
84 
85  evt.getByToken(cleanScToken_, pCleanSC);
86  evt.getByToken(uncleanScToken_, pUncleanSC);
87 
88  //
89  // collections are all taken now _____________________________________________________
90  //
91  //
92  // the collections to be produced:
93  reco::BasicClusterCollection basicClusters;
96 
97  //
98  // run over the uncleaned SC and check how many of them are matched to the cleaned ones
99  // if you find a matched one, create a reference to the cleaned collection and store it
100  // if you find an unmatched one, then keep all its basic clusters in the basic Clusters
101  // vector
102  int uncleanSize = pUncleanSC->size();
103  int cleanSize = pCleanSC->size();
104 
105  LogTrace("EcalCleaning") << "Size of Clean Collection: " << cleanSize << ", uncleanSize: " << uncleanSize
106  << std::endl;
107  //
108  // keep whether the SC in unique in the uncleaned collection
109  std::vector<int> isUncleanOnly; // 1 if unique in the uncleaned
110  std::vector<int> basicClusterOwner; // contains the index of the SC that owns that BS
111  std::vector<int> isSeed; // if this basic cluster is a seed it is 1
112  for (int isc = 0; isc < uncleanSize; ++isc) {
113  reco::SuperClusterRef unscRef(pUncleanSC, isc);
114  const std::vector<std::pair<DetId, float> >& uhits = unscRef->hitsAndFractions();
115  int uhitsSize = uhits.size();
116  bool foundTheSame = false;
117  for (int jsc = 0; jsc < cleanSize; ++jsc) { // loop over the cleaned SC
118  reco::SuperClusterRef cscRef(pCleanSC, jsc);
119  const std::vector<std::pair<DetId, float> >& chits = cscRef->hitsAndFractions();
120  int chitsSize = chits.size();
121  foundTheSame = true;
122  if (unscRef->seed()->seed() == cscRef->seed()->seed() && chitsSize == uhitsSize) {
123  // if the clusters are exactly the same then because the clustering
124  // algorithm works in a deterministic way, the order of the rechits
125  // will be the same
126  for (int i = 0; i < chitsSize; ++i) {
127  if (uhits[i].first != chits[i].first) {
128  foundTheSame = false;
129  break;
130  }
131  }
132  if (foundTheSame) { // ok you have found it!
133  // make the reference
134  //scRefs->push_back( edm::Ref<reco::SuperClusterCollection>(pCleanSC, jsc) );
135  scRefs->push_back(cscRef);
136  isUncleanOnly.push_back(0);
137  break;
138  }
139  }
140  }
141  if (not foundTheSame) {
142  // mark it as unique in the uncleaned
143  isUncleanOnly.push_back(1);
144  // keep all its basic clusters
145  for (reco::CaloCluster_iterator bciter = unscRef->clustersBegin(); bciter != unscRef->clustersEnd(); ++bciter) {
146  // the basic clusters
147  basicClusters.push_back(**bciter);
148  basicClusterOwner.push_back(isc);
149  }
150  }
151  }
152  int bcSize = basicClusters.size();
153 
154  LogDebug("EcalCleaning") << "Found cleaned SC: " << cleanSize << " uncleaned SC: " << uncleanSize << " from which "
155  << scRefs->size() << " will become refs to the cleaned collection";
156 
157  // now you have the collection of basic clusters of the SC to be remain in the
158  // in the clean collection, export them to the event
159  // you will need to reread them later in order to make correctly the refs to the SC
160  auto basicClusters_p = std::make_unique<reco::BasicClusterCollection>();
161  basicClusters_p->assign(basicClusters.begin(), basicClusters.end());
163  if (!(bccHandle.isValid())) {
164  edm::LogWarning("MissingInput") << "could not get a handle on the BasicClusterCollection!" << std::endl;
165  return;
166  }
167 
168  LogDebug("EcalCleaning") << "Got the BasicClusters from the event again";
169  // now you have to create again your superclusters
170  // you run over the uncleaned SC, but now you know which of them are
171  // the ones that are needed and which are their basic clusters
172  for (int isc = 0; isc < uncleanSize; ++isc) {
173  if (isUncleanOnly[isc] == 1) { // look for sc that are unique in the unclean collection
174  // make the basic cluster collection
175  reco::CaloClusterPtrVector clusterPtrVector;
176  reco::CaloClusterPtr seed; // the seed is the basic cluster with the highest energy
177  double energy = -1;
178  for (int jbc = 0; jbc < bcSize; ++jbc) {
179  if (basicClusterOwner[jbc] == isc) {
180  reco::CaloClusterPtr currentClu = reco::CaloClusterPtr(bccHandle, jbc);
181  clusterPtrVector.push_back(currentClu);
182  if (energy < currentClu->energy()) {
183  energy = currentClu->energy();
184  seed = currentClu;
185  }
186  }
187  }
188  reco::SuperClusterRef unscRef(pUncleanSC, isc);
189  reco::SuperCluster newSC(unscRef->energy(), unscRef->position(), seed, clusterPtrVector);
190  superClusters.push_back(newSC);
191  }
192  }
193  // export the collection of references to the clean collection
194  std::unique_ptr<reco::SuperClusterRefVector> scRefs_p(scRefs);
195  evt.put(std::move(scRefs_p), refScCollection_);
196 
197  // the collection of basic clusters is already in the event
198  // the collection of uncleaned SC
199  auto superClusters_p = std::make_unique<reco::SuperClusterCollection>();
200  superClusters_p->assign(superClusters.begin(), superClusters.end());
201  evt.put(std::move(superClusters_p), scCollection_);
202  LogDebug("EcalCleaning") << "Hybrid Clusters (Basic/Super) added to the Event! :-)";
203 }
#define LogDebug(id)
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:131
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
void push_back(Ptr< T > const &iPtr)
Definition: PtrVector.h:149
edm::EDGetTokenT< reco::SuperClusterCollection > uncleanScToken_
void produce(edm::Event &, const edm::EventSetup &) override
edm::Ptr< CaloCluster > CaloClusterPtr
edm::EDGetTokenT< reco::SuperClusterCollection > cleanScToken_
std::vector< SuperCluster > SuperClusterCollection
collection of SuperCluser objectr
#define LogTrace(id)
std::vector< BasicCluster > BasicClusterCollection
collection of BasicCluster objects
void push_back(value_type const &ref)
Add a Ref<C, T> to the RefVector.
Definition: RefVector.h:67
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
edm::RefVector< SuperClusterCollection > SuperClusterRefVector
vector of references to objects in the same colletion of SuperCluster objects
CleanAndMergeProducer(const edm::ParameterSet &ps)
def move(src, dest)
Definition: eostools.py:511