00001
00002
00003
00004
00005
00006 #include "analysis/Tuple.h"
00007 #include "analysis/Histogram.h"
00008 #include <fstream>
00009 #include <strstream>
00010 using namespace std;
00011
00012
00013 static const char* helptext=
00014 "\n----------------------------------------------"
00015 "\n makehist tuple_file histo_file item_name [min] [max] [step] [factor] [offset]"
00016 "\n"
00017 "\n tuple_file Name of standard tuple file, or \"help\" to give advice"
00018 "\n histo_file Name of histogram tuple file, or \"-\" to write to standard otput"
00019 "\n item_name Name of a column in the tuple"
00020 "\n min left edge of histogram [0]"
00021 "\n max right edge [min+1]"
00022 "\n step bin size [(max-min)/25]"
00023 "\n factor multiply the item [1.0]"
00024 "\n offset add to the item [0]"
00025 "\n"
00026 ;
00027
00028 int main(int argc, const char** argv)
00029 {
00030 int n=0;
00031 string tuple_file("-");
00032 string histo_file("-");
00033
00034 if( argc>++n ) tuple_file = argv[n];
00035 if (string(tuple_file).find("?") != string::npos
00036 || tuple_file=="help") {
00037 cerr << helptext << std::endl;
00038 return 0;
00039 }
00040
00041 if( argc>++n ) histo_file = argv[n];
00042 if (string(histo_file).find("?") != string::npos
00043 && histo_file!="-") {
00044 cerr << "command line problem" << std::endl;
00045 return 0;
00046 }
00047
00048
00049 string item_name("MC_Energy");
00050 if( argc>++n ) item_name = argv[n];
00051
00052 double xmin = (argc>++n)? atof(argv[n]) : 0;
00053 double xmax = (argc>++n)? atof(argv[n]) : xmax = xmin+1.0;
00054 double xstep = (argc>++n)? atof(argv[n]) : (xmax-xmin)/25;
00055 double factor = (argc>++n)? atof(argv[n]) : 1.0;
00056 double offset = (argc>++n)? atof(argv[n]) : 0;
00057 if( argc>++n ) offset= atof(argv[n]);
00058
00059
00060
00061 istream* infile;
00062 if( tuple_file== "-") {
00063 infile = &std::cin;
00064 } else {
00065 ifstream* realfile = new ifstream(tuple_file.c_str());
00066 if( !realfile->is_open()) {
00067 cerr << "Cannot open input file \"" << tuple_file << "\"" << endl;
00068 exit(1);
00069 }
00070
00071 infile = realfile;
00072 }
00073
00074 Tuple tup(*infile);
00075
00076 Tuple::const_iterator item= tup.find(item_name);
00077 if( item==tup.end() ) {
00078 cerr << "Tuple item '" << item_name.c_str() << "' not found: header follows" << endl;
00079 tup.writeHeader(cerr);
00080 return(2);
00081 }
00082
00083 const TupleItem& tuple_item= **item;
00084
00085
00086 strstream hist_title;
00087 if (factor != 1.0) hist_title << factor << '*';
00088 hist_title << tuple_item.name();
00089 if (offset > 0.) hist_title << '+' << offset;
00090 else if (offset < 0.) hist_title << offset;
00091 hist_title << '\0';
00092
00093
00094 Histogram hist( hist_title.str(), xmin, xmax, xstep) ;
00095
00096 do {
00097 (*infile) >> tup;
00098 if( ! infile->good() ) break;
00099 if( tuple_item() != 0) {
00100 hist.fill( factor*tuple_item()+offset ) ;
00101 }
00102
00103 } while( 1 ) ;
00104
00105
00106
00107 ostream* outfile;
00108 if( histo_file== "-") {
00109 outfile = &std::cout;
00110 }
00111 else {
00112 outfile = new ofstream(histo_file.c_str());
00113 }
00114 *outfile << "Tuple title: \""<< tup.title() << "\"\n" ;
00115 hist.print(*outfile);
00116 return 0;
00117 }