Repository navigation
Expand file tree
/
Copy pathTigSort.cxx
More file actions
486 lines (389 loc) · 11.1 KB
/
Copy pathTigSort.cxx
File metadata and controls
486 lines (389 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// TigSort
// MIDAS data sorting for SHARC / TIGRESS
// author: Ulrike Hager
// based on ROOT analyzer by K.Olchanski
// using some komodo routines by L. Erikson
#include <sys/time.h>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <signal.h>
#include <TMidasOnline.h>
#include <TMidasEvent.h>
#include <TMidasFile.h>
#include <XmlOdb.h>
#ifdef OLD_SERVER
#include <midasServer.h>
#endif
#ifdef HAVE_LIBNETDIRECTORY
#include <libNetDirectory/netDirectoryServer.h>
#endif
#include <TSystem.h>
#include <TROOT.h>
#include <TApplication.h>
#include <TTimer.h>
#include <TFile.h>
#include <TDirectory.h>
#include <TFolder.h>
//#include "TigGlobals.h"
#include "TigTimer.h"
#include "TigManager.h"
// data sizes
// Global Variables
int gRunNumber = 0;
bool gIsRunning = false;
bool gIsPedestalsRun = false;
bool gIsOffline = false;
int gEventCutoff = 0;
std::string configFile = "-";
TDirectory* gOnlineHistDir = NULL;
TFile* gOutputFile = NULL;
VirtualOdb* gOdb = NULL;
TigManager gCatalystMgr;
#define M_FILE_EXIT 0
void startRun(int transition,int run,int time)
{
std::cout << "startRun" << std::endl;
gIsRunning = true;
gRunNumber = run;
//gIsPedestalsRun = gOdb->odbReadBool("/experiment/edit on start/Pedestals run");
printf("Begin run: %d\n", gRunNumber);
// std::cout << "run number from odb: " << gOdb->odbReadInt("/Runinfo/Run number") << std::endl;
if(gOutputFile!=NULL)
{
gOutputFile->Write();
gOutputFile->Close();
gOutputFile=NULL;
}
char filename[1024];
// sprintf(filename, "asm%05d.root", run);
sprintf(filename, "asm%05d.root", run);
gOutputFile = new TFile(filename,"RECREATE");
#ifdef HAVE_LIBNETDIRECTORY
NetDirectoryExport(gOutputFile, "outputFile");
#endif
gOutputFile->cd();
// std::cout << "starting to parse input" << std::endl;
TigManager::Instance().ParseInputFile(configFile);
// std::cout << "parsed input" << std::endl;
}
void endRun(int transition,int run,int time)
{
// std::cout << "endRun" << std::endl;
gIsRunning = false;
gRunNumber = run;
TigManager::Instance().FlushTreeBuffers();
#ifdef OLD_SERVER
if (gManaHistosFolder)
gManaHistosFolder->Clear();
#endif
if (gOutputFile)
{
gOutputFile->Write();
gOutputFile->Close(); //close the histogram file
gOutputFile = NULL;
if (!gIsOffline) // write runlog
{
time_t startT = gOdb->odbReadUint32("/Runinfo/Start time binary");
time_t stopT = gOdb->odbReadUint32("/Runinfo/Stop time binary");
std::string time1(ctime(&startT));
std::string time2(ctime(&stopT));
int duration = difftime(stopT,startT);
std::string comment = gOdb->odbReadString("Experiment/Run Parameters/Comment");
std::cout << time1.substr(0, 10).c_str() << time1.substr(19, 5).c_str()
<< std::setw(8) << gRunNumber
<< std::setw(11) << time1.substr(11, 8).c_str()
<< std::setw(11) << time2.substr(11, 8).c_str()
<< std::setw(7) << duration << " s "
<< comment << std::endl;
std::string runlogFile = gOdb->odbReadString("Logger/Data dir");
runlogFile += "/runlog.txt";
std::cout << "runlog file: " << runlogFile << std::endl;
ofstream runlog;
runlog.open(runlogFile.c_str(), ofstream::out | ofstream::app);
runlog << time1.substr(0, 10).c_str() << time1.substr(19, 5).c_str()
<< std::setw(8) << gRunNumber
<< std::setw(11) << time1.substr(11, 8).c_str()
<< std::setw(11) << time2.substr(11, 8).c_str()
<< std::setw(7) << duration << " s "
<< comment << std::endl;
runlog.close();
}
}
printf("End of run %d\n",run);
}
void pauseRun(int transition,int run,int time)
{
}
void resumeRun(int transition,int run,int time)
{
}
void HandleMidasEvent(TMidasEvent& pEvent)
{
int eventId = pEvent.GetEventId();
if ( eventId == 1) //Tig64 data
{
TigManager::Instance().ProcessTig64(&pEvent,"WFDN");
}
else if ( eventId == 3) //scaler data
{
TigManager::Instance().ProcessScaler(&pEvent);
}
else
{
// unknown event type
// std::cout << "unknown event" << std::endl;
// pEvent.Print();
}
}
void eventHandler(const void*pheader,const void*pdata,int size)
{
TMidasEvent event;
memcpy(event.GetEventHeader(), pheader, sizeof(TMidas_EVENT_HEADER));
event.SetData(size, (char*)pdata);
event.SetBankList();
HandleMidasEvent(event);
}
int ProcessMidasFile(TApplication*app,const char*fname)
{
TMidasFile f;
bool tryOpen = f.Open(fname);
if (!tryOpen)
{
printf("Cannot open input file \"%s\"\n",fname);
return -1;
}
// std::string name = fname;
// name = name.substr(name.find_last_of("/\\")+1);
// int firstNum = name.find_first_of("0123456789");
// std::string number = name.substr(firstNum, name.find(".")-firstNum);
// std::istringstream(number) >> firstNum;
// startRun(0,firstNum,0);
int i=0;
while (1)
{
TMidasEvent event;
if (!f.Read(&event))
break;
int eventId = event.GetEventId();
// printf("Have an event of type %d\n",eventId);
if ((eventId & 0xFFFF) == 0x8000)
{
// begin run
// event.Print();
//char buf[256];
//memset(buf,0,sizeof(buf));
//memcpy(buf,event.GetData(),255);
//printf("buf is [%s]\n",buf);
//
// Load ODB contents from the ODB XML file
//
if (gOdb)
delete gOdb;
gOdb = new XmlOdb(event.GetData(),event.GetDataSize());
startRun(0,event.GetSerialNumber(),0);
std::cout << "Running...";
std::cout.flush();
}
else if ((eventId & 0xFFFF) == 0x8001)
// if ((eventId & 0xFFFF) == 0x8001)
{
// end run
// event.Print();
}
else
{
event.SetBankList();
//event.Print();
HandleMidasEvent(event);
}
if((i%5000)==0)
{
//resetClock2time();
// printf("Processing event %d\n",i);
std::cout << "." ;
std::cout.flush();
}
i++;
if ((gEventCutoff!=0)&&(i>=gEventCutoff))
{
printf("Reached event %d, exiting loop.\n",i);
break;
}
}
f.Close();
endRun(0,gRunNumber,0);
// start the ROOT GUI event loop
// app->Run(kTRUE);
if (gOdb)
delete gOdb;
return 0;
}
#ifdef HAVE_MIDAS
void MidasPollHandler()
{
if (!(TMidasOnline::instance()->poll(0)))
gSystem->ExitLoop();
}
int ProcessMidasOnline(TApplication*app, const char* hostname, const char* exptname)
{
TMidasOnline *midas = TMidasOnline::instance();
int err = midas->connect(hostname, exptname, "komodo");
if (err != 0)
{
fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
return -1;
}
gOdb = midas;
midas->setTransitionHandlers(startRun,endRun,pauseRun,resumeRun);
midas->registerTransitions();
/* reqister event requests */
midas->setEventHandler(eventHandler);
midas->eventRequest("SYSTEM",-1,-1,(1<<1));
/* fill present run parameters */
gRunNumber = gOdb->odbReadInt("/runinfo/Run number");
if ((gOdb->odbReadInt("/runinfo/State") == 3))
startRun(0,gRunNumber,0);
printf("Startup: run %d, is running: %d\n",gRunNumber,gIsRunning);
TigTimer tm(100,MidasPollHandler);
/*---- start main loop ----*/
//loop_online();
app->Run(kTRUE);
/* disconnect from experiment */
midas->disconnect();
return 0;
}
#endif
static bool gEnableShowMem = false;
int ShowMem(const char* label)
{
if (!gEnableShowMem)
return 0;
FILE* fp = fopen("/proc/self/statm","r");
if (!fp)
return 0;
int mem = 0;
fscanf(fp,"%d",&mem);
fclose(fp);
if (label)
printf("memory at %s is %d\n", label, mem);
return mem;
}
void help()
{
printf("\nUsage:\n");
printf("\t-h: print this help message\n");
printf("\t-c: config file (mapping)\n");
printf("\t-P: Start the TNetDirectory server on specified tcp port (for use with roody -Plocalhost:9091)\n");
printf("\t-p: Start the old midas histogram server on specified tcp port (for use with roody -Hlocalhost:9090)\n");
printf("\t-e: Number of events to read from input data files\n");
printf("\n");
exit(1);
}
// Main function call
int main(int argc, char *argv[])
{
setbuf(stdout,NULL);
setbuf(stderr,NULL);
signal(SIGILL, SIG_DFL);
signal(SIGBUS, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
std::vector<std::string> args;
for (int i=0; i<argc; i++)
{
if (strcmp(argv[i],"-h")==0)
help(); // does not return
args.push_back(argv[i]);
}
TApplication *app = new TApplication("TigSort", &argc, argv);
if(gROOT->IsBatch()) {
printf("Cannot run in batch mode\n");
return 1;
}
bool forceEnableGraphics = false;
bool testMode = false;
int oldTcpPort = 0;
int tcpPort = 0;
const char* hostname = NULL;
const char* exptname = NULL;
for (unsigned int i=1; i<args.size(); i++) // loop over the commandline options
{
const char* arg = args[i].c_str();
//printf("argv[%d] is %s\n",i,arg);
if (strncmp(arg,"-e",2)==0) // Event cutoff flag (only applicable in offline mode)
gEventCutoff = atoi(arg+2);
else if (strncmp(arg,"-m",2)==0) // Enable memory debugging
gEnableShowMem = true;
else if (strncmp(arg,"-p",2)==0) // Set the histogram server port
oldTcpPort = atoi(arg+2);
else if (strncmp(arg,"-P",2)==0) // Set the histogram server port
tcpPort = atoi(arg+2);
else if (strcmp(arg,"-T")==0)
testMode = true;
else if (strcmp(arg,"-g")==0)
forceEnableGraphics = true;
else if (strncmp(arg,"-H",2)==0)
hostname = strdup(arg+2);
else if (strncmp(arg,"-E",2)==0)
exptname = strdup(arg+2);
else if (strncmp(arg,"-c",2)==0)
configFile = strdup(arg+2);
else if (strcmp(arg,"-h")==0)
help(); // does not return
else if (arg[0] == '-')
help(); // does not return
}
// TigWindow mainWindow(gClient->GetRoot(), 200, 300);
gROOT->cd();
gOnlineHistDir = new TDirectory("histos", "TigSort online plots");
// app->Run(kTRUE);
// TigManager::Instance().ParseInputFile();
#ifdef OLD_SERVER
if (oldTcpPort)
// VerboseMidasServer(true);
StartMidasServer(oldTcpPort);
#else
if (oldTcpPort)
fprintf(stderr,"ERROR: No support for the old midas server!\n");
#endif
#ifdef HAVE_LIBNETDIRECTORY
if (tcpPort)
//VerboseNetDirectoryServer(true);
StartNetDirectoryServer(tcpPort, gOnlineHistDir);
#else
if (tcpPort)
fprintf(stderr,"ERROR: No support for the TNetDirectory server!\n");
#endif
// TBrowser *browser = new TBrowser();
gIsOffline = false;
for (unsigned int i=1; i<args.size(); i++)
{
const char* arg = args[i].c_str();
if (arg[0] != '-')
{
gIsOffline = true;
//gEnableGraphics = false;
//gEnableGraphics |= forceEnableGraphics;
ProcessMidasFile(app,arg);
}
}
if (testMode)
{
gOnlineHistDir->cd();
app->Run(kTRUE);
return 0;
}
// if we processed some data files,
// do not go into online mode.
if (gIsOffline)
return 0;
gIsOffline = false;
//gEnableGraphics = true;
#ifdef HAVE_MIDAS
ProcessMidasOnline(app, hostname, exptname);
#endif
return 0;
}
//end