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 << "-root (flag) : Don't display windows, but instead create root files" << endl;
00047 cerr << "-dir <dirname> (optional) : like -batch but write files to a named directory" << endl;
00048 cerr << "-occ (flag) : Show occupancy per event data for RawScanResult" << 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]) == "-root") {
00135 DisplayManager::setBatchMode();
00136 DisplayManager::setRootMode();
00137 } else if (string(argv[i]) == "-occ" || string(argv[i])=="-ope") {
00138 std::cout << "OPE data requested" << std::endl;
00139 info.occupancyHistograms=true;
00140 } else {
00141 isNames.push_back(argv[i]);
00142 }
00143 }
00144
00145 if (info.displayChannels.size() == 0 && info.displayChips.size() == 0){
00146 DisplayInfo info2 = getDefaultInfo();
00147 info2.occupancyHistograms = info.occupancyHistograms;
00148 info=info2;
00149 }
00150 return info;
00151 }
00152
00153 shared_ptr<Serializable> readObject(string name) {
00154 FILE* f = fopen(name.c_str(), "rb");
00155 if (f) {
00156 fclose(f);
00157 if (name.substr(name.size()-3) == ".gz"){
00158 cout << "Reading from archive file" << std::endl;
00159 return IOManagerArchiveFile::instance().read(name);
00160 } else {
00161 cout << "Reading from scratch space" << std::endl;
00162 return IOManagerFile::instance().read(name);
00163 }
00164 } else {
00165 cout << "Reading from information service" << std::endl;
00166 return IOManagerIS::instance().read(name);
00167 }
00168 }
00169
00170
00171
00172
00173 int main(int argc, char** argv) {
00174 IPCCore::init(argc,argv);
00175 setExceptionHandlers(argv[0]);
00176
00177 DisplayInfo info = handleArgs(argc, argv);
00178
00179
00180 TApplication* myapp=0;
00181 if (!DisplayManager::batchMode()) myapp=new TApplication("myapp", 0, 0);
00182
00183 int errorCount = 0;
00184
00185 vector<shared_ptr<DisplayData> > displayObjs;
00186 for (unsigned int i=0; i<isNames.size(); ++i) {
00187 try {
00188 shared_ptr<Serializable> ob = readObject(isNames[i]);
00189 if (!ob)
00190 throw IoException("No object `" + isNames[i] + "' in IS, scratch space or data archive", __FILE__, __LINE__);
00191
00192 if (DisplayManager::batchMode()){
00193 std::ostringstream oss;
00194 oss << DisplayManager::getOutputDir();
00195 displayObjs.push_back(DisplayManager::display(ob, info, oss));
00196 if (oss.str()!=""){
00197 std::string filename=DisplayManager::getOutputDir() + ob->getClassName();
00198 filename+=".";
00199 filename+=ob->getUniqueID();
00200 std::ofstream f(filename.c_str());
00201 f << oss.str();
00202 std::cout << "TEXT " << filename << std::endl;
00203 }
00204 }else{
00205 displayObjs.push_back(DisplayManager::display(ob, info, std::cout));
00206 }
00207 } catch (Sct::Exception& e) {
00208 e.sendToMrs(MRS_DIAGNOSTIC);
00209 ++errorCount;
00210 }
00211 }
00212
00213 if (myapp) {
00214 myapp->Run();
00215 } else {
00216 DisplayManager::OutputCanvases();
00217 }
00218
00219 return errorCount;
00220 }