00001 #include "DisplayManager.h"
00002 #include "DisplayInfo.h"
00003 #include "Displayer.h"
00004 #include "Sct/SctParameters.h"
00005 #include "Sct/Serializable.h"
00006 #include "Sct/IoExceptions.h"
00007 #include "Sct/IS/IOManagerIS.h"
00008 #include "Sct/File/IOManagerFile.h"
00009 #include "Sct/Archive/IOManagerArchiveFile.h"
00010 #include <iostream>
00011 #include <vector>
00012 #include <string>
00013 #include <TApplication.h>
00014
00015 using namespace std;
00016 using namespace Sct;
00017 using namespace Sct::IS;
00018 using namespace Sct::File;
00019 using namespace Sct::Archive;
00020 using namespace SctDataDisplay;
00021
00022
00023 vector<string> isNames;
00024
00028 DisplayInfo getDefaultInfo() {
00029 DisplayInfo info;
00030 for (unsigned int i=0; i<nChipModule; ++i) {
00031 info.displayChips.push_back(i);
00032 }
00033
00034 return info;
00035 }
00036
00037
00038 void printHelp() {
00039 cerr << "Usage: DataDisplayer [-batch] [-chip <start> <end>]* [-chan <start> <end>]* <ISNames> ..." << endl << endl;
00040 cerr << "DataDisplayer is a program that shows data that has been stored in IS" << endl;
00041 cerr << "If no -chip or -chan arguments are given, it will show information about all 12 chips and no channels by default"<< endl;
00042 cerr << "Overall module information is always shown." << endl;
00043 cerr << "Arguments: " << endl;
00044 cerr << "-batch (flag) : Don't display windows, but instead create postscript files" << endl;
00045 cerr << "-chip <start> <end> (optional) : A range of chips to display (may be specified more than once)" << endl;
00046 cerr << "-chan <start> <end> (optional) : A range of channels to display (may be specified more than once)" << endl;
00047 cerr << "<Names> (at least 1) : A list of names in IS or filenames of archived data" << endl;
00048 cerr << "--help : Show this help message" << endl;
00049 cerr << "All numbers are in base 0" << endl;
00050
00051 exit(-1);
00052 }
00053
00054 void parseChip(DisplayInfo& info, string start, string end) {
00055 istringstream iss1(start);
00056 unsigned int chipStart = nChipModule;
00057 iss1 >> chipStart;
00058 if (chipStart >= nChipModule) {
00059 cerr << "Invalid argument: " << start << " to -chip" << endl;
00060 printHelp();
00061 }
00062
00063 istringstream iss2(end);
00064 unsigned int chipEnd = nChipModule;
00065 iss2 >> chipEnd;
00066 if (chipEnd >= nChipModule) {
00067 cerr << "Invalid argument: " << end << " to -chip" << endl;
00068 printHelp();
00069 }
00070
00071 for (unsigned int i=chipStart; i<=chipEnd; ++i) {
00072 info.displayChips.push_back(i);
00073 }
00074 }
00075
00076 void parseChan(DisplayInfo& info, string start, string end) {
00077 istringstream iss1(start);
00078 unsigned int chanStart = nChannelModule;
00079 iss1 >> chanStart;
00080 if (chanStart >= nChannelModule) {
00081 cerr << "Invalid argument: " << start << " to -chan" << endl;
00082 printHelp();
00083 }
00084
00085 istringstream iss2(end);
00086 unsigned int chanEnd = nChannelModule;
00087 iss2 >> chanEnd;
00088 if (chanEnd >= nChannelModule) {
00089 cerr << "Invalid argument: " << end << " to -chan" << endl;
00090 printHelp();
00091 }
00092
00093 for (unsigned int i=chanStart; i<=chanEnd; ++i) {
00094 info.displayChannels.push_back(i);
00095 }
00096 }
00097
00098 DisplayInfo handleArgs(int argc, char** argv) {
00099 if (argc == 1) printHelp();
00100
00101 DisplayInfo info;
00102
00103 for (int i=1; i<argc; ++i) {
00104 if (string(argv[i]) == "--help") printHelp();
00105
00106 if (string(argv[i]) == "-chip") {
00107 if (i+2 >= argc) {
00108 cerr << "Invalid arguments for option -chip" << endl;
00109 printHelp();
00110 }
00111 parseChip(info, argv[i+1], argv[i+2]);
00112 i += 2;
00113 } else if (string(argv[i]) == "-chan") {
00114 if (i+2 >= argc) {
00115 cerr << "Invalid arguments for option -chip" << endl;
00116 printHelp();
00117 }
00118 parseChan(info, argv[i+1], argv[i+2]);
00119 i += 2;
00120 } else if (string(argv[i]) == "-batch") {
00121 DisplayManager::setBatchMode();
00122 } else {
00123 isNames.push_back(argv[i]);
00124 }
00125 }
00126
00127 if (info.displayChannels.size() == 0 && info.displayChips.size() == 0)
00128 info = getDefaultInfo();
00129
00130 return info;
00131 }
00132
00133 shared_ptr<Serializable> readObject(string name) {
00134 FILE* f = fopen(name.c_str(), "rb");
00135 if (f) {
00136 fclose(f);
00137 if (name.substr(name.size()-3) == ".gz"){
00138 cout << "Reading from archive file" << std::endl;
00139 return IOManagerArchiveFile::instance().read(name);
00140 } else {
00141 cout << "Reading from scratch space" << std::endl;
00142 return IOManagerFile::instance().read(name);
00143 }
00144 } else {
00145 cout << "Reading from information service" << std::endl;
00146 return IOManagerIS::instance().read(name);
00147 }
00148 }
00149
00150
00151
00152
00153 int main(int argc, char** argv) {
00154 setExceptionHandlers(argv[0]);
00155
00156 DisplayInfo info = handleArgs(argc, argv);
00157
00158
00159 TApplication* myapp=0;
00160 if (!DisplayManager::batchMode()) myapp=new TApplication("myapp", 0, 0);
00161
00162 int errorCount = 0;
00163
00164 vector<shared_ptr<DisplayData> > displayObjs;
00165 for (unsigned int i=0; i<isNames.size(); ++i) {
00166 try {
00167 shared_ptr<Serializable> ob = readObject(isNames[i]);
00168 if (!ob)
00169 throw IoException("No object `" + isNames[i] + "' in IS, scratch space or data archive", __FILE__, __LINE__);
00170
00171 if (DisplayManager::batchMode()){
00172 std::ostringstream oss;
00173 displayObjs.push_back(DisplayManager::display(ob, info, oss));
00174 if (oss.str()!=""){
00175 std::string filename=ob->getClassName();
00176 filename+=".";
00177 filename+=ob->getUniqueID();
00178 std::ofstream f(filename.c_str());
00179 f << oss.str();
00180 std::cout << "output to file " << filename << std::endl;
00181 }
00182 }else{
00183 displayObjs.push_back(DisplayManager::display(ob, info, std::cout));
00184 }
00185 } catch (Sct::Exception& e) {
00186 e.sendToMrs(MRS_DIAGNOSTIC);
00187 ++errorCount;
00188 }
00189 }
00190
00191 if (myapp) {
00192 myapp->Run();
00193 } else {
00194 DisplayManager::OutputCanvases();
00195 }
00196
00197 return errorCount;
00198 }