1 | |
---|
2 | /** \class ExRootFilter |
---|
3 | * |
---|
4 | * Class simplifying classification and subarrays handling |
---|
5 | * |
---|
6 | * $Date: 2008-06-04 13:57:55 $ |
---|
7 | * $Revision: 1.1 $ |
---|
8 | * |
---|
9 | * |
---|
10 | * \author P. Demin - UCL, Louvain-la-Neuve |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | #include "ExRootAnalysis/ExRootFilter.h" |
---|
15 | #include "ExRootAnalysis/ExRootClassifier.h" |
---|
16 | |
---|
17 | #include "TSeqCollection.h" |
---|
18 | #include "TObjArray.h" |
---|
19 | |
---|
20 | #include <iostream> |
---|
21 | #include <stdexcept> |
---|
22 | #include <sstream> |
---|
23 | |
---|
24 | using namespace std; |
---|
25 | |
---|
26 | typedef map<Int_t, TObjArray*> TCategoryMap; |
---|
27 | typedef map<ExRootClassifier*, pair<Bool_t, TCategoryMap> > TClassifierMap; |
---|
28 | |
---|
29 | ExRootFilter::ExRootFilter(const TSeqCollection *collection) : |
---|
30 | fCollection(collection) |
---|
31 | { |
---|
32 | fIter = fCollection->MakeIterator(); |
---|
33 | } |
---|
34 | |
---|
35 | //------------------------------------------------------------------------------ |
---|
36 | |
---|
37 | ExRootFilter::~ExRootFilter() |
---|
38 | { |
---|
39 | TClassifierMap::iterator itMap; |
---|
40 | TCategoryMap::iterator itSubMap; |
---|
41 | for(itMap = fMap.begin(); itMap != fMap.end(); ++itMap) |
---|
42 | { |
---|
43 | for(itSubMap = itMap->second.second.begin(); |
---|
44 | itSubMap != itMap->second.second.end(); ++itSubMap) |
---|
45 | { |
---|
46 | delete (itSubMap->second); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | delete fIter; |
---|
51 | } |
---|
52 | |
---|
53 | //------------------------------------------------------------------------------ |
---|
54 | |
---|
55 | void ExRootFilter::Reset(ExRootClassifier *classifier) |
---|
56 | { |
---|
57 | TClassifierMap::iterator itMap; |
---|
58 | TCategoryMap::iterator itSubMap; |
---|
59 | if(classifier) |
---|
60 | { |
---|
61 | itMap = fMap.find(classifier); |
---|
62 | if(itMap != fMap.end()) |
---|
63 | { |
---|
64 | itMap->second.first = kTRUE; |
---|
65 | for(itSubMap = itMap->second.second.begin(); |
---|
66 | itSubMap != itMap->second.second.end(); ++itSubMap) |
---|
67 | { |
---|
68 | itSubMap->second->Clear(); |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | else |
---|
73 | { |
---|
74 | for(itMap = fMap.begin(); itMap != fMap.end(); ++itMap) |
---|
75 | { |
---|
76 | itMap->second.first = kTRUE; |
---|
77 | for(itSubMap = itMap->second.second.begin(); |
---|
78 | itSubMap != itMap->second.second.end(); ++itSubMap) |
---|
79 | { |
---|
80 | itSubMap->second->Clear(); |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | //------------------------------------------------------------------------------ |
---|
87 | |
---|
88 | TObjArray *ExRootFilter::GetSubArray(ExRootClassifier *classifier, Int_t category) |
---|
89 | { |
---|
90 | Int_t result; |
---|
91 | TObject *element; |
---|
92 | TObjArray *array; |
---|
93 | TCategoryMap::iterator itSubMap; |
---|
94 | pair<TCategoryMap::iterator, bool> pairSubMap; |
---|
95 | pair<TClassifierMap::iterator, bool> pairMap; |
---|
96 | |
---|
97 | TClassifierMap::iterator itMap = fMap.find(classifier); |
---|
98 | if(itMap == fMap.end()) |
---|
99 | { |
---|
100 | pairMap = fMap.insert(make_pair(classifier, make_pair(kTRUE, TCategoryMap()))); |
---|
101 | if(!pairMap.second) |
---|
102 | { |
---|
103 | throw runtime_error("can't insert category map"); |
---|
104 | } |
---|
105 | |
---|
106 | itMap = pairMap.first; |
---|
107 | } |
---|
108 | |
---|
109 | if(itMap->second.first) |
---|
110 | { |
---|
111 | itMap->second.first = kFALSE; |
---|
112 | fIter->Reset(); |
---|
113 | while((element = fIter->Next()) != 0) |
---|
114 | { |
---|
115 | result = classifier->GetCategory(element); |
---|
116 | if(result < 0) continue; |
---|
117 | itSubMap = itMap->second.second.find(result); |
---|
118 | if(itSubMap == itMap->second.second.end()) |
---|
119 | { |
---|
120 | array = new TObjArray(fCollection->GetSize()); |
---|
121 | pairSubMap = itMap->second.second.insert(make_pair(result, array)); |
---|
122 | if(!pairSubMap.second) |
---|
123 | { |
---|
124 | throw runtime_error("can't insert category"); |
---|
125 | } |
---|
126 | |
---|
127 | itSubMap = pairSubMap.first; |
---|
128 | } |
---|
129 | itSubMap->second->Add(element); |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | itSubMap = itMap->second.second.find(category); |
---|
134 | return (itSubMap != itMap->second.second.end()) ? itSubMap->second : 0; |
---|
135 | } |
---|
136 | |
---|
137 | //------------------------------------------------------------------------------ |
---|
138 | |
---|