Hall-D Software  alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
root2email.cc
Go to the documentation of this file.
1 // $Id$
2 //
3 // Author: David Lawrence April 7, 2006
4 //
5 //
6 // root2email.cc
7 //
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <string>
12 #include <vector>
13 #include <sstream>
14 #include <stdlib.h>
15 using namespace std;
16 
17 #include <TCanvas.h>
18 #include <TFrame.h>
19 #include <TFile.h>
20 #include <TH2F.h>
21 #include <TROOT.h>
22 
23 const char *filename = "mctrk_ana.root";
24 
25 typedef struct{
26  string message;
27  string gif_file;
28 }plot_t;
29 
30 vector<string> email_address;
31 vector<string> histnames;
32 vector<plot_t> plots;
33 
34 void ParseCommandLineArguments(int narg, char *argv[]);
35 void Usage(void);
36 void AddPlot(TFile &f, string what);
37 
38 int colors[] = {kRed, kBlue, kGreen};
39 int Ncolors = 3;
40 
41 //-----------
42 // main
43 //-----------
44 int main(int narg, char *argv[])
45 {
46 
47  ParseCommandLineArguments(narg, argv);
48 
49  // open ROOT file for reading
50  TFile f(filename);
51  if(!f.IsOpen()){
52  cout<<"Can't open file \""<<filename<<"\" !"<<endl;
53  return 0;
54  }
55  cout<<"Opened ROOT file \""<<f.GetName()<<"\""<<endl;
56 
57  // Generate plots
58  for(unsigned int i=0; i<histnames.size(); i++){
59  AddPlot(f, histnames[i]);
60  }
61 
62  // Create command to send e-mail (excluding the e-mail address)
63  stringstream cmd;
64  cmd<<"(echo \"Auto-generated from "<<filename<<":\"; ";
65  cmd<<"echo \" \"; echo \" \"; ";
66  for(unsigned int i=0;i<plots.size();i++){
67  cmd<<"echo \""<<plots[i].message<<"\"; ";
68  cmd<<"uuencode "<<plots[i].gif_file<<" "<<plots[i].gif_file<<"; ";
69  }
70  if(plots.size()==0)cmd<<"echo \"No histograms specified!!\"; ";
71  cmd<<") | mail -s \"ROOT plots\" ";
72 
73  // Loop over e-mail addresses, sending the message to each one
74  for(unsigned int i=0; i<email_address.size(); i++){
75  stringstream my_cmd;
76  my_cmd<<cmd.str()<<email_address[i];
77  cout<<my_cmd.str()<<endl;
78  int res = system(my_cmd.str().c_str());
79  if(res!=0) cerr << "Error executing \""<<my_cmd.str()<<"\"" << endl;
80 
81  }
82 
83 
84  return 0;
85 }
86 
87 //------------------------------
88 // AddPlot
89 //------------------------------
90 void AddPlot(TFile &f, string histname)
91 {
92  plot_t plot;
93  stringstream mess;
94 
95  TH1F *hist=(TH1F*)gROOT->FindObject(histname.c_str());
96  //f.GetObject(histname.c_str(), hist);
97  if(!hist){
98  mess<<"Unable to read in histogram '"<<histname<<"' !";
99  plot.gif_file = string("");
100  }else{
101  // This would be soooo much better if one of the output
102  // binary formats(gif, jpg, png ..) worked right here.
103  // As it is, the output only looks correct for vector-oriented
104  // outputs (eps, pdf ,...) so we have to convert it externally.
105 
106  // Generate EPS file using ROOT
107  TCanvas c1("c1",histname.c_str(),200,10,700,500);
108  c1.SetGrid();
109  c1.SetFillColor(19);
110  hist->SetLineColor(colors[plots.size()%Ncolors]);
111  hist->Draw();
112  c1.Print("tmp.eps");
113 
114  // Convert EPS to PPM using ghostscript(gs)
115  int res = system("echo quit | gs -sDEVICE=ppm -r72x72 -g565x405 -sOutputFile=tmp.ppm -dNOPAUSE tmp.eps > /dev/null");
116  if(res!=0) cerr << "Error running \"gs\" command." << endl;
117 
118  // Generate unique name of GIF file
119  stringstream outfile;
120  outfile<<"plot"<<plots.size()<<".gif";
121  plot.gif_file = outfile.str();
122 
123  // Convert PPM to GIF using ppmtogif
124  stringstream cmd;
125  cmd<<"ppmtogif tmp.ppm > "<<outfile.str();
126  res = system(cmd.str().c_str());
127  if(res!=0) cerr << "Error running \""<<cmd.str()<<"\"" << endl;
128 
129  // Add histogram name to message
130  mess<<histname;
131 
132  // Delete temporary files
133  res = system("rm -f tmp.ppm tmp.eps");
134  if(res!=0) cerr << "Error running \"rm\" command." << endl;
135  }
136 
137  // Add plot to list. We do this even if we failed to generate a plot
138  // so the e-mail can have an error message
139  plot.message = mess.str();
140  plots.push_back(plot);
141 }
142 
143 //------------------------------
144 // ParseCommandLineArguments
145 //------------------------------
146 void ParseCommandLineArguments(int narg, char *argv[])
147 {
148  if(narg<2){
149  Usage();
150  }
151 
152  for(int i=1; i<narg; i++){
153  if(string(argv[i]) == "-cf"){
154  }else if(string(argv[i]) == "-r"){
155  if(i<narg-1){
156  email_address.push_back(argv[i+1]);
157  i++;
158  }
159  }else if(string(argv[i]) == "-H"){
160  if(i<narg-1){
161  histnames.push_back(argv[i+1]);
162  i++;
163  }
164  }else if(string(argv[i]) == "-if"){
165  if(i<narg-1){
166  filename = argv[i+1];
167  i++;
168  }
169  }else if(string(argv[i]) == "-h"){
170  Usage();
171  }else{
172 
173  }
174  }
175 
176  if(email_address.size() == 0){
177  cout<<endl;
178  cout<<"You MUST provide at least one e-mail address!!"<<endl<<endl;
179  Usage();
180  }
181 }
182 
183 //------------------------------
184 // Usage
185 //------------------------------
186 void Usage(void)
187 {
188  cout<<endl;
189  cout<<"Usage:"<<endl;
190  cout<<" root2email [options]"<<endl;
191  cout<<endl;
192  cout<<" -r email-address recipient e-mail address"<<endl;
193  cout<<" (can be given multiple times)"<<endl;
194  cout<<" -H histogram name of histogram. If histogram"<<endl;
195  cout<<" is in a TDirectory, include the"<<endl;
196  cout<<" path. (can be given multiple times)"<<endl;
197  cout<<" -if rootfile name of ROOT file to use"<<endl;
198  cout<<" -h print this help message."<<endl;
199 
200  cout<<endl;
201  exit(0);
202 }
203 
int Ncolors
Definition: root2email.cc:39
char string[256]
TString filename
int colors[]
vector< plot_t > plots
Definition: root2email.cc:32
string message
Definition: root2email.cc:26
Double_t c1[2][NMODULES]
Definition: tw_corr.C:68
TF1 * f
Definition: FitGains.C:21
vector< string > email_address
Definition: root2email.cc:30
void ParseCommandLineArguments(int &narg, char *argv[])
Definition: hd_dump.cc:124
TFile * outfile
Definition: tw_corr.C:46
void AddPlot(TFile &f, string what)
Definition: root2email.cc:90
string gif_file
Definition: root2email.cc:27
vector< string > histnames
Definition: root2email.cc:31
void Usage(JApplication &app)
Definition: hd_ana.cc:33
int main(int argc, char *argv[])
Definition: gendoc.cc:6
TH1F * hist[Idx+1]
Definition: readhist.C:10