1 |
//--------------------------------------------------------------------------------------------------
|
2 |
// $Id: MergerMod.h,v 1.1 2008/12/11 17:37:29 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 |
~MergerMod() {}
|
27 |
|
28 |
void AddInputName(const char *n);
|
29 |
const char *GetMergedName() const { return fMergedName; }
|
30 |
const char *GetOutputName() const { return GetMergedName(); }
|
31 |
void SetMergedName(const char *n) { fMergedName=n; }
|
32 |
void SetOutputName(const char *n) { SetMergedName(n); }
|
33 |
|
34 |
protected:
|
35 |
void SlaveBegin();
|
36 |
void Process();
|
37 |
|
38 |
std::vector<std::string> fInList; //list of to be merged objects (input)
|
39 |
TString fMergedName; //name of merged collection (output)
|
40 |
|
41 |
ClassDefT(MergerMod, 1) // Merger module
|
42 |
};
|
43 |
}
|
44 |
#endif
|
45 |
|
46 |
//--------------------------------------------------------------------------------------------------
|
47 |
template <class T>
|
48 |
mithep::MergerMod<T>::MergerMod<T>(const char *name, const char *title) :
|
49 |
BaseMod(name,title),
|
50 |
fInList(0)
|
51 |
{
|
52 |
// Constructor.
|
53 |
}
|
54 |
|
55 |
template <class T>
|
56 |
void mithep::MergerMod<T>::AddInputName(const char *n)
|
57 |
{
|
58 |
// Add name to the list of input collections.
|
59 |
|
60 |
if (!n)
|
61 |
return;
|
62 |
fInList.push_back(std::string(n));
|
63 |
}
|
64 |
|
65 |
//--------------------------------------------------------------------------------------------------
|
66 |
template <class T>
|
67 |
void mithep::MergerMod<T>::Process()
|
68 |
{
|
69 |
// Merge the input objects and publish merged collection.
|
70 |
|
71 |
ObjArray<T> *colout = new mithep::ObjArray<T>(0);
|
72 |
colout->SetName(GetMergedName());
|
73 |
|
74 |
UInt_t n = fInList.size();
|
75 |
for (UInt_t i=0; i<n; ++i) {
|
76 |
const TObject *in = GetObjThisEvt<TObject>(fInList.at(i).c_str());
|
77 |
if (!in)
|
78 |
continue;
|
79 |
|
80 |
const BaseCollection *bcol = dynamic_cast<const BaseCollection*>(in);
|
81 |
if (bcol)
|
82 |
colout->Add(bcol);
|
83 |
else {
|
84 |
const T *optr = dynamic_cast<const T*>(in);
|
85 |
if (optr)
|
86 |
colout->Add(optr);
|
87 |
}
|
88 |
}
|
89 |
|
90 |
// attempt sorting (if implemented by underlying objects)
|
91 |
colout->Sort();
|
92 |
|
93 |
// add to event for other modules to use
|
94 |
AddObjThisEvt(colout);
|
95 |
}
|
96 |
|
97 |
//--------------------------------------------------------------------------------------------------
|
98 |
template <class T>
|
99 |
void mithep::MergerMod<T>::SlaveBegin()
|
100 |
{
|
101 |
// Check if output name was set.
|
102 |
|
103 |
if (!fMergedName.IsNull())
|
104 |
return;
|
105 |
|
106 |
SendError(kAbortModule, "SlaveBegin", "No output name given.");
|
107 |
}
|