ROOTANA
Loading...
Searching...
No Matches
manalyzer_v0.cxx
Go to the documentation of this file.
1//
2// MIDAS analyzer
3//
4// K.Olchanski
5//
6
7#include <stdio.h>
8#include <assert.h>
9
10#include "manalyzer.h"
11#include "midasio.h"
12
13//////////////////////////////////////////////////////////
14
15static bool gTrace = false;
16
17//////////////////////////////////////////////////////////
18//
19// Methods of TARunInfo
20//
21//////////////////////////////////////////////////////////
22
23TARunInfo::TARunInfo(int runno, const char* filename)
24{
25 if (gTrace)
26 printf("TARunInfo::ctor!\n");
27 fRunNo = runno;
28 if (filename)
29 fFileName = filename;
30 fOdb = NULL;
31#ifdef HAVE_ROOT
32 fRoot = new TARootHelper(this);
33#endif
34}
35
37{
38 if (gTrace)
39 printf("TARunInfo::dtor!\n");
40 fRunNo = 0;
41 fFileName = "(deleted)";
42 if (fOdb) {
43 delete fOdb;
44 fOdb = NULL;
45 }
46#ifdef HAVE_ROOT
47 if (fRoot) {
48 delete fRoot;
49 fRoot = NULL;
50 }
51#endif
52}
53
54//////////////////////////////////////////////////////////
55//
56// Methods of TAFlowEvent
57//
58//////////////////////////////////////////////////////////
59
61{
62 if (gTrace)
63 printf("TAFlowEvent::ctor: chain %p\n", flow);
64 fNext = flow;
65}
66
68{
69 if (gTrace)
70 printf("TAFlowEvent::dtor: this %p, next %p\n", this, fNext);
71 if (fNext)
72 delete fNext;
73 fNext = NULL;
74}
75
76//////////////////////////////////////////////////////////
77//
78// Methods of TARunInterace
79//
80//////////////////////////////////////////////////////////
81
83{
84 if (gTrace)
85 printf("TARunInterface::ctor, run %d\n", runinfo->fRunNo);
86}
87
89{
90 if (gTrace)
91 printf("TARunInterface::BeginRun, run %d\n", runinfo->fRunNo);
92}
93
95{
96 if (gTrace)
97 printf("TARunInterface::EndRun, run %d\n", runinfo->fRunNo);
98}
99
101{
102 if (gTrace)
103 printf("TARunInterface::PauseRun, run %d\n", runinfo->fRunNo);
104}
105
107{
108 if (gTrace)
109 printf("TARunInterface::ResumeRun, run %d\n", runinfo->fRunNo);
110}
111
113{
114 if (gTrace)
115 printf("TARunInterface::Analyze!\n");
116 return flow;
117}
118
120{
121 if (gTrace)
122 printf("TARunInterface::AnalyzeSpecialEvent!\n");
123}
124
125//////////////////////////////////////////////////////////
126//
127// Methods of TAModuleInterface
128//
129//////////////////////////////////////////////////////////
130
131void TAModuleInterface::Init(const std::vector<std::string> &args)
132{
133 if (gTrace)
134 printf("TAModuleInterface::Init!\n");
135}
136
138{
139 if (gTrace)
140 printf("TAModuleInterface::Finish!\n");
141}
142
143#ifdef HAVE_ROOT
144
145//////////////////////////////////////////////////////////
146//
147// Methods of TARootHelper
148//
149//////////////////////////////////////////////////////////
150
151#ifdef HAVE_XMLSERVER
152#include "xmlServer.h"
153#include "TROOT.h"
154#endif
155
156#ifdef XHAVE_LIBNETDIRECTORY
157#include "netDirectoryServer.h"
158#endif
159
160TApplication* TARootHelper::fgApp = NULL;
163THttpServer* TARootHelper::fgHttpServer = NULL;
164
165TARootHelper::TARootHelper(const TARunInfo* runinfo) // ctor
166{
167 if (gTrace)
168 printf("TARootHelper::ctor!\n");
169
170 char xfilename[1024];
171 sprintf(xfilename, "output%05d.root", runinfo->fRunNo);
172
173 fOutputFile = new TFile(xfilename, "RECREATE");
174
175 assert(fOutputFile->IsOpen()); // FIXME: survive failure to open ROOT file
176
177 fOutputFile->cd();
178
179#ifdef XHAVE_LIBNETDIRECTORY
180 NetDirectoryExport(fOutputFile, "ManalyzerOutputFile");
181#endif
182#ifdef HAVE_XMLSERVER
183 if (fgXmlServer)
184 fgXmlServer->Export(fOutputFile, "ManalyzerOutputFile");
185#endif
186}
187
189{
190 if (gTrace)
191 printf("TARootHelper::dtor!\n");
192
193 if (fOutputFile != NULL) {
194 fOutputFile->Write();
195 fOutputFile->Close();
196 fOutputFile = NULL;
197 }
198
199 if (fgDir)
200 fgDir->cd();
201}
202
203#endif
204
205#include <stdio.h>
206#include <stdlib.h>
207#include <string.h>
208#include <sys/time.h>
209#include <assert.h>
210#include <signal.h>
211
212#include "manalyzer.h"
213#include "midasio.h"
214
215#ifdef HAVE_ROOT_XML
216#include "XmlOdb.h"
217#endif
218
219#ifdef HAVE_THTTP_SERVER
220#include "THttpServer.h"
221#endif
222
223#ifdef HAVE_ROOT
224#include <TSystem.h>
225#endif
226
227//////////////////////////////////////////////////////////
228//
229// Methods of TARegisterModule
230//
231//////////////////////////////////////////////////////////
232
233std::vector<TAModuleInterface*> *gModules = NULL;
234
236{
237 if (!gModules)
238 gModules = new std::vector<TAModuleInterface*>;
239 gModules->push_back(m);
240}
241
242//////////////////////////////////////////////////////////
243//
244// Methods of EmptyOdb
245//
246//////////////////////////////////////////////////////////
247
248class EmptyOdb: public VirtualOdb
249{
250public:
251 int odbReadArraySize(const char*name) { return 0; }
252 int odbReadAny( const char*name, int index, int tid,void* buf, int bufsize = 0) { return 0; };
253 int odbReadInt( const char*name, int index = 0, int defaultValue = 0) { return defaultValue; }
254 uint32_t odbReadUint32(const char*name, int index = 0, uint32_t defaultValue = 0) { return defaultValue; }
255 float odbReadFloat(const char*name, int index = 0, float defaultValue = 0) { return defaultValue; }
256 double odbReadDouble(const char*name, int index = 0, double defaultValue = 0) { return defaultValue; }
257 bool odbReadBool( const char*name, int index = 0, bool defaultValue = false) { return defaultValue; }
258 const char* odbReadString(const char*name, int index = 0,const char* defaultValue = NULL) { return defaultValue; }
259};
260
261#if 0
262static double GetTimeSec()
263{
264 struct timeval tv;
265 gettimeofday(&tv,NULL);
266 return tv.tv_sec + 0.000001*tv.tv_usec;
267}
268#endif
269
270class RunHandler
271{
272public:
274 std::vector<TARunInterface*> fRunRun;
275
276 RunHandler() // ctor
277 {
278 fRunInfo = NULL;
279 }
280
281 ~RunHandler() // dtor
282 {
283 if (fRunInfo) {
284 delete fRunInfo;
285 fRunInfo = NULL;
286 }
287 }
288
289 void CreateRun(int run_number, const char* file_name)
290 {
291 assert(fRunInfo == NULL);
292 assert(fRunRun.size() == 0);
293
294 fRunInfo = new TARunInfo(run_number, file_name);
295
296 for (unsigned i=0; i<(*gModules).size(); i++)
297 fRunRun.push_back((*gModules)[i]->NewRun(fRunInfo));
298 }
299
300 void BeginRun()
301 {
302 assert(fRunInfo != NULL);
303 assert(fRunInfo->fOdb != NULL);
304 for (unsigned i=0; i<fRunRun.size(); i++)
306 }
307
308 void EndRun()
309 {
310 assert(fRunInfo);
311
312 for (unsigned i=0; i<fRunRun.size(); i++)
314 }
315
317 {
318 assert(fRunInfo);
319
320 for (unsigned i=0; i<fRunRun.size(); i++) {
321 delete fRunRun[i];
322 fRunRun[i] = NULL;
323 }
324
325 fRunRun.clear();
326 assert(fRunRun.size() == 0);
327
328 delete fRunInfo;
329 fRunInfo = NULL;
330 }
331
333 {
334 for (unsigned i=0; i<fRunRun.size(); i++)
336 }
337
338 void AnalyzeEvent(TMEvent* event, TAFlags* flags, TMWriterInterface *writer)
339 {
340 assert(fRunInfo != NULL);
341 assert(fRunInfo->fOdb != NULL);
342
343 TAFlowEvent* flow = NULL;
344
345 for (unsigned i=0; i<fRunRun.size(); i++) {
346 flow = fRunRun[i]->Analyze(fRunInfo, event, flags, flow);
347 if (*flags & TAFlag_SKIP)
348 break;
349 }
350
351 if (*flags & TAFlag_WRITE)
352 if (writer)
353 TMWriteEvent(writer, event);
354
355 if (flow)
356 delete flow;
357 }
358};
359
360#ifdef HAVE_MIDAS
361
362#include "TMidasOnline.h"
363
364#ifdef HAVE_ROOT
365#include "TSystem.h"
366#endif
367
369{
370public:
372 int fNumAnalyze;
374 bool fQuit;
375
376 OnlineHandler(int num_analyze, TMWriterInterface* writer) // ctor
377 {
378 fQuit = false;
379 fNumAnalyze = num_analyze;
380 fWriter = writer;
381 }
382
384 {
385 fWriter = NULL;
386 }
387
388 void StartRun(int run_number)
389 {
390 fRun.CreateRun(run_number, NULL);
392 fRun.BeginRun();
393 }
394
395 void Transition(int transition, int run_number, int transition_time)
396 {
397 //printf("OnlineHandler::Transtion: transition %d, run %d, time %d\n", transition, run_number, transition_time);
398
399 if (transition == TR_START) {
400 if (fRun.fRunInfo) {
401 fRun.EndRun();
402 fRun.fRunInfo->fOdb = NULL;
403 fRun.DeleteRun();
404 }
405 assert(fRun.fRunInfo == NULL);
406
407 StartRun(run_number);
408 printf("Begin run: %d\n", run_number);
409 } else if (transition == TR_STOP) {
410 fRun.EndRun();
411 fRun.fRunInfo->fOdb = NULL;
412 fRun.DeleteRun();
413 printf("End of run %d\n", run_number);
414 }
415 }
416
417 void Event(const void* data, int data_size)
418 {
419 //printf("OnlineHandler::Event: ptr %p, size %d\n", data, data_size);
420
421 if (!fRun.fRunInfo) {
422 StartRun(0); // start fake run for events outside of a run
423 }
424
425 TMEvent* event = new TMEvent(data, data_size);
426
427 TAFlags flags = 0;
428
429 fRun.AnalyzeEvent(event, &flags, fWriter);
430
431 if (flags & TAFlag_QUIT)
432 fQuit = true;
433
434 if (fNumAnalyze > 0) {
435 fNumAnalyze--;
436 if (fNumAnalyze == 0)
437 fQuit = true;
438 }
439
440 if (event) {
441 delete event;
442 event = NULL;
443 }
444 }
445};
446
447static int ProcessMidasOnline(const std::vector<std::string>& args, const char* hostname, const char* exptname, int num_analyze, TMWriterInterface* writer)
448{
450
451 int err = midas->connect(hostname, exptname, "rootana");
452 if (err != 0) {
453 fprintf(stderr,"Cannot connect to MIDAS, error %d\n", err);
454 return -1;
455 }
456
457 OnlineHandler* h = new OnlineHandler(num_analyze, writer);
458
459 midas->RegisterHandler(h);
460 midas->registerTransitions();
461
462 /* reqister event requests */
463
464 midas->eventRequest("SYSTEM",-1,-1,(1<<1));
465
466 int run_number = midas->odbReadInt("/runinfo/Run number");
467 int run_state = midas->odbReadInt("/runinfo/State");
468
469 for (unsigned i=0; i<(*gModules).size(); i++)
470 (*gModules)[i]->Init(args);
471
472 if ((run_state == STATE_RUNNING)||(run_state == STATE_PAUSED)) {
473 h->StartRun(run_number);
474 }
475
476 while (!h->fQuit) {
477#ifdef HAVE_THTTP_SERVER
479 TARootHelper::fgHttpServer->ProcessRequests();
480 }
481#endif
482#ifdef HAVE_ROOT
484 gSystem->DispatchOneEvent(kTRUE);
485 }
486#endif
487 if (!TMidasOnline::instance()->poll(10))
488 break;
489 }
490
491 if (h->fRun.fRunInfo) {
492 h->fRun.EndRun();
493 h->fRun.fRunInfo->fOdb = NULL;
494 h->fRun.DeleteRun();
495 }
496
497 for (unsigned i=0; i<(*gModules).size(); i++)
498 (*gModules)[i]->Finish();
499
500 /* disconnect from experiment */
501 midas->disconnect();
502
503 return 0;
504}
505
506#endif
507
508static int ProcessMidasFiles(const std::vector<std::string>& files, const std::vector<std::string>& args, int num_skip, int num_analyze, TMWriterInterface* writer)
509{
510 for (unsigned i=0; i<(*gModules).size(); i++)
511 (*gModules)[i]->Init(args);
512
513 RunHandler run;
514
515 bool done = false;
516
517 for (unsigned i=0; i<files.size(); i++) {
518 std::string filename = files[i];
519
520 TMReaderInterface *reader = TMNewReader(filename.c_str());
521
522 if (reader->fError) {
523 printf("Could not open \"%s\", error: %s\n", filename.c_str(), reader->fErrorString.c_str());
524 delete reader;
525 continue;
526 }
527
528 while (1) {
529 TMEvent* event = TMReadEvent(reader);
530
531 if (!event) // EOF
532 break;
533
534 if (event->error) {
535 delete event;
536 break;
537 }
538
539 if (event->event_id == 0x8000) // begin of run event
540 {
541 int runno = event->serial_number;
542
543 if (run.fRunInfo) {
544 if (run.fRunInfo->fRunNo == runno) {
545 // next subrun file, nothing to do
546 run.fRunInfo->fFileName = filename;
547 } else {
548 // file with a different run number
549 run.EndRun();
550 run.DeleteRun();
551 }
552 }
553
554 if (!run.fRunInfo) {
555 run.CreateRun(runno, filename.c_str());
556#ifdef HAVE_ROOT_XML
557 run.fRunInfo->fOdb = new XmlOdb(event->GetEventData(), event->data_size);
558#else
559 run.fRunInfo->fOdb = new EmptyOdb();
560#endif
561 run.BeginRun();
562 }
563
564 assert(run.fRunInfo);
565
566 run.AnalyzeSpecialEvent(event);
567
568 if (writer)
569 TMWriteEvent(writer, event);
570 }
571 else if (event->event_id == 0x8001) // end of run event
572 {
573 //int runno = event->serial_number;
574 run.AnalyzeSpecialEvent(event);
575 if (writer)
576 TMWriteEvent(writer, event);
577
578 if (run.fRunInfo->fOdb) {
579 delete run.fRunInfo->fOdb;
580 run.fRunInfo->fOdb = NULL;
581 }
582
583#ifdef HAVE_ROOT_XML
584 run.fRunInfo->fOdb = new XmlOdb(event->GetEventData(), event->data_size);
585#else
586 run.fRunInfo->fOdb = new EmptyOdb();
587#endif
588 }
589 else if (event->event_id == 0x8002) // message event
590 {
591 run.AnalyzeSpecialEvent(event);
592 if (writer)
593 TMWriteEvent(writer, event);
594 }
595 else
596 {
597 if (!run.fRunInfo) {
598 // create a fake begin of run
599 run.CreateRun(0, filename.c_str());
600 run.fRunInfo->fOdb = new EmptyOdb();
601 run.BeginRun();
602 }
603
604 if (num_skip > 0) {
605 num_skip--;
606 } else {
607 TAFlags flags = 0;
608
609 run.AnalyzeEvent(event, &flags, writer);
610
611 if (flags & TAFlag_QUIT)
612 done = true;
613
614 if (num_analyze > 0) {
615 num_analyze--;
616 if (num_analyze == 0)
617 done = true;
618 }
619 }
620 }
621
622 delete event;
623
624 if (done)
625 break;
626
627#ifdef HAVE_ROOT
629 gSystem->DispatchOneEvent(kTRUE);
630 }
631#endif
632 }
633
634 reader->Close();
635 delete reader;
636
637 if (done)
638 break;
639 }
640
641 if (run.fRunInfo) {
642 run.EndRun();
643 run.DeleteRun();
644 }
645
646 for (unsigned i=0; i<(*gModules).size(); i++)
647 (*gModules)[i]->Finish();
648
649 return 0;
650}
651
652static bool gEnableShowMem = false;
653
654#if 0
655static int ShowMem(const char* label)
656{
657 if (!gEnableShowMem)
658 return 0;
659
660 FILE* fp = fopen("/proc/self/statm","r");
661 if (!fp)
662 return 0;
663
664 int mem = 0;
665 fscanf(fp,"%d",&mem);
666 fclose(fp);
667
668 if (label)
669 printf("memory at %s is %d\n", label, mem);
670
671 return mem;
672}
673#endif
674
676{
677public:
679 : TARunInterface(runinfo)
680 {
681 if (gTrace)
682 printf("EventDumpRun::ctor, run %d\n", runinfo->fRunNo);
683 }
684
686 {
687 if (gTrace)
688 printf("EventDumpRun::dtor!\n");
689 }
690
691 void BeginRun(TARunInfo* runinfo)
692 {
693 printf("EventDumpRun::BeginRun, run %d\n", runinfo->fRunNo);
694 }
695
696 void EndRun(TARunInfo* runinfo)
697 {
698 printf("EventDumpRun::EndRun, run %d\n", runinfo->fRunNo);
699 }
700
701 void PauseRun(TARunInfo* runinfo)
702 {
703 printf("EventDumpRun::PauseRun, run %d\n", runinfo->fRunNo);
704 }
705
706 void ResumeRun(TARunInfo* runinfo)
707 {
708 printf("EventDumpRun::ResumeRun, run %d\n", runinfo->fRunNo);
709 }
710
711 TAFlowEvent* Analyze(TARunInfo* runinfo, TMEvent* event, TAFlags* flags, TAFlowEvent* flow)
712 {
713 printf("EventDumpRun::Analyze, run %d, ", runinfo->fRunNo);
714 event->FindAllBanks();
715 std::string h = event->HeaderToString();
716 std::string b = event->BankListToString();
717 printf("%s: %s\n", h.c_str(), b.c_str());
718 return flow;
719 }
720
721 void AnalyzeSpecialEvent(TARunInfo* runinfo, TMEvent* event)
722 {
723 printf("EventDumpRun::AnalyzeSpecialEvent, run %d, event serno %d, id 0x%04x, data size %d\n", runinfo->fRunNo, event->serial_number, (int)event->event_id, event->data_size);
724 }
725};
726
728{
729public:
730
731 void Init(const std::vector<std::string> &args)
732 {
733 if (gTrace)
734 printf("EventDumpModule::Init!\n");
735 }
736
737 void Finish()
738 {
739 if (gTrace)
740 printf("EventDumpModule::Finish!\n");
741 }
742
744 {
745 if (gTrace)
746 printf("EventDumpModule::NewRun, run %d, file %s\n", runinfo->fRunNo, runinfo->fFileName.c_str());
747 return new EventDumpRun(runinfo);
748 }
749};
750
751#ifdef HAVE_ROOT
752#include <TGMenu.h>
753#include <TGButton.h>
754#include <TBrowser.h>
755
756#define CTRL_QUIT 1
757#define CTRL_NEXT 2
758#define CTRL_CONTINUE 3
759#define CTRL_PAUSE 4
760
761#define CTRL_TBROWSER 11
762
763class XCtrl
764{
765public:
767
768 XCtrl() // ctor
769 {
770 fValue = 0;
771 }
772};
773
775{
776public:
779
780 XButton(TGWindow*p, const char* text, XCtrl* ctrl, int value) // ctor
781 : TGTextButton(p, text)
782 {
783 fCtrl = ctrl;
784 fValue = value;
785 }
786
787#if 0
788 void Pressed()
789 {
790 printf("Pressed!\n");
791 }
792
793 void Released()
794 {
795 printf("Released!\n");
796 }
797#endif
798
799 void Clicked()
800 {
801 //printf("Clicked button %s, value %d!\n", GetString().Data(), fValue);
802 if (fCtrl)
804 //gSystem->ExitLoop();
805 }
806};
807
808class MainWindow: public TGMainFrame
809{
810private:
811 TGPopupMenu* fMenu;
812 TGMenuBar* fMenuBar;
813 TGLayoutHints* menuBarLayout;
814 TGLayoutHints* menuBarItemLayout;
815
816public:
818
819public:
820 MainWindow(const TGWindow*w, int s1, int s2, XCtrl* ctrl) // ctor
821 : TGMainFrame(w, s1, s2)
822 {
823 if (gTrace)
824 printf("MainWindow::ctor!\n");
825
826 fCtrl = ctrl;
827 //SetCleanup(kDeepCleanup);
828
829 SetWindowName("ROOT Analyzer Control");
830
831 // layout the gui
832 fMenu = new TGPopupMenu(gClient->GetRoot());
833 fMenu->AddEntry("New TBrowser", CTRL_TBROWSER);
834 fMenu->AddEntry("-", 0);
835 fMenu->AddEntry("Next", CTRL_NEXT);
836 fMenu->AddEntry("Continue", CTRL_CONTINUE);
837 fMenu->AddEntry("Pause", CTRL_PAUSE);
838 fMenu->AddEntry("-", 0);
839 fMenu->AddEntry("Quit", CTRL_QUIT);
840
841 menuBarItemLayout = new TGLayoutHints(kLHintsTop|kLHintsLeft, 0, 4, 0, 0);
842
843 fMenu->Associate(this);
844
845 fMenuBar = new TGMenuBar(this, 1, 1, kRaisedFrame);
846 fMenuBar->AddPopup("&Rootana", fMenu, menuBarItemLayout);
847 fMenuBar->Layout();
848
849 menuBarLayout = new TGLayoutHints(kLHintsTop|kLHintsLeft|kLHintsExpandX);
850 AddFrame(fMenuBar, menuBarLayout);
851
852 // Create a container frames containing buttons
853
854 // one button is resized up to the parent width.
855 // Note! this width should be fixed!
856 TGVerticalFrame *hframe1 = new TGVerticalFrame(this, 170, 50, kFixedWidth);
857
858 // to take whole space we need to use kLHintsExpandX layout hints
859 hframe1->AddFrame(new XButton(hframe1, "&Next ", ctrl, CTRL_NEXT),
860 new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 0, 2, 2));
861 AddFrame(hframe1, new TGLayoutHints(kLHintsCenterX, 2, 2, 5, 1));
862
863 // two buttons are resized up to the parent width.
864 // Note! this width should be fixed!
865 TGCompositeFrame *cframe1 = new TGCompositeFrame(this, 170, 20, kHorizontalFrame | kFixedWidth);
866
867 // to share whole parent space we need to use kLHintsExpandX layout hints
868 cframe1->AddFrame(new XButton(cframe1, "&Continue", ctrl, CTRL_CONTINUE),
869 new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 2, 2, 2));
870
871 cframe1->AddFrame(new XButton(cframe1, "&Pause", ctrl, CTRL_PAUSE),
872 new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 2, 2, 2));
873
874 AddFrame(cframe1, new TGLayoutHints(kLHintsCenterX, 2, 2, 5, 1));
875
876 // three buttons are resized up to the parent width.
877 // Note! this width should be fixed!
878 TGCompositeFrame *cframe2 = new TGCompositeFrame(this, 170, 20, kHorizontalFrame | kFixedWidth);
879
880#if 0
881 TGButton* ok = new XButton(cframe2, "OK", ctrl, 0);
882 // to share whole parent space we need to use kLHintsExpandX layout hints
883 cframe2->AddFrame(ok, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 3, 2, 2, 2));
884
885 TGButton* cancel = new XButton(cframe2, "Cancel ", ctrl, 0);
886 cframe2->AddFrame(cancel, new TGLayoutHints(kLHintsTop | kLHintsExpandX, 3, 2, 2, 2));
887#endif
888
889 cframe2->AddFrame(new XButton(cframe2, "&Quit ", ctrl, CTRL_QUIT),
890 new TGLayoutHints(kLHintsTop | kLHintsExpandX, 2, 0, 2, 2));
891
892 AddFrame(cframe2, new TGLayoutHints(kLHintsCenterX, 2, 2, 5, 1));
893
894 MapSubwindows();
895 Layout();
896 MapWindow();
897 }
898
899 ~MainWindow() // dtor // Closing the control window closes the whole program
900 {
901 if (gTrace)
902 printf("MainWindow::dtor!\n");
903
904 delete fMenu;
905 delete fMenuBar;
906 delete menuBarLayout;
907 delete menuBarItemLayout;
908 }
909
911 {
912 if (gTrace)
913 printf("MainWindow::CloseWindow()\n");
914
915 if (fCtrl)
917 //gSystem->ExitLoop();
918 }
919
920 Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
921 {
922 //printf("GUI Message %d %d %d\n",(int)msg,(int)parm1,(int)parm2);
923 switch (GET_MSG(msg))
924 {
925 default:
926 break;
927 case kC_COMMAND:
928 switch (GET_SUBMSG(msg))
929 {
930 default:
931 break;
932 case kCM_MENU:
933 //printf("parm1 %d\n", (int)parm1);
934 switch (parm1)
935 {
936 case CTRL_TBROWSER:
937 new TBrowser();
938 break;
939 default:
940 //printf("Control %d!\n", (int)parm1);
941 if (fCtrl)
942 fCtrl->fValue = parm1;
943 //gSystem->ExitLoop();
944 break;
945 }
946 break;
947 }
948 break;
949 }
950
951 return kTRUE;
952 }
953};
954#endif
955
957{
958public:
960 int fSkip;
961#ifdef HAVE_ROOT
962 static XCtrl* fgCtrl;
964#endif
965
967 : TARunInterface(runinfo)
968 {
969 if (gTrace)
970 printf("InteractiveRun::ctor, run %d\n", runinfo->fRunNo);
971 fContinue = false;
972 fSkip = 0;
973#ifdef HAVE_ROOT
974 if (!fgCtrl)
975 fgCtrl = new XCtrl;
976 if (!fgCtrlWindow && runinfo->fRoot->fgApp) {
977 fgCtrlWindow = new MainWindow(gClient->GetRoot(), 200, 300, fgCtrl);
978 }
979#endif
980 }
981
983 {
984 if (gTrace)
985 printf("InteractiveRun::dtor!\n");
986 }
987
988 void BeginRun(TARunInfo* runinfo)
989 {
990 printf("InteractiveRun::BeginRun, run %d\n", runinfo->fRunNo);
991 }
992
993 void EndRun(TARunInfo* runinfo)
994 {
995 printf("InteractiveRun::EndRun, run %d\n", runinfo->fRunNo);
996
997#ifdef HAVE_ROOT
998 if (fgCtrlWindow && runinfo->fRoot->fgApp) {
999 while (1) {
1000#ifdef HAVE_THTTP_SERVER
1002 TARootHelper::fgHttpServer->ProcessRequests();
1003 }
1004#endif
1005#ifdef HAVE_ROOT
1006 if (TARootHelper::fgApp) {
1007 gSystem->DispatchOneEvent(kTRUE);
1008 }
1009#endif
1010#ifdef HAVE_MIDAS
1011 if (!TMidasOnline::instance()->sleep(10)) {
1012 // FIXME: indicate that we should exit the analyzer
1013 return;
1014 }
1015#else
1016 gSystem->Sleep(10);
1017#endif
1018
1019 int ctrl = fgCtrl->fValue;
1020 fgCtrl->fValue = 0;
1021
1022 switch (ctrl) {
1023 case CTRL_QUIT:
1024 return;
1025 case CTRL_NEXT:
1026 return;
1027 case CTRL_CONTINUE:
1028 return;
1029 }
1030 }
1031 }
1032#endif
1033 }
1034
1035 void PauseRun(TARunInfo* runinfo)
1036 {
1037 printf("InteractiveRun::PauseRun, run %d\n", runinfo->fRunNo);
1038 }
1039
1040 void ResumeRun(TARunInfo* runinfo)
1041 {
1042 printf("InteractiveRun::ResumeRun, run %d\n", runinfo->fRunNo);
1043 }
1044
1045 TAFlowEvent* Analyze(TARunInfo* runinfo, TMEvent* event, TAFlags* flags, TAFlowEvent* flow)
1046 {
1047 printf("InteractiveRun::Analyze, run %d, %s\n", runinfo->fRunNo, event->HeaderToString().c_str());
1048
1049#ifdef HAVE_ROOT
1050 if (fgCtrl->fValue == CTRL_QUIT) {
1051 *flags |= TAFlag_QUIT;
1052 return flow;
1053 } else if (fgCtrl->fValue == CTRL_PAUSE) {
1054 fContinue = false;
1055 }
1056#endif
1057
1058 if (fContinue && !(*flags & TAFlag_DISPLAY)) {
1059 return flow;
1060 } else {
1061 fContinue = false;
1062 }
1063
1064 if (fSkip > 0) {
1065 fSkip--;
1066 return flow;
1067 }
1068
1069#ifdef HAVE_ROOT
1070 if (fgCtrlWindow && runinfo->fRoot->fgApp) {
1071 while (1) {
1072#ifdef HAVE_THTTP_SERVER
1074 TARootHelper::fgHttpServer->ProcessRequests();
1075 }
1076#endif
1077#ifdef HAVE_ROOT
1078 if (TARootHelper::fgApp) {
1079 gSystem->DispatchOneEvent(kTRUE);
1080 }
1081#endif
1082#ifdef HAVE_MIDAS
1083 if (!TMidasOnline::instance()->sleep(10)) {
1084 *flags |= TAFlag_QUIT;
1085 return flow;
1086 }
1087#else
1088 gSystem->Sleep(10);
1089#endif
1090
1091 int ctrl = fgCtrl->fValue;
1092 fgCtrl->fValue = 0;
1093
1094 switch (ctrl) {
1095 case CTRL_QUIT:
1096 *flags |= TAFlag_QUIT;
1097 return flow;
1098 case CTRL_NEXT:
1099 return flow;
1100 case CTRL_CONTINUE:
1101 fContinue = true;
1102 return flow;
1103 }
1104 }
1105 }
1106#endif
1107
1108 while (1) {
1109 char str[256];
1110 fprintf(stdout, "manalyzer> "); fflush(stdout);
1111 fgets(str, sizeof(str)-1, stdin);
1112
1113 printf("command [%s]\n", str);
1114
1115 if (str[0] == 'h') { // "help"
1116 printf("Interactive manalyzer commands:\n");
1117 printf(" q - quit\n");
1118 printf(" h - help\n");
1119 printf(" c - continue until next TAFlag_DISPLAY event\n");
1120 printf(" n - next event\n");
1121 printf(" aNNN - analyze N events, i.e. \"a10\"\n");
1122 } else if (str[0] == 'q') { // "quit"
1123 *flags |= TAFlag_QUIT;
1124 return flow;
1125 } else if (str[0] == 'n') { // "next"
1126 return flow;
1127 } else if (str[0] == 'c') { // "continue"
1128 fContinue = true;
1129 return flow;
1130 } else if (str[0] == 'a') { // "analyze" N events
1131 int num = atoi(str+1);
1132 printf("Analyzing %d events\n", num);
1133 if (num > 0) {
1134 fSkip = num-1;
1135 }
1136 return flow;
1137 }
1138 }
1139
1140 return flow;
1141 }
1142
1144 {
1145 if (gTrace)
1146 printf("InteractiveRun::AnalyzeSpecialEvent, run %d, event serno %d, id 0x%04x, data size %d\n", runinfo->fRunNo, event->serial_number, (int)event->event_id, event->data_size);
1147 }
1148};
1149
1152
1154{
1155public:
1156
1157 void Init(const std::vector<std::string> &args)
1158 {
1159 if (gTrace)
1160 printf("InteractiveModule::Init!\n");
1161 }
1162
1163 void Finish()
1164 {
1165 if (gTrace)
1166 printf("InteractiveModule::Finish!\n");
1167 }
1168
1170 {
1171 if (gTrace)
1172 printf("InteractiveModule::NewRun, run %d, file %s\n", runinfo->fRunNo, runinfo->fFileName.c_str());
1173 return new InteractiveRun(runinfo);
1174 }
1175};
1176
1177static void help()
1178{
1179 printf("\nUsage:\n");
1180 printf("\n./analyzer.exe [-h] [-R8081] [-oOutputfile.mid] [file1 file2 ...] [-- arguments passed to modules ...]\n");
1181 printf("\n");
1182 printf("\t-h: print this help message\n");
1183 printf("\t-Hhostname: connect to MIDAS experiment on given host\n");
1184 printf("\t-Eexptname: connect to this MIDAS experiment\n");
1185 printf("\t-oOutputfile.mid: write selected events into this file\n");
1186 printf("\t-Rnnnn: Start the ROOT THttpServer HTTP server on specified tcp port, access by firefox http://localhost:8081\n");
1187 printf("\t-Xnnnn: Start the Xml server on specified tcp port (for use with roody -Xlocalhost:9091)\n");
1188 printf("\t-Pnnnn: Start the TNetDirectory server on specified tcp port (for use with roody -Plocalhost:9091)\n");
1189 printf("\t-eNNN: Number of events to analyze\n");
1190 printf("\t-sNNN: Number of events to skip before starting analysis\n");
1191 printf("\t--dump: activate the event dump module\n");
1192 printf("\t-t: Enable tracing of constructors, destructors and function calls\n");
1193 printf("\t-m: Enable memory leak debugging\n");
1194 printf("\t-g: Enable graphics display when processing data files\n");
1195 printf("\t-i: Enable intractive mode\n");
1196 printf("\t--: All following arguments are passed to the analyzer modules Init() method\n");
1197 printf("\n");
1198 printf("Example1: analyze online data: ./analyzer.exe -P9091\n");
1199 printf("Example2: analyze existing data: ./analyzer.exe /data/alpha/current/run00500.mid\n");
1200 exit(1);
1201}
1202
1203// Main function call
1204
1205int manalyzer_main(int argc, char *argv[])
1206{
1207 setbuf(stdout, NULL);
1208 setbuf(stderr, NULL);
1209
1210 signal(SIGILL, SIG_DFL);
1211 signal(SIGBUS, SIG_DFL);
1212 signal(SIGSEGV, SIG_DFL);
1213 signal(SIGPIPE, SIG_DFL);
1214
1215 std::vector<std::string> args;
1216 for (int i=0; i<argc; i++) {
1217 if (strcmp(argv[i],"-h")==0)
1218 help(); // does not return
1219 args.push_back(argv[i]);
1220 }
1221
1222 int tcpPort = 0;
1223 int xmlTcpPort = 0;
1224 int httpPort = 0;
1225 const char* hostname = NULL;
1226 const char* exptname = NULL;
1227
1228 int num_skip = 0;
1229 int num_analyze = 0;
1230
1231 TMWriterInterface *writer = NULL;
1232
1233 bool event_dump = false;
1234 bool root_graphics = false;
1235 bool interactive = false;
1236
1237 std::vector<std::string> files;
1238 std::vector<std::string> modargs;
1239
1240 for (unsigned int i=1; i<args.size(); i++) { // loop over the commandline options
1241 const char* arg = args[i].c_str();
1242 //printf("argv[%d] is %s\n",i,arg);
1243
1244 if (args[i] == "--") {
1245 for (unsigned j=i+1; j<args.size(); j++)
1246 modargs.push_back(args[j]);
1247 break;
1248 } else if (args[i] == "--dump") {
1249 event_dump = true;
1250 } else if (args[i] == "-g") {
1251 root_graphics = true;
1252 } else if (args[i] == "-i") {
1253 interactive = true;
1254 } else if (args[i] == "-t") {
1255 gTrace = true;
1258 } else if (strncmp(arg,"-o",2)==0) {
1259 writer = TMNewWriter(arg+2);
1260 } else if (strncmp(arg,"-s",2)==0) {
1261 num_skip = atoi(arg+2);
1262 } else if (strncmp(arg,"-e",2)==0) {
1263 num_analyze = atoi(arg+2);
1264 } else if (strncmp(arg,"-m",2)==0) { // Enable memory debugging
1265 gEnableShowMem = true;
1266 } else if (strncmp(arg,"-P",2)==0) { // Set the histogram server port
1267 tcpPort = atoi(arg+2);
1268 } else if (strncmp(arg,"-X",2)==0) { // Set the histogram server port
1269 xmlTcpPort = atoi(arg+2);
1270 } else if (strncmp(arg,"-R",2)==0) { // Set the ROOT THttpServer HTTP port
1271 httpPort = atoi(arg+2);
1272 } else if (strncmp(arg,"-H",2)==0) {
1273 hostname = strdup(arg+2);
1274 } else if (strncmp(arg,"-E",2)==0) {
1275 exptname = strdup(arg+2);
1276 } else if (strcmp(arg,"-h")==0) {
1277 help(); // does not return
1278 } else if (arg[0] == '-') {
1279 help(); // does not return
1280 } else {
1281 files.push_back(args[i]);
1282 }
1283 }
1284
1285 if (!gModules)
1286 gModules = new std::vector<TAModuleInterface*>;
1287
1288 if ((*gModules).size() == 0)
1289 event_dump = true;
1290
1291 if (event_dump)
1292 (*gModules).push_back(new EventDumpModule);
1293
1294 if (interactive)
1295 (*gModules).push_back(new InteractiveModule);
1296
1297 printf("Registered modules: %d\n", (int)(*gModules).size());
1298
1299#ifdef HAVE_ROOT
1300 if (root_graphics) {
1301 TARootHelper::fgApp = new TApplication("manalyzer", NULL, NULL, 0, 0);
1302 }
1303
1304 TARootHelper::fgDir = new TDirectory("manalyzer", "location of histograms");
1306#endif
1307
1308#ifdef XHAVE_LIBNETDIRECTORY
1309 if (tcpPort) {
1312 }
1313#else
1314 if (tcpPort)
1315 fprintf(stderr,"ERROR: No support for the TNetDirectory server!\n");
1316#endif
1317
1318#ifdef HAVE_XMLSERVER
1319 if (xmlTcpPort) {
1320 XmlServer* s = new XmlServer();
1321 s->SetVerbose(true);
1322 s->Start(xmlTcpPort);
1323 s->Export(gROOT, "ROOT");
1324 s->Export(TARootHelper::fgDir, "manalyzer");
1326 }
1327#else
1328 if (xmlTcpPort)
1329 fprintf(stderr,"ERROR: No support for the XML Server!\n");
1330#endif
1331
1332 if (httpPort) {
1333#ifdef HAVE_THTTP_SERVER
1334 char str[256];
1335 sprintf(str, "http:127.0.0.1:%d", httpPort);
1336 THttpServer *s = new THttpServer(str);
1337 //s->SetTimer(100, kFALSE);
1339#else
1340 fprintf(stderr,"ERROR: No support for the THttpServer!\n");
1341#endif
1342 }
1343
1344 for (unsigned i=0; i<files.size(); i++) {
1345 printf("file[%d]: %s\n", i, files[i].c_str());
1346 }
1347
1348 if (files.size() > 0) {
1349 ProcessMidasFiles(files, modargs, num_skip, num_analyze, writer);
1350 } else {
1351#ifdef HAVE_MIDAS
1352 ProcessMidasOnline(modargs, hostname, exptname, num_analyze, writer);
1353#endif
1354 }
1355
1356 if (writer) {
1357 writer->Close();
1358 delete writer;
1359 writer = NULL;
1360 }
1361
1362 return 0;
1363}
1364
1365/* emacs
1366 * Local Variables:
1367 * tab-width: 8
1368 * c-basic-offset: 3
1369 * indent-tabs-mode: nil
1370 * End:
1371 */
double GetTimeSec()
int odbReadArraySize(const char *name)
int odbReadInt(const char *name, int index=0, int defaultValue=0)
double odbReadDouble(const char *name, int index=0, double defaultValue=0)
const char * odbReadString(const char *name, int index=0, const char *defaultValue=NULL)
bool odbReadBool(const char *name, int index=0, bool defaultValue=false)
int odbReadAny(const char *name, int index, int tid, void *buf, int bufsize=0)
uint32_t odbReadUint32(const char *name, int index=0, uint32_t defaultValue=0)
float odbReadFloat(const char *name, int index=0, float defaultValue=0)
void Init(const std::vector< std::string > &args)
TARunInterface * NewRun(TARunInfo *runinfo)
TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
void AnalyzeSpecialEvent(TARunInfo *runinfo, TMEvent *event)
void BeginRun(TARunInfo *runinfo)
void PauseRun(TARunInfo *runinfo)
void EndRun(TARunInfo *runinfo)
EventDumpRun(TARunInfo *runinfo)
void ResumeRun(TARunInfo *runinfo)
TARunInterface * NewRun(TARunInfo *runinfo)
void Init(const std::vector< std::string > &args)
void ResumeRun(TARunInfo *runinfo)
static MainWindow * fgCtrlWindow
InteractiveRun(TARunInfo *runinfo)
static XCtrl * fgCtrl
void BeginRun(TARunInfo *runinfo)
void AnalyzeSpecialEvent(TARunInfo *runinfo, TMEvent *event)
void EndRun(TARunInfo *runinfo)
void PauseRun(TARunInfo *runinfo)
TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
void CloseWindow()
TGLayoutHints * menuBarItemLayout
TGLayoutHints * menuBarLayout
TGPopupMenu * fMenu
MainWindow(const TGWindow *w, int s1, int s2, XCtrl *ctrl)
TGMenuBar * fMenuBar
Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2)
void Event(const void *data, int data_size)
void Transition(int transition, int run_number, int transition_time)
TMWriterInterface * fWriter
RunHandler fRun
OnlineHandler(int num_analyze, TMWriterInterface *writer)
void StartRun(int run_number)
std::vector< TARunInterface * > fRunRun
TARunInfo * fRunInfo
std::vector< TARunObject * > fRunRun
void AnalyzeEvent(TMEvent *event, TAFlags *flags, TMWriterInterface *writer)
void CreateRun(int run_number, const char *file_name)
void AnalyzeSpecialEvent(TMEvent *event)
void EndRun(TAFlags *flags)
virtual ~TAFlowEvent()
Definition manalyzer.cxx:91
TAFlowEvent * fNext
Definition manalyzer.h:51
virtual void Finish()
virtual void Init(const std::vector< std::string > &args)
TARegisterModule(TAModuleInterface *m)
static TApplication * fgApp
Definition manalyzer.h:156
static THttpServer * fgHttpServer
Definition manalyzer.h:157
static XmlServer * fgXmlServer
TFile * fOutputFile
Definition manalyzer.h:154
static TDirectory * fgDir
Definition manalyzer.h:155
std::string fFileName
Definition manalyzer.h:25
MVOdb * fOdb
Definition manalyzer.h:26
int fRunNo
Definition manalyzer.h:24
TARootHelper * fRoot
Definition manalyzer.h:27
virtual void EndRun(TARunInfo *runinfo)
virtual void PauseRun(TARunInfo *runinfo)
virtual TAFlowEvent * Analyze(TARunInfo *runinfo, TMEvent *event, TAFlags *flags, TAFlowEvent *flow)
virtual void AnalyzeSpecialEvent(TARunInfo *runinfo, TMEvent *event)
virtual void ResumeRun(TARunInfo *runinfo)
virtual void BeginRun(TARunInfo *runinfo)
virtual Bool_t cd(const char *path=0)
std::string HeaderToString() const
print the MIDAS event header
Definition midasio.cxx:656
uint32_t serial_number
MIDAS event serial number.
Definition midasio.h:59
uint32_t data_size
MIDAS event data size.
Definition midasio.h:61
uint16_t event_id
MIDAS event ID.
Definition midasio.h:57
MIDAS online connection, including access to online ODB.
virtual int Close()=0
std::string fErrorString
Definition midasio.h:116
static bool fgTrace
Definition midasio.h:117
virtual int Close()=0
static bool fgTrace
Definition midasio.h:127
void RegisterHandler(TMHandlerInterface *h)
void registerTransitions()
Ask MIDAS to tell us about run transitions.
int disconnect()
Disconnect from MIDAS.
static TMidasOnline * instance()
int connect(const char *hostname, const char *exptname, const char *progname)
Connect to MIDAS experiment.
int eventRequest(const char *bufferName, int eventId, int triggerMask, int samplingType, bool poll=false)
Request data for delivery via callback (setEventHandler) or by polling (via receiveEvent)
XButton(TGWindow *p, const char *text, XCtrl *ctrl, int value)
XCtrl * fCtrl
void Clicked()
Access to ODB saved in XML format inside midas .mid files.
Definition mxmlodb.cxx:172
void Export(TDirectory *dir, const char *exportName)
void SetVerbose(bool verbose)
void Start(int port)
int TAFlags
Definition manalyzer.h:72
#define TAFlag_DISPLAY
Definition manalyzer.h:78
#define TAFlag_SKIP
Definition manalyzer.h:75
#define TAFlag_WRITE
Definition manalyzer.h:77
#define TAFlag_QUIT
Definition manalyzer.h:76
void TMWriteEvent(TMWriterInterface *writer, const TMEvent *event)
Definition midasio.cxx:651
TMWriterInterface * TMNewWriter(const char *destination)
Definition midasio.cxx:543
TMEvent * TMReadEvent(TMReaderInterface *reader)
Definition midasio.cxx:585
TMReaderInterface * TMNewReader(const char *source)
Definition midasio.cxx:447
int TAFlags
std::vector< TAFactory * > * gModules
static bool gTrace
Definition manalyzer.cxx:19
#define CTRL_NEXT
#define CTRL_TBROWSER
#define CTRL_CONTINUE
#define CTRL_QUIT
#define CTRL_PAUSE
static int ProcessMidasFiles(const std::vector< std::string > &files, const std::vector< std::string > &args, int num_skip, int num_analyze, TMWriterInterface *writer)
static bool gTrace
#define CTRL_NEXT
static int ProcessMidasOnline(const std::vector< std::string > &args, const char *hostname, const char *exptname, int num_analyze, TMWriterInterface *writer)
int manalyzer_main(int argc, char *argv[])
#define CTRL_CONTINUE
#define CTRL_QUIT
static bool gEnableShowMem
std::vector< TAModuleInterface * > * gModules
#define CTRL_PAUSE
static void help()
void NetDirectoryExport(TDirectory *dir, const char *exportName)
void VerboseNetDirectoryServer(bool verbose)
void StartNetDirectoryServer(int port, TDirectory *dir)
int ShowMem(const char *label)