12 #include "TApplication.h"
16 #include "TAttMarker.h"
25 #include "TGraphErrors.h"
26 #include "TMultiGraph.h"
62 inline bool setLogX(
TObject*
object)
66 T* p =
dynamic_cast<T*
>(object);
82 public JEquals<JLegend>
98 JLegend(
const std::string& location,
99 const Double_t factor = 1.0) :
111 return (location !=
"" && factor != 0.0);
120 bool equals(
const JLegend& legend)
const
122 return (this->location == legend.location);
132 friend inline std::istream&
operator>>(std::istream&
in, JLegend& legend)
134 in >> legend.location;
136 if (!(in >> legend.factor)) {
153 friend inline std::ostream&
operator<<(std::ostream& out,
const JLegend& legend)
155 return out << legend.location <<
' ' << legend.factor;
158 std::string location;
169 inline Ssiz_t getWidth(
const TString& buffer)
173 for (Ssiz_t
i = 0;
i != buffer.Length(); ++
i) {
175 if (buffer(
i) ==
' ')
177 else if (buffer(
i) >=
'A' && buffer(
i) <=
'Z')
179 else if (buffer(
i) >=
'a' && buffer(
i) <=
'z')
181 else if (buffer(
i) >=
'0' && buffer(
i) <=
'9')
183 else if (buffer(
i) ==
'.' || buffer(
i) ==
':' || buffer(
i) ==
';' || buffer(
i) ==
',' || buffer(
i) ==
'\'')
193 const std::string MASTER =
"__H__";
195 const char*
const JName_t =
"?";
196 const char*
const JTitle_t =
"%";
206 int main(
int argc,
char **argv)
211 typedef JRange<double> JRange_t;
245 JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
247 zap[
'f'] =
make_field(inputFile,
"<input file>:<object name>");
249 zap[
'w'] =
make_field(canvas,
"size of canvas <nx>x<ny> [pixels]") = JCanvas(500, 500);
252 zap[
'L'] =
make_field(legend,
"position legend e.g. TR [factor]") = JLegend(), JLegend(
"TL"), JLegend(
"TR"), JLegend(
"BR"), JLegend(
"BL");
253 zap[
'x'] =
make_field(
X,
"abscissa range") = JRange_t::DEFAULT_RANGE();
254 zap[
'y'] =
make_field(
Y,
"ordinate range") = JRange_t::DEFAULT_RANGE();
255 zap[
'z'] =
make_field(
Z,
"ordinate range of projection)") = JRange_t::DEFAULT_RANGE();
256 zap[
'X'] =
make_field(logx,
"logarithmic x-axis (-XX log10 axis)");
257 zap[
'Y'] =
make_field(logy,
"logarithmic y-axis");
258 zap[
'Z'] =
make_field(logz,
"logarithmic y-axis; after projection");
260 zap[
'>'] =
make_field(xLabel,
"x-axis label") =
"";
261 zap[
'^'] =
make_field(yLabel,
"y-axis label") =
"";
262 zap[
'C'] =
make_field(drawLine,
"draw line (-C black-and-white -CC colour)");
264 zap[
'l'] =
make_field(lineWidth,
"line width") = 2;
265 zap[
'S'] =
make_field(markerSize,
"marker size") = 1.0;
266 zap[
'O'] =
make_field(option,
"plotting option") =
"";
268 zap[
'B'] =
make_field(batch,
"batch processing");
269 zap[
'T'] =
make_field(title,
"graphics title ("
270 <<
"\"" << JName_t <<
"\" -> ROOT name; "
271 <<
"\"" << JTitle_t <<
"\" -> ROOT title)") =
"KM3NeT preliminary";
273 zap[
'g'] =
make_field(group,
"group colour codes of objects") = 1;
274 zap[
't'] =
make_field(xTimeFormat,
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") =
"";
279 catch(
const exception &error) {
280 FATAL(error.what() << endl);
284 gROOT->SetBatch(batch);
286 TApplication* tp =
new TApplication(
"user", NULL, NULL);
287 TCanvas* cv =
new TCanvas(
"c1",
"c1", canvas.x, canvas.y);
289 unique_ptr<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh(),
parameters));
292 gStyle->SetTitleOffset(gStyle->GetTitleOffset() * 1.3,
"Y");
295 gROOT->SetStyle(
"gplot");
298 cv->SetFillStyle(4000);
299 cv->SetFillColor(kWhite);
308 Double_t
xmin = numeric_limits<double>::max();
309 Double_t
xmax = numeric_limits<double>::lowest();
311 Double_t ymin = numeric_limits<double>::max();
312 Double_t ymax = numeric_limits<double>::lowest();
321 logy = (logy || logz);
336 DEBUG(
"Input: " << *input << endl);
341 ERROR(
"File: " << input->getFullFilename() <<
" not opened." << endl);
345 const TRegexp regexp(input->getObjectName());
347 TIter iter(dir->GetListOfKeys());
349 for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
351 const TString tag(key->GetName());
353 DEBUG(
"Key: " << tag <<
" match = " << tag.Contains(regexp) << endl);
357 if (tag.Contains(regexp) &&
isTObject(key)) {
359 if (title == JName_t) {
360 title = key->GetName();
361 }
else if (title == JTitle_t) {
362 title = key->GetTitle();
365 JRootObject object(key->ReadObj());
377 line.SetLineColor(marker.GetMarkerColor());
383 TProfile& h1 =
dynamic_cast<TProfile&
>(*object);
385 object = h1.ProjectionX();
391 TH2& h2 =
dynamic_cast<TH2&
>(*object);
397 h2.GetYaxis()->FindBin(
Z.getLowerLimit()),
398 h2.GetYaxis()->FindBin(
Z.getUpperLimit()) - 1);
402 h2.GetYaxis()->GetNbins());
408 h2.GetXaxis()->FindBin(
Z.getLowerLimit()),
409 h2.GetXaxis()->FindBin(
Z.getUpperLimit()) - 1);
413 h2.GetXaxis()->GetNbins());
417 ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
426 TH3& h3 =
dynamic_cast<TH3&
>(*object);
442 ERROR(
"For 3D histograms, use option option -P for projections or use JPlot2D -P <projection>" << endl);
452 if (dynamic_cast<TMarker*>(
object.
get()) == NULL) {
453 dynamic_cast<TAttMarker&
>(*object) = marker;
459 if (dynamic_cast<TLine*>(
object.
get()) == NULL) {
460 dynamic_cast<TAttLine&
> (*object) = line;
469 TAttFill& fill =
dynamic_cast<TAttFill&
>(*object);
471 fill.SetFillColor(marker.GetMarkerColor());
482 TH1& h1 =
dynamic_cast<TH1&
>(*object);
484 for (
int i = 1;
i <= h1.GetNbinsX(); ++
i) {
485 h1.SetBinError(
i, 0.0);
492 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
494 for (Int_t
i = 0;
i != g1.GetN(); ++
i) {
506 TH1& h1 =
dynamic_cast<TH1&
>(*object);
508 h1.SetStats(stats != -1);
510 xmin = min(
xmin, h1.GetXaxis()->GetXmin());
511 xmax = max(xmax, h1.GetXaxis()->GetXmax());
512 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
513 ymax = max(ymax, h1.GetMaximum());
515 if (!logy && h1.GetListOfFunctions() != NULL) {
516 for (unique_ptr<TIterator>
iterator(h1.GetListOfFunctions()->MakeIterator()); TF1*
f1 = (TF1*)
iterator->Next(); ) {
517 ymin = min(ymin, f1->GetMinimum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
518 ymax = max(ymax, f1->GetMaximum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
526 TGraph&
g1 =
dynamic_cast<TGraph&
>(*object);
528 for (Int_t
i = 0;
i != g1.GetN(); ++
i) {
531 xmax = max(xmax, g1.GetX()[
i]);
533 if (!logy || g1.GetY()[
i] > 0.0) {
534 ymin = min(ymin, g1.GetY()[
i]);
535 ymax = max(ymax, g1.GetY()[
i]);
543 TGraphErrors&
g1 =
dynamic_cast<TGraphErrors&
>(*object);
545 for (Int_t
i = 0;
i != g1.GetN(); ++
i) {
546 if (!logy || g1.GetY()[
i] - g1.GetEY()[
i] > 0.0) { ymin = min(ymin, g1.GetY()[
i] - g1.GetEY()[
i]); }
547 if (!logy || g1.GetY()[
i] + g1.GetEY()[
i] > 0.0) { ymax = max(ymax, g1.GetY()[
i] + g1.GetEY()[
i]); }
554 TMultiGraph& m1 =
dynamic_cast<TMultiGraph&
>(*object);
556 for (TIter i1(m1.GetListOfGraphs()); TGraph*
g1 =
dynamic_cast<TGraph*
>(i1()); ) {
558 for (Int_t
i = 0;
i != g1->GetN(); ++
i) {
561 xmax = max(xmax, g1->GetX()[
i]);
563 if (!logy || g1->GetY()[
i] > 0.0) {
564 ymin = min(ymin, g1->GetY()[
i]);
565 ymax = max(ymax, g1->GetY()[
i]);
574 TF2& f2 =
dynamic_cast<TF2&
>(*object);
577 TString formula = f2.GetExpFormula();
578 TString _z_ = TString::Format(
"%f", 0.5 * (
Z.getLowerLimit() +
Z.getUpperLimit()));
585 f2.GetRange(__xmin, __ymin, __xmax, __ymax);
589 formula.ReplaceAll(
"y", _z_);
593 f1->SetRange(__xmin, __xmax);
597 formula.ReplaceAll(
"x", _z_);
598 formula.ReplaceAll(
"y",
"x");
602 f1->SetRange(__ymin, __ymax);
606 ERROR(
"For 2D functions, use option option -P for projections or use JPlot2D" << endl);
611 DEBUG(
"TF1: " << f1->GetExpFormula() << endl);
613 f1->SetParameters(f2.GetParameters());
621 TF1&
f1 =
dynamic_cast<TF1&
>(*object);
626 f1.GetRange(__xmin, __xmax);
630 ymin = min(ymin, f1.GetMinimum());
631 ymax = max(ymax, f1.GetMaximum());
637 THStack& hs =
dynamic_cast<THStack&
>(*object);
639 NOTICE(
"THStack" << endl);
641 unique_ptr<TIterator>
iterator(hs.GetHists()->MakeIterator());
645 TH1& h1 =
dynamic_cast<TH1&
>(*i);
647 NOTICE(
"TH1[" << index <<
"] " << h1.GetName() << endl);
649 xmin = min(xmin, h1.GetXaxis()->GetXmin());
650 xmax = max(xmax, h1.GetXaxis()->GetXmax());
652 ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
653 ymax = max(ymax, h1.GetMaximum());
656 h1.SetLineColor(kBlack);
665 TLine& h1 =
dynamic_cast<TLine&
>(*object);
668 xmax = max(xmax, h1.GetX2());
669 ymin = min(ymin, h1.GetY1());
670 ymax = max(ymax, h1.GetY2());
676 for (TString buffer[] = {
object.getLabel(), input->getFilename().c_str(),
"" }, *
i = buffer; *
i !=
""; ++
i) {
678 *
i = (*i)(TRegexp(
"\\[.*\\]"));
680 if (
i->Length() > 2) {
681 object.setLabel((*
i)(1,
i->Length() - 2));
685 DEBUG(
"Add object: " << tag <<
" with label <" <<
object.
getLabel() <<
">" << endl);
688 master =
dynamic_cast<TH1*
>(
object.get());
691 listOfObjects.push_back(
object);
696 if (listOfObjects.empty()) {
697 ERROR(
"Nothing to draw." << endl);
706 TH1* h1 =
dynamic_cast<TH1*
> (
i->get());
707 TGraph*
g1 =
dynamic_cast<TGraph*
> (
i->get());
708 TMultiGraph* m1 =
dynamic_cast<TMultiGraph*
>(
i->get());
709 TAttLine* ls =
dynamic_cast<TAttLine*
> (
i->get());
717 iterator.reset(h1->GetListOfFunctions()->MakeIterator());
719 }
else if (g1 != NULL) {
721 iterator.reset(g1->GetListOfFunctions()->MakeIterator());
723 }
else if (m1 != NULL) {
725 for (TIter i1(m1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
726 for (TIter i2(gi->GetListOfFunctions()); TF1*
fi =
dynamic_cast<TF1*
>(i2()); ) {
731 iterator.reset(list.MakeIterator());
734 if (iterator != NULL) {
736 Double_t x1[] = { numeric_limits<Double_t>::max(), numeric_limits<Double_t>::max() };
737 Double_t x2[] = { numeric_limits<Double_t>::lowest(), numeric_limits<Double_t>::lowest() };
739 for (
int nf = 0,
ns = 0, nc = 1; TF1*
f1 = (TF1*) iterator->Next(); ++nf) {
741 f1->GetRange(x1[1], x2[1]);
746 if (listOfObjects.size() == 1) {
748 if (x1[0] == x1[1] &&
761 f1->SetLineColor(ls->GetLineColor());
785 xmin =
X.getLowerLimit();
786 xmax =
X.getUpperLimit();
790 ymin =
Y.getLowerLimit();
791 ymax =
Y.getUpperLimit();
792 }
else if (ymax > ymin) {
798 if (!listOfObjects.empty()) {
806 for (Int_t
i = 1;
i <=
master->GetXaxis()->GetNbins(); ++
i) {
807 master->SetBinContent(
i, ymin);
814 TText* p =
new TText(0.5, 0.5,
MAKE_CSTRING(
"No data"));
822 if (logx) { gPad->SetLogx(); }
823 if (logy) { gPad->SetLogy(); }
826 master->SetTitle(title.c_str());
835 if (xLabel !=
"") {
master->GetXaxis()->SetTitle(xLabel.c_str());
master->GetXaxis()->CenterTitle(
true); }
836 if (yLabel !=
"") {
master->GetYaxis()->SetTitle(yLabel.c_str());
master->GetYaxis()->CenterTitle(
true); }
841 master->GetYaxis()->SetMoreLogLabels( logy &&
log10(ymax/ymin) < 2);
842 master->GetYaxis()->SetNoExponent ( logy &&
log10(ymax/ymin) < 2);
845 master->SetNdivisions(
i->second,
i->first.c_str());
848 if (xTimeFormat !=
"") {
850 master->GetXaxis()->SetTimeDisplay(1);
852 if (xTimeFormat ==
"utc") {
853 master->GetXaxis()->SetTimeFormat(
"#splitline{}{#splitline{%d-%m-%y}{ %H:%M}}");
854 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
855 }
else if (xTimeFormat ==
"UTC") {
856 master->GetXaxis()->SetTimeFormat(
"%d-%m-%y");
857 master->GetXaxis()->SetTimeOffset(0.0,
"gmt");
859 master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
863 master->Draw(option.c_str());
868 if (dynamic_cast<TH1*>(
i->get()) !=
master) {
869 if (setLogX<TH1> (
i->get())) {}
870 else if (setLogX<TF1> (
i->get())) {}
871 else if (setLogX<TGraphErrors>(
i->get())) {}
872 else if (setLogX<TGraph> (
i->get())) {}
873 else if (setLogX<TMultiGraph> (
i->get())) {}
874 else if (setLogX<TLine> (
i->get())) {}
875 else if (setLogX<TEllipse> (
i->get())) {}
880 if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
881 if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
884 gStyle->SetOptStat(stats);
886 gStyle->SetOptFit(kFALSE);
891 DEBUG(
"Draw " << (*i)->GetName() <<
' ' << (*i)->GetTitle() << endl);
893 string buffer(option);
901 TF1*
f1 =
dynamic_cast<TF1*
> (
i->get());
902 TGraph*
g1 =
dynamic_cast<TGraph*
> (
i->get());
903 TMultiGraph* q1 =
dynamic_cast<TMultiGraph*
>(
i->get());
910 if (g1->GetN() > 1 && drawLine)
918 for (TIter i1(q1->GetListOfGraphs()); TGraph* gi =
dynamic_cast<TGraph*
>(i1()); ) {
920 string zbuf = buffer;
922 if (gi->GetN() > 1 && drawLine)
927 gi->Draw(zbuf.c_str());
932 (*i).Draw(buffer.c_str());
938 if (legend.is_valid()) {
941 group = listOfObjects.size();
944 Ssiz_t height = listOfObjects.size()/group;
948 width = max(width, getWidth(
i->getLabel()));
951 TLegend* lg =
getLegend(width, height, legend.location, legend.factor);
953 for (
size_t i = 0;
i < listOfObjects.size();
i += group) {
955 const JRootObject&
object = listOfObjects[
i];
957 if (dynamic_cast<TMarker*>(
object.
get()) == NULL &&
958 dynamic_cast<TLine*
> (
object.get()) == NULL &&
959 dynamic_cast<TText*> (
object.
get()) == NULL) {
977 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.
bool is_valid(const json &js)
Check validity of JSon data.
Utility class to parse parameter values.
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
#define MAKE_CSTRING(A)
Make C-string.
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
Utility class to parse parameter values.
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.
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.
then fatal The output file must have the wildcard in the e g root fi 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
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.
#define DEBUG(A)
Message macros.
Double_t g1(const Double_t x)
Function.