12 #include "TApplication.h" 
   16 #include "TAttMarker.h" 
   25 #include "TGraphErrors.h" 
   26 #include "TGraphAsymmErrors.h" 
   27 #include "TMultiGraph.h" 
   63   inline bool setLogX(
TObject* 
object)
 
   67     T* p = 
dynamic_cast<T*
>(object);
 
   99     JLegend(
const std::string& location,
 
  100             const Double_t     factor = 1.0) :
 
  112       return (location != 
"" && factor != 0.0);
 
  121     bool equals(
const JLegend& legend)
 const 
  123       return (this->location == legend.location);
 
  133     friend inline std::istream& 
operator>>(std::istream& in, JLegend& legend)
 
  135       in >> legend.location;
 
  137       if (!(in >> legend.factor)) {
 
  154     friend inline std::ostream& 
operator<<(std::ostream& out, 
const JLegend& legend)
 
  156       return out << legend.location << 
' ' << legend.factor;
 
  159     std::string location;
 
  170   inline Ssiz_t getWidth(
const TString& buffer)
 
  174     for (Ssiz_t i = 0; i != buffer.Length(); ++i) {
 
  176       if (buffer(i) == 
' ')
 
  178       else if (buffer(i) >= 
'A' && buffer(i) <= 
'Z')
 
  180       else if (buffer(i) >= 
'a' && buffer(i) <= 
'z')
 
  182       else if (buffer(i) >= 
'0' && buffer(i) <= 
'9')
 
  184       else if (buffer(i) == 
'.' || buffer(i) == 
':' || buffer(i) == 
';' || buffer(i) == 
',' || buffer(i) == 
'\'')
 
  194   const std::string MASTER   = 
"__H__"; 
 
  196   const char* 
const JName_t  = 
"?";     
 
  197   const char* 
const JTitle_t = 
"%";     
 
  207 int main(
int argc, 
char **argv)
 
  246     JParser<> zap(
"General purpose plot program for 1D ROOT objects.");
 
  248     zap[
'f'] = 
make_field(inputFile,  
"<input file>:<object name>");
 
  250     zap[
'w'] = 
make_field(canvas,     
"size of canvas <nx>x<ny> [pixels]")  = 
JCanvas(500, 500);
 
  253     zap[
'L'] = 
make_field(legend,     
"position legend e.g. TR [factor]")   = JLegend(), JLegend(
"TL"), JLegend(
"TR"), JLegend(
"BR"), JLegend(
"BL");
 
  254     zap[
'x'] = 
make_field(X,          
"abscissa range")                     = JRange_t::DEFAULT_RANGE();
 
  255     zap[
'y'] = 
make_field(Y,          
"ordinate range")                     = JRange_t::DEFAULT_RANGE();
 
  256     zap[
'z'] = 
make_field(Z,          
"ordinate range of projection)")      = JRange_t::DEFAULT_RANGE();
 
  257     zap[
'X'] = 
make_field(logx,       
"logarithmic x-axis (-XX log10 axis)");
 
  258     zap[
'Y'] = 
make_field(logy,       
"logarithmic y-axis");
 
  259     zap[
'Z'] = 
make_field(logz,       
"logarithmic y-axis; after projection");
 
  261     zap[
'>'] = 
make_field(xLabel,     
"x-axis label")                       = 
"";
 
  262     zap[
'^'] = 
make_field(yLabel,     
"y-axis label")                       = 
"";
 
  263     zap[
'C'] = 
make_field(drawLine,   
"draw line (-C black-and-white -CC colour)");
 
  264     zap[
'F'] = 
make_field(fillArea,   
"fill area")                          = 0;
 
  265     zap[
'l'] = 
make_field(lineWidth,  
"line width")                         = 2;
 
  266     zap[
'S'] = 
make_field(markerSize, 
"marker size")                        = 1.0;
 
  267     zap[
'O'] = 
make_field(option,     
"plotting option")                    = 
"";
 
  269     zap[
'B'] = 
make_field(batch,      
"batch processing");
 
  270     zap[
'T'] = 
make_field(title,      
"graphics title ("  
  271                           << 
"\"" << JName_t  << 
"\" -> ROOT name; " 
  272                           << 
"\"" << JTitle_t << 
"\" -> ROOT title)")      = 
"KM3NeT preliminary";
 
  274     zap[
'g'] = 
make_field(group,      
"group colour codes of objects")      = 1;
 
  275     zap[
't'] = 
make_field(xTimeFormat, 
"set time format for x-axis, e.g. \%d\\/\%m\\/\\%y%F1970-01-01 00:00:00") = 
"";
 
  280   catch(
const exception &error) {
 
  281     FATAL(error.what() << endl);
 
  285   gROOT->SetBatch(batch);
 
  287   TApplication* tp = 
new TApplication(
"user", NULL, NULL);
 
  288   TCanvas*      cv = 
new TCanvas(
"c1", 
"c1", canvas.
x, canvas.
y);
 
  290   unique_ptr<TStyle> gStyle(
new JStyle(
"gplot", cv->GetWw(), cv->GetWh(), parameters));
 
  293     gStyle->SetTitleOffset(gStyle->GetTitleOffset() * 1.3, 
"Y");
 
  296   gROOT->SetStyle(
"gplot");
 
  299   cv->SetFillStyle(4000);
 
  300   cv->SetFillColor(kWhite);
 
  309   Double_t 
xmin = numeric_limits<double>::max();
 
  310   Double_t 
xmax = numeric_limits<double>::lowest();
 
  312   Double_t ymin = numeric_limits<double>::max();
 
  313   Double_t ymax = numeric_limits<double>::lowest();
 
  322   logy = (logy || logz);
 
  337     DEBUG(
"Input: " << *input << endl);
 
  342       ERROR(
"File: " << input->getFullFilename() << 
" not opened." << endl);
 
  346     const TRegexp regexp(input->getObjectName());
 
  348     TIter iter(dir->GetListOfKeys());
 
  350     for (TKey* key; (key = (TKey*) iter.Next()) != NULL; ) {
 
  352       const TString tag(key->GetName());
 
  354       DEBUG(
"Key: " << tag << 
" match = " << tag.Contains(regexp) << endl);
 
  358       if (tag.Contains(regexp) && 
isTObject(key)) {
 
  360         if        (title == JName_t) {
 
  361           title = key->GetName();
 
  362         } 
else if (title == JTitle_t) {
 
  363           title = key->GetTitle();
 
  378           line.SetLineColor(marker.GetMarkerColor());
 
  384           TProfile& h1 = 
dynamic_cast<TProfile&
>(*object);
 
  386           object = h1.ProjectionX();
 
  392           TH2& h2 = 
dynamic_cast<TH2&
>(*object);
 
  403                                       h2.GetYaxis()->GetNbins());
 
  414                                       h2.GetXaxis()->GetNbins());
 
  418             ERROR(
"For 2D histograms, use option option -P for projections or use JPlot2D" << endl);
 
  427           TH3& h3 = 
dynamic_cast<TH3&
>(*object);
 
  443             ERROR(
"For 3D histograms, use option option -P for projections or use JPlot2D -P <projection>" << endl);
 
  453           if (
dynamic_cast<TMarker*
>(
object.get()) == NULL) {
 
  454             dynamic_cast<TAttMarker&
>(*object) = marker; 
 
  460           if (
dynamic_cast<TLine*
>(
object.get())   == NULL) {
 
  461             dynamic_cast<TAttLine&
>  (*object) = line;
 
  470             TAttFill& fill = 
dynamic_cast<TAttFill&
>(*object);
 
  473               fill.SetFillColor(marker.GetMarkerColor());
 
  474               fill.SetFillStyle(abs(fillArea));
 
  486             TH1& h1 = 
dynamic_cast<TH1&
>(*object);
 
  488             for (
int i = 1; i <= h1.GetNbinsX(); ++i) {
 
  489               h1.SetBinError(i, 0.0);
 
  496             TGraphErrors& 
g1 = 
dynamic_cast<TGraphErrors&
>(*object);
 
  498             for (Int_t i = 0; i != 
g1.GetN(); ++i) {
 
  510           TH1& h1 = 
dynamic_cast<TH1&
>(*object);
 
  512           h1.SetStats(stats != -1);
 
  514           xmin = min(
xmin, h1.GetXaxis()->GetXmin());
 
  515           xmax = max(
xmax, h1.GetXaxis()->GetXmax());       
 
  516           ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
 
  517           ymax = max(ymax, h1.GetMaximum());
 
  519           if (!logy && h1.GetListOfFunctions() != NULL) {
 
  520             for (unique_ptr<TIterator> 
iterator(h1.GetListOfFunctions()->MakeIterator()); TF1* 
f1 = (TF1*) 
iterator->Next(); ) {
 
  521               ymin = min(ymin, 
f1->GetMinimum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
 
  522               ymax = max(ymax, 
f1->GetMaximum(h1.GetXaxis()->GetXmin(), h1.GetXaxis()->GetXmax()));
 
  530           TGraph& 
g1 = 
dynamic_cast<TGraph&
>(*object);
 
  532           for (Int_t i = 0; i != 
g1.GetN(); ++i) {
 
  537             if (!logy || 
g1.GetY()[i] > 0.0) {
 
  538               ymin = min(ymin, 
g1.GetY()[i]);
 
  539               ymax = max(ymax, 
g1.GetY()[i]);
 
  547           TGraphErrors& 
g1 = 
dynamic_cast<TGraphErrors&
>(*object);
 
  549           for (Int_t i = 0; i != 
g1.GetN(); ++i) {
 
  550             if (!logy || 
g1.GetY()[i] - 
g1.GetEY()[i] > 0.0) { ymin = min(ymin, 
g1.GetY()[i] - 
g1.GetEY()[i]); }
 
  551             if (!logy || 
g1.GetY()[i] + 
g1.GetEY()[i] > 0.0) { ymax = max(ymax, 
g1.GetY()[i] + 
g1.GetEY()[i]); }
 
  558           TMultiGraph& m1 = 
dynamic_cast<TMultiGraph&
>(*object);
 
  560           for (TIter i1(m1.GetListOfGraphs()); TGraph* 
g1 = 
dynamic_cast<TGraph*
>(i1()); ) {
 
  562             for (Int_t i = 0; i != 
g1->GetN(); ++i) {
 
  567               if (!logy || 
g1->GetY()[i] > 0.0) {
 
  568                 ymin = min(ymin, 
g1->GetY()[i]);
 
  569                 ymax = max(ymax, 
g1->GetY()[i]);
 
  578           TF2& f2 = 
dynamic_cast<TF2&
>(*object);
 
  581           TString formula = f2.GetExpFormula();
 
  589           f2.GetRange(__xmin, __ymin, __xmax, __ymax);
 
  593             formula.ReplaceAll(
"y", _z_);
 
  597             f1->SetRange(__xmin, __xmax);
 
  601             formula.ReplaceAll(
"x", _z_);
 
  602             formula.ReplaceAll(
"y", 
"x");
 
  606             f1->SetRange(__ymin, __ymax);
 
  610             ERROR(
"For 2D functions, use option option -P for projections or use JPlot2D" << endl);
 
  615           DEBUG(
"TF1: " << 
f1->GetExpFormula() << endl);
 
  617           f1->SetParameters(f2.GetParameters());
 
  625           TF1& 
f1 = 
dynamic_cast<TF1&
>(*object);
 
  630           f1.GetRange(__xmin, __xmax);
 
  634           ymin = min(ymin, 
f1.GetMinimum());
 
  635           ymax = max(ymax, 
f1.GetMaximum());
 
  641           THStack& hs = 
dynamic_cast<THStack&
>(*object);
 
  643           NOTICE(
"THStack" << endl);
 
  645           unique_ptr<TIterator> 
iterator(hs.GetHists()->MakeIterator());
 
  649             TH1& h1 = 
dynamic_cast<TH1&
>(*i);
 
  651             NOTICE(
"TH1[" << index << 
"] " << h1.GetName() << endl);
 
  653             xmin = min(
xmin, h1.GetXaxis()->GetXmin());
 
  654             xmax = max(
xmax, h1.GetXaxis()->GetXmax());
 
  656             ymin = min(ymin, logy ? h1.GetMinimum(0.0) : h1.GetMinimum());
 
  657             ymax = max(ymax, h1.GetMaximum());
 
  660             h1.SetLineColor(kBlack);
 
  669           TLine& h1 = 
dynamic_cast<TLine&
>(*object);
 
  673           ymin = min(ymin, h1.GetY1());
 
  674           ymax = max(ymax, h1.GetY2());
 
  680         for (TString buffer[] = { 
object.getLabel(), input->getFilename().c_str(), 
"" }, *i = buffer; *i != 
""; ++i) {
 
  682           *i = (*i)(TRegexp(
"\\[.*\\]"));
 
  684           if (i->Length() > 2) {
 
  685             object.setLabel((*i)(1, i->Length() - 2));
 
  689         DEBUG(
"Add object: " << tag << 
" with label <" << 
object.
getLabel() << 
">" << endl);
 
  691         if (master == NULL) {
 
  692           master = 
dynamic_cast<TH1*
>(
object.get());
 
  695         listOfObjects.push_back(
object);
 
  700   if (listOfObjects.empty()) {
 
  701     ERROR(
"Nothing to draw." << endl);
 
  710     TH1*         h1 = 
dynamic_cast<TH1*
>        (i->get());
 
  711     TGraph*      
g1 = 
dynamic_cast<TGraph*
>     (i->get());
 
  712     TMultiGraph* m1 = 
dynamic_cast<TMultiGraph*
>(i->get());
 
  713     TAttLine*    
ls = 
dynamic_cast<TAttLine*
>   (i->get());
 
  721       iterator.reset(h1->GetListOfFunctions()->MakeIterator());
 
  723     } 
else if (
g1 != NULL) {
 
  725       iterator.reset(
g1->GetListOfFunctions()->MakeIterator());
 
  727     } 
else if (m1 != NULL) {
 
  729       for (TIter i1(m1->GetListOfGraphs()); TGraph* gi = 
dynamic_cast<TGraph*
>(i1()); ) {
 
  730         for (TIter i2(gi->GetListOfFunctions()); TF1* fi = 
dynamic_cast<TF1*
>(i2()); ) {
 
  740       Double_t x1[] = { numeric_limits<Double_t>::max(),    numeric_limits<Double_t>::max() };
 
  741       Double_t x2[] = { numeric_limits<Double_t>::lowest(), numeric_limits<Double_t>::lowest() };
 
  743       for (
int nf = 0, 
ns = 0, nc = 1; TF1* 
f1 = (TF1*) 
iterator->Next(); ++nf) {
 
  745         f1->GetRange(x1[1], x2[1]);
 
  750         if (listOfObjects.size() == 1) {
 
  752           if      (x1[0] == x1[1] && 
 
  765           f1->SetLineColor(
ls->GetLineColor());
 
  796   } 
else if (ymax > ymin) {
 
  802   if (!listOfObjects.empty()) {
 
  804     if (master == NULL) {
 
  806       master = 
new TH1D(MASTER.c_str(), NULL, 1000, 
xmin, 
xmax);
 
  808       master->SetStats(kFALSE);
 
  810       for (Int_t i = 1; i <= master->GetXaxis()->GetNbins(); ++i) {
 
  811         master->SetBinContent(i, ymin);
 
  816   if (master == NULL) {
 
  818     TText* p = 
new TText(0.5, 0.5, 
MAKE_CSTRING(
"No data"));
 
  826     if (logx) { gPad->SetLogx(); }
 
  827     if (logy) { gPad->SetLogy(); }
 
  829     master->GetXaxis()->SetRangeUser(
xmin, 
xmax);
 
  830     master->SetTitle(title.c_str());
 
  836     master->SetMinimum(ymin);
 
  837     master->SetMaximum(ymax);
 
  839     if (xLabel != 
"") { master->GetXaxis()->SetTitle(xLabel.c_str()); master->GetXaxis()->CenterTitle(
true); }
 
  840     if (yLabel != 
"") { master->GetYaxis()->SetTitle(yLabel.c_str()); master->GetYaxis()->CenterTitle(
true); }
 
  842     master->GetXaxis()->SetMoreLogLabels((logx == 1 && log10(
xmax/
xmin) < 2) ||
 
  845     master->GetYaxis()->SetMoreLogLabels( logy      && log10(ymax/ymin) < 2);
 
  846     master->GetYaxis()->SetNoExponent   ( logy      && log10(ymax/ymin) < 2);
 
  849       master->SetNdivisions(i->second, i->first.c_str());
 
  852     if (xTimeFormat != 
"") {
 
  854       master->GetXaxis()->SetTimeDisplay(1);
 
  856       if        (xTimeFormat == 
"utc") {
 
  857         master->GetXaxis()->SetTimeFormat(
"#splitline{}{#splitline{%d-%m-%y}{ %H:%M}}");
 
  858         master->GetXaxis()->SetTimeOffset(0.0, 
"gmt");
 
  859       } 
else if (xTimeFormat == 
"UTC") {
 
  860         master->GetXaxis()->SetTimeFormat(
"%d-%m-%y");
 
  861         master->GetXaxis()->SetTimeOffset(0.0, 
"gmt");
 
  863         master->GetXaxis()->SetTimeFormat(xTimeFormat.c_str());
 
  867     master->Draw(option.c_str());
 
  872       if (
dynamic_cast<TH1*
>(i->get()) != master) {
 
  873         if      (setLogX<TH1>         (i->get())) {}
 
  874         else if (setLogX<TF1>         (i->get())) {}
 
  875         else if (setLogX<TGraphErrors>(i->get())) {}
 
  876         else if (setLogX<TGraph>      (i->get())) {}
 
  877         else if (setLogX<TMultiGraph> (i->get())) {}
 
  878         else if (setLogX<TLine>       (i->get())) {}
 
  879         else if (setLogX<TEllipse>    (i->get())) {}
 
  884   if (grid.count(
'x') || grid.count(
'X')) { gPad->SetGridx(); }
 
  885   if (grid.count(
'y') || grid.count(
'Y')) { gPad->SetGridy(); }
 
  888     gStyle->SetOptStat(stats);
 
  890     gStyle->SetOptFit(kFALSE);
 
  895     DEBUG(
"Draw " << (*i)->GetName() << 
' ' << (*i)->GetTitle() << endl);
 
  897     string buffer(option);
 
  905     TF1*         
f1 = 
dynamic_cast<TF1*
>        (i->get());
 
  906     TGraph*      
g1 = 
dynamic_cast<TGraph*
>     (i->get());
 
  907     TMultiGraph* q1 = 
dynamic_cast<TMultiGraph*
>(i->get());
 
  914       if (
g1->GetN() > 1 && drawLine)
 
  916       else if (fillArea == 0 ||
 
  917                (
dynamic_cast<TGraphErrors*
>     (
g1) == NULL &&
 
  918                 dynamic_cast<TGraphAsymmErrors*
>(
g1) == NULL))
 
  924       for (TIter i1(q1->GetListOfGraphs()); TGraph* gi = 
dynamic_cast<TGraph*
>(i1()); ) {
 
  926         string zbuf = buffer;
 
  928         if (gi->GetN() > 1 && drawLine)
 
  933         gi->Draw(zbuf.c_str());
 
  938       (*i).Draw(buffer.c_str());
 
  944   if (legend.is_valid()) {
 
  947       group = listOfObjects.size();
 
  950     Ssiz_t height = listOfObjects.size()/group;
 
  954       width = max(width, getWidth(i->getLabel()));
 
  957     TLegend* lg = 
getLegend(width, height, legend.location, legend.factor);
 
  959     for (
size_t i = 0; i < listOfObjects.size(); i += group) {
 
  963       if (
dynamic_cast<TMarker*
>(
object.get()) == NULL  &&
 
  964           dynamic_cast<TLine*
>  (
object.get()) == NULL  &&
 
  965           dynamic_cast<TText*
>  (
object.get()) == NULL) {
 
  983   return (master != NULL ? 0 : 1);
 
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
 
General purpose messaging.
 
#define DEBUG(A)
Message macros.
 
Utility class to parse command line options.
 
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
 
int main(int argc, char **argv)
 
I/O formatting auxiliaries.
 
#define MAKE_CSTRING(A)
Make C-string.
 
Utility class to parse parameter values.
 
Double_t g1(const Double_t x)
Function.
 
Auxiliary class to define a range between two values.
 
Utility class to parse parameter values.
 
Auxiliary data structure for TObject with a user defined label.
 
Utility class to parse command line options.
 
Data structure for size of TCanvas.
 
int y
number of pixels in Y
 
int x
number of pixels in X
 
Wrapper class around ROOT TStyle.
 
const JPolynome f1(1.0, 2.0, 3.0)
Function.
 
std::string getLabel(const JLocation &location)
Get module label for monitoring and other applications.
 
void setLogarithmicX(TList *list)
Make x-axis of objects in list logarithmic (e.g. after using log10()).
 
void setRange(double &xmin, double &xmax, const bool logx)
Set axis range.
 
bool isTAttFill(const TObject *object)
Get drawing option of object.
 
TDirectory * getDirectory(const JRootObjectID &id)
Get TDirectory pointer.
 
bool isTObject(const TKey *key)
Check if given key corresponds to a TObject.
 
bool isTAttLine(const TObject *object)
Get drawing option of object.
 
static const char LABEL_TERMINATOR
label terminator
 
T & getInstance(const T &object)
Get static instance from temporary object.
 
bool equals(const JFirst_t &first, const JSecond_t &second, const double precision=std::numeric_limits< double >::min())
Check equality.
 
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
 
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option, const Double_t factor=1.0)
Get legend.
 
bool is_valid(const json &js)
Check validity of JSon data.
 
Type definition of range.
 
Template definition of auxiliary base class for comparison of data structures.
 
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
 
JProperties getProperties()
Get properties of this class.
 
Auxiliary data structure to list files in directory.