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