11 #include "TApplication.h"
15 #include "TAttMarker.h"
24 #include "TGraphErrors.h"
25 #include "TMultiGraph.h"
61 inline bool setLogX(
TObject*
object)
65 T* p =
dynamic_cast<T*
>(object);
80 public JEquals<JLegend>
96 JLegend(
const std::string& location,
97 const Double_t factor = 1.0) :
109 return (location !=
"" && factor != 0.0);
118 bool equals(
const JLegend& legend)
const
120 return (this->location == legend.location);
130 friend inline std::istream&
operator>>(std::istream&
in, JLegend& legend)
132 in >> legend.location;
134 if (!(in >> legend.factor)) {
151 friend inline std::ostream&
operator<<(std::ostream& out,
const JLegend& legend)
153 return out << legend.location <<
' ' << legend.factor;
156 std::string location;
160 const std::string MASTER =
"__H__";
162 const char*
const JName_t =
"?";
163 const char*
const JTitle_t =
"%";
173 int main(
int argc,
char **argv)
178 typedef JRange<double> JRange_t;
209 JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
211 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
213 zap[
'w'] =
make_field(canvas,
"size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
215 zap[
'L'] =
make_field(legend,
"position legend e.g. TR [factor]") = JLegend(), JLegend(
"TL"), JLegend(
"TR"), JLegend(
"BR"), JLegend(
"BL");
216 zap[
'x'] =
make_field(
X,
"abscissa range") = JRange_t();
217 zap[
'y'] =
make_field(
Y,
"ordinate range") = JRange_t();
218 zap[
'z'] =
make_field(
Z,
"ordinate range of projection)") = JRange_t();
219 zap[
'X'] =
make_field(logx,
"logarithmic x-axis (-XX log10 axis)");
220 zap[
'Y'] =
make_field(logy,
"logarithmic y-axis");
221 zap[
'Z'] =
make_field(logz,
"logarithmic y-axis; after projection");
222 zap[
'P'] =
make_field(project,
"projection") =
'\0',
'x',
'X',
'y',
'Y';
223 zap[
'>'] =
make_field(xLabel,
"x-axis label") =
"";
224 zap[
'^'] =
make_field(yLabel,
"y-axis label") =
"";
227 zap[
'l'] =
make_field(lineWidth,
"line width") = 2;
228 zap[
'S'] =
make_field(markerSize,
"marker size") = 1.0;
229 zap[
'O'] =
make_field(option,
"plotting option") =
"";
231 zap[
'B'] =
make_field(batch,
"batch processing");
232 zap[
'T'] =
make_field(title,
"graphics title ("
233 <<
"\"" << JName_t <<
"\" -> ROOT name; "
234 <<
"\"" << JTitle_t <<
"\" -> ROOT title)") =
"KM3NeT preliminary";
236 zap[
'g'] =
make_field(group,
"group colour codes of objects") = 1;
237 zap[
't'] =
make_field(xTimeFormat,
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") =
"";
242 catch(
const exception &error) {
243 FATAL(error.what() << endl);
247 gROOT->SetBatch(batch);
249 TApplication* tp =
new TApplication(
"user", NULL, NULL);
250 TCanvas* cv =
new TCanvas(
"c1",
"c1", canvas.x, canvas.y);
252 JSinglePointer<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh()));
254 gROOT->SetStyle(
"gplot");
258 cv->SetFillStyle(4000);
259 cv->SetFillColor(kWhite);
268 Double_t
xmin = numeric_limits<double>::max();
269 Double_t
xmax = numeric_limits<double>::lowest();
271 Double_t ymin = numeric_limits<double>::max();
272 Double_t ymax = numeric_limits<double>::lowest();
277 const bool px = (project ==
'x' || project ==
'X');
278 const bool py = (project ==
'y' || project ==
'Y');
279 const bool pz = (project ==
'z' || project ==
'Z');
281 logy = (logy || logz);
296 DEBUG(
"Input: " << *input << endl);
301 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
305 const TRegexp regexp(input->getObjectName());
307 TIter iter(dir->GetListOfKeys());
309 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
311 const TString tag(key->GetName());
313 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
317 if (tag.Contains(regexp) &&
isTObject(key)) {
319 if (title == JName_t) {
320 title = key->GetName();
321 }
else if (title == JTitle_t) {
322 title = key->GetTitle();
325 JRootObject object(key->ReadObj());
338 line.SetLineColor(marker.GetMarkerColor());
344 TProfile&
h1 =
dynamic_cast<TProfile&
>(*object);
346 object = h1.ProjectionX();
352 TH2& h2 =
dynamic_cast<TH2&
>(*object);
358 h2.GetYaxis()->FindBin(
Z.getLowerLimit()),
359 h2.GetYaxis()->FindBin(
Z.getUpperLimit()) - 1);
363 h2.GetYaxis()->GetNbins());
369 h2.GetXaxis()->FindBin(
Z.getLowerLimit()),
370 h2.GetXaxis()->FindBin(
Z.getUpperLimit()) - 1);
374 h2.GetXaxis()->GetNbins());
378 ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
387 TH3& h3 =
dynamic_cast<TH3&
>(*object);
403 ERROR(
"For 3D histograms, use option option -P for projections or use JPlot2D -P <projection>" << endl);
413 if (dynamic_cast<TMarker*>(
object.
get()) == NULL) {
414 dynamic_cast<TAttMarker&
>(*object) = marker;
420 if (dynamic_cast<TLine*>(
object.
get()) == NULL) {
421 dynamic_cast<TAttLine&
> (*object) = line;
430 TAttFill& fill =
dynamic_cast<TAttFill&
>(*object);
432 fill.SetFillColor(marker.GetMarkerColor());
443 TH1&
h1 =
dynamic_cast<TH1&
>(*object);
445 for (
int i = 1; i <= h1.GetNbinsX(); ++i) {
446 h1.SetBinError(i, 0.0);
453 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
455 for (Int_t i = 0; i != g1.GetN(); ++i) {
467 TH1&
h1 =
dynamic_cast<TH1&
>(*object);
469 h1.SetStats(stats != -1);
471 xmin = min(
xmin, h1.GetXaxis()->GetXmin());
472 xmax = max(xmax, h1.GetXaxis()->GetXmax());
473 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
474 ymax = max(ymax, h1.GetMaximum());
480 TGraph&
g1 =
dynamic_cast<TGraph&
>(*object);
482 for (Int_t i = 0; i != g1.GetN(); ++i) {
485 xmax = max(xmax, g1.GetX()[i]);
487 if (!logy || g1.GetY()[i] > 0.0) {
488 ymin = min(ymin, g1.GetY()[i]);
489 ymax = max(ymax, g1.GetY()[i]);
497 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
499 for (Int_t i = 0; i != g1.GetN(); ++i) {
500 if (!logy || g1.GetY()[i] - g1.GetEY()[i] > 0.0) { ymin = min(ymin, g1.GetY()[i] - g1.GetEY()[i]); }
501 if (!logy || g1.GetY()[i] + g1.GetEY()[i] > 0.0) { ymax = max(ymax, g1.GetY()[i] + g1.GetEY()[i]); }
508 TMultiGraph& m1 =
dynamic_cast<TMultiGraph&
>(*object);
510 for (TIter i1(m1.GetListOfGraphs()); TGraph*
g1 =
dynamic_cast<TGraph*
>(i1()); ) {
512 for (Int_t i = 0; i != g1->GetN(); ++i) {
515 xmax = max(xmax, g1->GetX()[i]);
517 if (!logy || g1->GetY()[i] > 0.0) {
518 ymin = min(ymin, g1->GetY()[i]);
519 ymax = max(ymax, g1->GetY()[i]);
528 TF2& f2 =
dynamic_cast<TF2&
>(*object);
531 TString formula = f2.GetExpFormula();
532 TString _z_ = TString::Format(
"%f", 0.5 * (
Z.getLowerLimit() +
Z.getUpperLimit()));
539 f2.GetRange(__xmin, __ymin, __xmax, __ymax);
543 formula.ReplaceAll(
"y", _z_);
547 f1->SetRange(__xmin, __xmax);
551 formula.ReplaceAll(
"x", _z_);
552 formula.ReplaceAll(
"y",
"x");
556 f1->SetRange(__ymin, __ymax);
560 ERROR(
"For 2D functions, use option option -P for projections or use JPlot2D" << endl);
565 DEBUG(
"TF1: " << f1->GetExpFormula() << endl);
567 f1->SetParameters(f2.GetParameters());
575 TF1&
f1 =
dynamic_cast<TF1&
>(*object);
580 f1.GetRange(__xmin, __xmax);
584 ymin = min(ymin, f1.GetMinimum());
585 ymax = max(ymax, f1.GetMaximum());
591 THStack& hs =
dynamic_cast<THStack&
>(*object);
593 NOTICE(
"THStack" << endl);
595 TIterator*
iterator = hs.GetHists()->MakeIterator();
597 for (
size_t index = 1;
TObject* i = iterator->Next(); ++index) {
599 TH1&
h1 =
dynamic_cast<TH1&
>(*i);
601 NOTICE(
"TH1[" << index <<
"] " << h1.GetName() << endl);
603 xmin = min(xmin, h1.GetXaxis()->GetXmin());
604 xmax = max(xmax, h1.GetXaxis()->GetXmax());
606 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
607 ymax = max(ymax, h1.GetMaximum());
610 h1.SetLineColor(kBlack);
619 TLine&
h1 =
dynamic_cast<TLine&
>(*object);
622 xmax = max(xmax, h1.GetX2());
623 ymin = min(ymin, h1.GetY1());
624 ymax = max(ymax, h1.GetY2());
630 for (TString buffer[] = {
object.getLabel(), input->getFilename().c_str(),
"" }, *i = buffer; *i !=
""; ++i) {
632 *i = (*i)(TRegexp(
"\\[.*\\]"));
634 if (i->Length() > 2) {
635 object.setLabel((*i)(1, i->Length() - 2));
639 DEBUG(
"Add object: " << tag <<
" with label <" <<
object.
getLabel() <<
">" << endl);
642 master =
dynamic_cast<TH1*
>(
object.get());
645 listOfObjects.push_back(
object);
650 if (listOfObjects.empty()) {
651 ERROR(
"Nothing to draw." << endl);
660 TH1*
h1 =
dynamic_cast<TH1*
> (i->get());
661 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
662 TMultiGraph* m1 =
dynamic_cast<TMultiGraph*
>(i->get());
663 TAttLine* ls =
dynamic_cast<TAttLine*
> (i->get());
670 iterator = h1->GetListOfFunctions()->MakeIterator();
672 }
else if (g1 != NULL) {
674 iterator = g1->GetListOfFunctions()->MakeIterator();
676 }
else if (m1 != NULL) {
678 for (TIter i1(m1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
679 for (TIter i2(gi->GetListOfFunctions()); TF1* fi =
dynamic_cast<TF1*
>(i2()); ) {
684 iterator = list.MakeIterator();
687 if (iterator != NULL) {
689 Double_t x1[] = { numeric_limits<Double_t>::max(), numeric_limits<Double_t>::max() };
690 Double_t x2[] = { numeric_limits<Double_t>::lowest(), numeric_limits<Double_t>::lowest() };
692 for (
int nf = 0, ns = 0, nc = 1; TF1*
f1 = (TF1*) iterator->Next(); ++nf) {
694 f1->GetRange(x1[1], x2[1]);
699 if (listOfObjects.size() == 1) {
701 if (x1[0] == x1[1] &&
714 f1->SetLineColor(ls->GetLineColor());
737 if (
X != JRange_t()) {
738 xmin =
X.getLowerLimit();
739 xmax =
X.getUpperLimit();
742 if (
Y != JRange_t()) {
743 ymin =
Y.getLowerLimit();
744 ymax =
Y.getUpperLimit();
751 if (!listOfObjects.empty()) {
759 for (Int_t i = 1; i <=
master->GetXaxis()->GetNbins(); ++i) {
760 master->SetBinContent(i, ymin);
767 TText* p =
new TText(0.5, 0.5,
MAKE_CSTRING(
"No data"));
775 if (logx) { gPad->SetLogx(); }
776 if (logy) { gPad->SetLogy(); }
779 master->SetTitle(title.c_str());
788 if (xLabel !=
"") {
master->GetXaxis()->SetTitle(xLabel.c_str());
master->GetXaxis()->CenterTitle(
true); }
789 if (yLabel !=
"") {
master->GetYaxis()->SetTitle(yLabel.c_str());
master->GetYaxis()->CenterTitle(
true); }
794 master->GetYaxis()->SetMoreLogLabels( logy &&
log10(ymax/ymin) < 2);
795 master->GetYaxis()->SetNoExponent ( logy &&
log10(ymax/ymin) < 2);
798 master->SetNdivisions(i->second, i->first.c_str());
801 if (xTimeFormat !=
"") {
803 master->GetXaxis()->SetTimeDisplay(1);
805 if (xTimeFormat ==
"utc") {
806 master->GetXaxis()->SetTimeFormat(
"#splitline{}{#splitline{%d-%m-%y}{ %H:%M}}");
807 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
808 }
else if (xTimeFormat ==
"UTC") {
809 master->GetXaxis()->SetTimeFormat(
"%d-%m-%y");
810 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
812 master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
816 master->Draw(option.c_str());
821 if (dynamic_cast<TH1*>(i->get()) !=
master) {
822 if (setLogX<TH1> (i->get())) {}
823 else if (setLogX<TF1> (i->get())) {}
824 else if (setLogX<TGraphErrors>(i->get())) {}
825 else if (setLogX<TGraph> (i->get())) {}
826 else if (setLogX<TMultiGraph> (i->get())) {}
827 else if (setLogX<TLine> (i->get())) {}
828 else if (setLogX<TEllipse> (i->get())) {}
833 if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
834 if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
837 gStyle->SetOptStat(stats);
839 gStyle->SetOptFit(kFALSE);
844 DEBUG(
"Draw " << (*i)->GetName() <<
' ' << (*i)->GetTitle() << endl);
846 string buffer(option);
854 TF1*
f1 =
dynamic_cast<TF1*
> (i->get());
855 TGraph*
g1 =
dynamic_cast<TGraph*
> (i->get());
856 TMultiGraph* q1 =
dynamic_cast<TMultiGraph*
>(i->get());
863 if (g1->GetN() > 1 && drawLine)
871 for (TIter i1(q1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
873 string zbuf = buffer;
875 if (gi->GetN() > 1 && drawLine)
880 gi->Draw(zbuf.c_str());
885 (*i).Draw(buffer.c_str());
891 if (legend.is_valid()) {
893 Ssiz_t height = listOfObjects.size();
897 width = max(width, i->getLabel().Length());
900 TLegend* lg =
getLegend(width, height, legend.location, legend.factor);
903 if (dynamic_cast<TMarker*>(i->get()) == NULL &&
904 dynamic_cast<TLine*> (i->get()) == NULL &&
905 dynamic_cast<TText*> (i->get()) == NULL) {
906 lg->AddEntry(*i,
" " + i->getLabel(),
isTAttLine(*i) ?
"L" :
"LPF");
923 return (
master != NULL ? 0 : 1);
Utility class to parse command line options.
then echo Test string reversed by master(hit< return > to continue)." $DIR/JProcess -c "$DIR/JEcho" -rC fi if (( 1 ))
int main(int argc, char *argv[])
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
#define MAKE_CSTRING(A)
Make C-string.
then for HISTOGRAM in h0 h1
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
const JPolynome f1(1.0, 2.0, 3.0)
Function.
bool isTAttLine(const TObject *object)
Get drawing option of TH1.
I/O formatting auxiliaries.
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
T & getInstance(const T &object)
Get static instance from temporary object.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
set_variable E_E log10(E_{fit}/E_{#mu})"
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Template definition of auxiliary base class for comparison of data structures.
bool is_valid(const json &js)
Check validity of JSon data.
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option, const Double_t factor=1.0)
Get legend.
General purpose messaging.
static const char LABEL_TERMINATOR
label terminator
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Auxiliary class to define a range between two values.
Utility class to parse command line options.
Auxiliary class to handle multiple boolean-like I/O.
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
no fit printf nominal n $STRING awk v X
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
do set_variable MODULE getModule a $WORKDIR detector_a datx L $STRING JEditDetector a $WORKDIR detector_a datx M $MODULE setz o $WORKDIR detector_a datx JEditDetector a $WORKDIR detector_b datx M $MODULE setz o $WORKDIR detector_b datx done echo Output stored at $WORKDIR detector_a datx and $WORKDIR tripod_a txt JDrawDetector2D a $WORKDIR detector_a datx a $WORKDIR detector_b datx L BL o detector $FORMAT $BATCH JDrawDetector2D T $WORKDIR tripod_a txt T $WORKDIR tripod_b txt L BL o tripod $FORMAT $BATCH JCompareDetector a $WORKDIR detector_a datx b $WORKDIR detector_b datx o $WORKDIR abc root &dev null for KEY in X Y Z
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
#define DEBUG(A)
Message macros.
Double_t g1(const Double_t x)
Function.