13 #include "TProfile3D.h"
59 int main(
int argc,
char **argv)
72 JParser<> zap(
"Auxiliary program for histogram operations.");
74 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
89 <<
"\n\t\"" << JOpera::SAME_AS_OPERATION() <<
"\" -> same as operation; or"
90 <<
"\n\t\"" << JOpera::SAME_AS_INPUT() <<
"\" -> same as input; else"
91 <<
"\n\t as specified") = JOpera::SAME_AS_OPERATION();
96 catch(
const exception &error) {
97 FATAL(error.what() << endl);
105 DEBUG(
"Input: " << *input << endl);
110 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
114 const TRegexp regexp(input->getObjectName());
116 TIter iter(dir->GetListOfKeys());
118 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
120 const TString tag(key->GetName());
122 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
126 if (tag.Contains(regexp) &&
isTObject(key)) {
130 if (dynamic_cast<TH3*>(p) == NULL) {
131 FATAL(
"Object " << p->GetName() <<
" not compatible with histogram operations." << endl);
134 listOfObjects.push_back(p);
142 if (listOfObjects.size() == 0) {
144 FATAL(
"Number of histograms " << listOfObjects.size() <<
" = 0." << endl);
146 }
else if (listOfObjects.size() == 1 && (opera == JOpera::Add() ||
147 opera == JOpera::Subtract() ||
148 opera == JOpera::Multiply() ||
149 opera == JOpera::Divide() ||
150 opera == JOpera::Replace())) {
154 TH3* h1 =
dynamic_cast<TH3*
>(listOfObjects[0]);
155 TF2*
f1 =
dynamic_cast<TF2*
>(h1->GetListOfFunctions()->First());
158 FATAL(h1->GetName() <<
" has no associated function." << endl);
161 NOTICE(h1->GetName() <<
' ' << opera <<
' ' << f1->GetName() << endl);
163 if (
name == JOpera::SAME_AS_OPERATION())
164 h3 = (TH3*) h1->Clone(opera.c_str());
165 else if (
name == JOpera::SAME_AS_INPUT())
166 h3 = (TH3*) h1->Clone(h1->GetName());
168 h3 = (TH3*) h1->Clone(
name.c_str());
170 if (opera == JOpera::Add()) {
174 }
else if (opera == JOpera::Subtract()) {
178 }
else if (opera == JOpera::Multiply()) {
182 }
else if (opera == JOpera::Divide()) {
186 }
else if (opera == JOpera::Replace()) {
189 h3->Fill(0.0, 0.0, 0.0);
193 }
else if (listOfObjects.size() == 2) {
195 TH3* h1 =
dynamic_cast<TH3*
>(listOfObjects[0]);
196 TH3* h2 =
dynamic_cast<TH3*
>(listOfObjects[1]);
198 NOTICE(h1->GetName() <<
' ' << opera <<
' ' << h2->GetName() << endl);
200 if (
name == JOpera::SAME_AS_OPERATION())
201 h3 = (TH3*) h1->Clone(opera.c_str());
202 else if (
name == JOpera::SAME_AS_INPUT())
203 h3 = (TH3*) h1->Clone(h1->GetName());
205 h3 = (TH3*) h1->Clone(
name.c_str());
207 if (opera == JOpera::Add()) {
209 h3->Add (h1, h2, +1.0, +1.0);
211 }
else if (opera == JOpera::Subtract()) {
213 h3->Add (h1, h2, +1.0, -1.0);
215 }
else if (opera == JOpera::Multiply()) {
217 h3->Multiply(h1, h2, +1.0, +1.0);
219 }
else if (opera == JOpera::Divide()) {
221 h3->Divide (h1, h2, +1.0, +1.0);
223 }
else if (opera == JOpera::add() ||
224 opera == JOpera::subtract() ||
225 opera == JOpera::multiply() ||
226 opera == JOpera::divide()) {
228 for (Int_t i1 = 1; i1 <= h1->GetNbinsX(); ++i1) {
229 for (Int_t i2 = 1; i2 <= h1->GetNbinsY(); ++i2) {
230 for (Int_t i3 = 1; i3 <= h1->GetNbinsZ(); ++i3) {
232 const Double_t
x = h1->GetXaxis()->GetBinCenter(i1);
233 const Double_t
y = h1->GetYaxis()->GetBinCenter(i2);
234 const Double_t z = h1->GetZaxis()->GetBinCenter(i3);
236 const Int_t j1 = h2->GetXaxis()->FindBin(x);
237 const Int_t j2 = h2->GetYaxis()->FindBin(y);
238 const Int_t j3 = h2->GetYaxis()->FindBin(z);
240 const Double_t w1 = h1->GetBinContent(i1,i2,i3);
241 const Double_t w2 = h2->GetBinContent(j1,j2,j3);
245 if (opera == JOpera::add()) {
249 }
else if (opera == JOpera::subtract()) {
253 }
else if (opera == JOpera::multiply()) {
257 }
else if (opera == JOpera::divide()) {
260 ERROR(
"Bin " << h2->GetName() <<
"[" << j1 <<
"," << j2 <<
"] empty." << endl);
267 h3->SetBinContent(i1, i2, i3, w3);
272 }
else if (opera == JOpera::efficiency() ||
273 opera == JOpera::stdev() ||
274 opera == JOpera::sqrt()) {
276 for (Int_t i1 = 1; i1 <= h1->GetNbinsX(); ++i1) {
277 for (Int_t i2 = 1; i2 <= h1->GetNbinsY(); ++i2) {
278 for (Int_t i3 = 1; i3 <= h1->GetNbinsZ(); ++i3) {
280 const Double_t y1 = h1->GetBinContent(i1,i2,i3);
281 const Double_t y2 = h2->GetBinContent(i1,i2,i3);
283 Double_t w1 = h1->GetBinError(i1,i2,i3);
284 Double_t w2 = h2->GetBinError(i1,i2,i3);
289 if (opera == JOpera::efficiency()) {
292 ERROR(
"Bin " << h2->GetName() <<
"[" << i1 <<
"," << i2 <<
"] empty." << endl);
304 w3 = y3 * fabs(y3 - 1.0) * sqrt(w1*w1 + w2*w2);
307 }
else if (opera == JOpera::stdev()) {
309 w3 = sqrt(w1*w1 + w2*w2);
316 }
else if (opera == JOpera::sqrt()) {
318 y3 = (y1+y2) * (y1-y2);
328 h3->SetBinContent(i1, i2, i3, y3);
329 h3->SetBinError (i1, i2, i3, w3);
335 }
else if (opera == JOpera::Add() ||
336 opera == JOpera::Multiply()) {
340 TH3* h1 =
dynamic_cast<TH3*
>(*i);
342 NOTICE(h1->GetName() <<
' ' << opera << endl);
346 if (
name == JOpera::SAME_AS_OPERATION())
347 h3 = (TH3*) h1->Clone(opera.c_str());
348 else if (
name == JOpera::SAME_AS_INPUT())
349 h3 = (TH3*) h1->Clone(h1->GetName());
351 h3 = (TH3*) h1->Clone(
name.c_str());
355 if (opera == JOpera::Add()) {
357 h3->Add (h3, h1, +1.0, +1.0);
359 }
else if (opera == JOpera::Multiply()) {
361 h3->Multiply(h3, h1, +1.0, +1.0);
368 FATAL(
"Incompatible number of histograms " << listOfObjects.size() <<
" with option " << opera << endl);
Utility class to parse command line options.
int main(int argc, char *argv[])
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
General purpose messaging.
then fatal The output file must have the wildcard in the name
Utility class to parse command line options.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
#define DEBUG(A)
Message macros.