1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: MergerMod.h,v 1.2 2009/03/02 13:27:17 loizides Exp $
|
3 |
//
|
4 |
// MergerMod
|
5 |
//
|
6 |
// This templated module merges arbitrarily many collections of objects. It puts them into an
|
7 |
// object array of the templated type.
|
8 |
//
|
9 |
// Authors: C.Loizides
|
10 |
//--------------------------------------------------------------------------------------------------
|
11 |
|
12 |
#ifndef MITPHYSICS_MODS_MERGERMOD_H
|
13 |
#define MITPHYSICS_MODS_MERGERMOD_H
|
14 |
|
15 |
#include "MitAna/TreeMod/interface/BaseMod.h"
|
16 |
#include "MitAna/DataTree/interface/Collections.h"
|
17 |
|
18 |
namespace mithep
|
19 |
{
|
20 |
template <class T>
|
21 |
class MergerMod : public BaseMod
|
22 |
{
|
23 |
public:
|
24 |
MergerMod(const char *name="MergerMod",
|
25 |
const char *title="Merger module");
|
26 |
|
27 |
void AddInputName(const char *n);
|
28 |
const char *GetMergedName() const { return fMergedName; }
|
29 |
const char *GetOutputName() const { return GetMergedName(); }
|
30 |
void SetMergedName(const char *n) { fMergedName=n; }
|
31 |
void SetOutputName(const char *n) { SetMergedName(n); }
|
32 |
|
33 |
protected:
|
34 |
void SlaveBegin();
|
35 |
void Process();
|
36 |
|
37 |
std::vector<std::string> fInList; //list of to be merged objects (input)
|
38 |
TString fMergedName; //name of merged collection (output)
|
39 |
|
40 |
ClassDefT(MergerMod, 1) // Merger module
|
41 |
};
|
42 |
}
|
43 |
#endif
|
44 |
|
45 |
//--------------------------------------------------------------------------------------------------
|
46 |
template <class T>
|
47 |
mithep::MergerMod<T>::MergerMod<T>(const char *name, const char *title) :
|
48 |
BaseMod(name,title),
|
49 |
fInList(0)
|
50 |
{
|
51 |
// Constructor.
|
52 |
}
|
53 |
|
54 |
template <class T>
|
55 |
void mithep::MergerMod<T>::AddInputName(const char *n)
|
56 |
{
|
57 |
// Add name to the list of input collections.
|
58 |
|
59 |
if (!n)
|
60 |
return;
|
61 |
fInList.push_back(std::string(n));
|
62 |
}
|
63 |
|
64 |
//--------------------------------------------------------------------------------------------------
|
65 |
template <class T>
|
66 |
void mithep::MergerMod<T>::Process()
|
67 |
{
|
68 |
// Merge the input objects and publish merged collection.
|
69 |
|
70 |
ObjArray<T> *colout = new mithep::ObjArray<T>(0);
|
71 |
colout->SetName(GetMergedName());
|
72 |
|
73 |
UInt_t n = fInList.size();
|
74 |
for (UInt_t i=0; i<n; ++i) {
|
75 |
const TObject *in = GetObjThisEvt<TObject>(fInList.at(i).c_str());
|
76 |
if (!in)
|
77 |
continue;
|
78 |
|
79 |
const BaseCollection *bcol = dynamic_cast<const BaseCollection*>(in);
|
80 |
if (bcol)
|
81 |
colout->Add(bcol);
|
82 |
else {
|
83 |
const T *optr = dynamic_cast<const T*>(in);
|
84 |
if (optr)
|
85 |
colout->Add(optr);
|
86 |
}
|
87 |
}
|
88 |
|
89 |
// attempt sorting (if implemented by underlying objects)
|
90 |
colout->Sort();
|
91 |
|
92 |
// add to event for other modules to use
|
93 |
AddObjThisEvt(colout);
|
94 |
}
|
95 |
|
96 |
//--------------------------------------------------------------------------------------------------
|
97 |
template <class T>
|
98 |
void mithep::MergerMod<T>::SlaveBegin()
|
99 |
{
|
100 |
// Check if output name was set.
|
101 |
|
102 |
if (!fMergedName.IsNull())
|
103 |
return;
|
104 |
|
105 |
SendError(kAbortModule, "SlaveBegin", "No output name given.");
|
106 |
}
|