ROOTANA
Loading...
Searching...
No Matches
mjsroot.cxx
Go to the documentation of this file.
1//
2// MIDAS jsroot server
3//
4// K.Olchanski
5//
6
7#undef NDEBUG // this program requires working assert()
8
9#include <stdio.h>
10#include <assert.h>
11#include <signal.h> // signal();
12
13#include "TSystem.h"
14#include "TFile.h"
15#include "TKey.h"
16#include "THttpServer.h"
17
18//////////////////////////////////////////////////////////
19//
20// main program
21//
22//////////////////////////////////////////////////////////
23
24static void help()
25{
26 printf("\nUsage: ./mjsroot.exe [-h] -R8081 [file1.root file2.root ...]\n");
27 printf("\n");
28 printf("-h: print this help message\n");
29 printf("\n");
30 printf("-Rnnnn: Start the ROOT THttpServer HTTP server on specified tcp port, use -R8081, access by firefox http://localhost:8081\n");
31 printf("\n");
32 printf("Example: use jsroot to look at a ROOT file: ./mjsroot.exe -R8082 root_output_files/output00371.root; firefox http://localhost:8082\n");
33 exit(1);
34}
35
36// duplicate c++20 std::string s.starts_with()
37
38static bool starts_with(const std::string& s, const char* prefix)
39{
40 return (s.substr(0, strlen(prefix)) == prefix);
41}
42
43// read all keys from ROOT file. why do we need this?!?
44
45void ReadFile(TDirectoryFile *f)
46{
47 for (TObject* keyobj: *f->GetListOfKeys()) {
48 TKey* key = (TKey*)keyobj;
49 //printf("key %p, name [%s] title [%s]\n", key, key->GetName(), key->GetTitle());
50 TObject* obj = f->Get(key->GetName());
51 TClass* t = obj->IsA();
52 //printf("obj %p, class [%s], key %p, name [%s], title [%s]\n", obj, t->GetName(), key, key->GetName(), key->GetTitle());
53 if (strcmp(t->GetName(), "TDirectoryFile") == 0) {
54 ReadFile((TDirectoryFile*)obj);
55 }
56 }
57}
58
59// Main function call
60
61int main(int argc, char* argv[])
62{
63 setbuf(stdout, NULL);
64 setbuf(stderr, NULL);
65
66 signal(SIGILL, SIG_DFL);
67 signal(SIGBUS, SIG_DFL);
68 signal(SIGSEGV, SIG_DFL);
69 signal(SIGPIPE, SIG_DFL);
70
71 std::vector<std::string> args;
72 for (int i=0; i<argc; i++) {
73 if (strcmp(argv[i],"-h")==0)
74 help(); // does not return
75 args.push_back(argv[i]);
76 }
77
78 int httpPort = 0;
79
80 std::vector<std::string> files;
81
82 for (unsigned int i=1; i<args.size(); i++) { // loop over the commandline options
83 std::string arg = args[i];
84 //printf("argv[%d] is %s\n",i,arg);
85
86 if (0) {
87 } else if (starts_with(arg, "-R")) { // Set the ROOT THttpServer HTTP port
88 httpPort = atoi(arg.c_str()+2);
89 } else if (arg == "-h") {
90 help(); // does not return
91 } else if (arg[0] == '-') {
92 help(); // does not return
93 } else {
94 files.push_back(args[i]);
95 }
96 }
97
98 if (httpPort == 0) {
99 help(); // does not return
100 }
101
102 char str[256];
103 snprintf(str, sizeof(str), "http:127.0.0.1:%d?cors", httpPort);
104
105 THttpServer* httpServer = new THttpServer(str);
106
107 if (!httpServer->IsAnyEngine()) {
108 fprintf(stderr,"ERROR: Cannot start web server on http port %d, see previous error message.\n", httpPort);
109 exit(1);
110 }
111
112 //s->SetTimer(100, kFALSE);
113
114 std::vector<TFile*> root_files;
115
116 for (unsigned i=0; i<files.size(); i++) {
117 printf("file[%d]: %s\n", i, files[i].c_str());
118
119 TFile* f = new TFile(files[i].c_str(), "READ");
120
121 if (!f->IsOpen()) {
122 fprintf(stderr, "Error: cannot open ROOT file \"%s\"\n", files[i].c_str());
123 exit(1);
124 }
125
126 //f->ReadAll(); // does not read all histograms into memory
127 //f->ReadKeys(); // reads all the subdirectories, but not histograms, so everything is in the wrong order
128
129 ReadFile(f);
130
131 root_files.push_back(f);
132 }
133
134
135 while (1) {
136 int nreq = httpServer->ProcessRequests();
137 if (nreq > 0) {
138 //printf("ProcessRequests() returned %d\n", nreq);
139 continue;
140 }
141
142 //gSystem->DispatchOneEvent(kTRUE);
143
144 bool intr = gSystem->ProcessEvents();
145 if (intr)
146 break;
147
148 usleep(1000);
149 }
150
151 printf("Interrupted, shutting down!\n");
152
153 // shutdown the web server
154
155 delete httpServer;
156 httpServer = NULL;
157
158 // close ROOT files
159
160 for (TFile* f: root_files) {
161 f->Close();
162 }
163
164 root_files.clear();
165
166 return 0;
167}
168
169/* emacs
170 * Local Variables:
171 * tab-width: 8
172 * c-basic-offset: 3
173 * indent-tabs-mode: nil
174 * End:
175 */
int main(int argc, char *argv[])
Definition mjsroot.cxx:61
void ReadFile(TDirectoryFile *f)
Definition mjsroot.cxx:45
static bool starts_with(const std::string &s, const char *prefix)
Definition mjsroot.cxx:38
static void help()
Definition mjsroot.cxx:24