57 int main(
int argc,
char **argv)
70 JParser<> zap(
"Auxiliary program for histogram operations.");
72 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
88 <<
"\n\"" << JOpera::SAME_AS_OPERATION() <<
"\" -> same as operation; or"
89 <<
"\n\"" << JOpera::SAME_AS_INPUT() <<
"\" -> same as input; else"
90 <<
"\nas specified")) = JOpera::SAME_AS_OPERATION();
95 catch(
const exception &error) {
96 FATAL(error.what() << endl);
104 DEBUG(
"Input: " << *input << endl);
109 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
113 const TRegexp regexp(input->getObjectName());
115 TIter iter(dir->GetListOfKeys());
117 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
119 const TString tag(key->GetName());
121 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
125 if (tag.Contains(regexp)) {
128 TProfile* q =
dynamic_cast<TProfile*
>(p);
131 p = q->ProjectionX();
134 if (dynamic_cast<TH1*>(p) == NULL) {
135 FATAL(
"Object " << p->GetName() <<
" not compatible with histogram operations." << endl);
138 listOfObjects.push_back(p);
146 if (listOfObjects.size() == 0) {
148 FATAL(
"Number of histograms " << listOfObjects.size() <<
" = 0." << endl);
150 }
else if (listOfObjects.size() == 1 && (opera == JOpera::Add() ||
151 opera == JOpera::Subtract() ||
152 opera == JOpera::Multiply() ||
153 opera == JOpera::Divide() ||
154 opera == JOpera::Replace())) {
158 TH1*
h1 =
dynamic_cast<TH1*
>(listOfObjects[0]);
159 TF1* f1 = (TF1*) h1->GetListOfFunctions()->First();
162 FATAL(h1->GetName() <<
" has no associated function." << endl);
165 NOTICE(h1->GetName() <<
' ' << opera <<
' ' << f1->GetName() << endl);
167 if (
name == JOpera::SAME_AS_OPERATION())
168 h3 = (TH1*) h1->Clone(opera.c_str());
169 else if (
name == JOpera::SAME_AS_INPUT())
170 h3 = (TH1*) h1->Clone(h1->GetName());
172 h3 = (TH1*) h1->Clone(
name.c_str());
174 if (opera == JOpera::Add()) {
178 }
else if (opera == JOpera::Subtract()) {
182 }
else if (opera == JOpera::Multiply()) {
186 }
else if (opera == JOpera::Divide()) {
190 }
else if (opera == JOpera::Replace()) {
197 }
else if (listOfObjects.size() == 2) {
201 TH1*
h1 =
dynamic_cast<TH1*
>(listOfObjects[0]);
202 TH1* h2 =
dynamic_cast<TH1*
>(listOfObjects[1]);
204 NOTICE(h1->GetName() <<
' ' << opera <<
' ' << h2->GetName() << endl);
206 if (
name == JOpera::SAME_AS_OPERATION())
207 h3 = (TH1*) h1->Clone(opera.c_str());
208 else if (
name == JOpera::SAME_AS_INPUT())
209 h3 = (TH1*) h1->Clone(h1->GetName());
211 h3 = (TH1*) h1->Clone(
name.c_str());
213 if (opera == JOpera::Add()) {
215 h3->Add (h1, h2, +1.0, +1.0);
217 }
else if (opera == JOpera::Subtract()) {
219 h3->Add (h1, h2, +1.0, -1.0);
221 }
else if (opera == JOpera::Multiply()) {
223 h3->Multiply(h1, h2, +1.0, +1.0);
225 }
else if (opera == JOpera::Divide()) {
227 h3->Divide (h1, h2, +1.0, +1.0);
229 }
else if (opera == JOpera::add() ||
230 opera == JOpera::subtract() ||
231 opera == JOpera::multiply() ||
232 opera == JOpera::divide()) {
234 for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
236 const Double_t x = h1->GetBinCenter (i);
237 const Double_t w1 = h1->GetBinContent(i);
239 const Int_t
j = h2->FindBin(x);
240 const Double_t w2 = h2->GetBinContent(j);
244 if (opera == JOpera::add()) {
248 }
else if (opera == JOpera::subtract()) {
252 }
else if (opera == JOpera::multiply()) {
256 }
else if (opera == JOpera::divide()) {
259 ERROR(
"Bin " << h2->GetName() <<
"[" << j <<
"] empty." << endl);
266 h3->SetBinContent(i, w3);
269 }
else if (opera == JOpera::efficiency() ||
271 opera == JOpera::sqrt()) {
273 for (Int_t i = 1; i <= h1->GetNbinsX(); ++i) {
275 const Double_t y1 = h1->GetBinContent(i);
276 const Double_t y2 = h2->GetBinContent(i);
278 Double_t w1 = h1->GetBinError(i);
279 Double_t w2 = h2->GetBinError(i);
284 if (opera == JOpera::efficiency()) {
287 ERROR(
"Bin " << h2->GetName() <<
"[" << i <<
"] empty." << endl);
299 w3 = y3 * fabs(y3 - 1.0) * sqrt(w1*w1 + w2*w2);
304 w3 = sqrt(w1*w1 + w2*w2);
311 }
else if (opera == JOpera::sqrt()) {
313 y3 = (y1+y2) * (y1-y2);
323 h3->SetBinContent(i, y3);
324 h3->SetBinError (i, w3);
328 }
else if (opera == JOpera::Add() ||
329 opera == JOpera::Multiply()) {
335 TH1*
h1 =
dynamic_cast<TH1*
>(*i);
337 NOTICE(h1->GetName() <<
' ' << opera << endl);
341 if (
name == JOpera::SAME_AS_OPERATION())
342 h3 = (TH1*) h1->Clone(opera.c_str());
343 else if (
name == JOpera::SAME_AS_INPUT())
344 h3 = (TH1*) h1->Clone(h1->GetName());
346 h3 = (TH1*) h1->Clone(
name.c_str());
350 if (opera == JOpera::Add()) {
352 h3->Add (h3, h1, +1.0, +1.0);
354 }
else if (opera == JOpera::Multiply()) {
356 h3->Multiply(h3, h1, +1.0, +1.0);
363 FATAL(
"Incompatible number of histograms " << listOfObjects.size() <<
" with option " << opera << endl);
Utility class to parse command line options.
int main(int argc, char *argv[])
then for HISTOGRAM in h0 h1
#define MAKE_STRING(A)
Make string.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
General purpose messaging.
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.