f4b7b88faddff8f48bff1b6998f752cedc10506f
[u/mrichter/AliRoot.git] / EVE / macros / alieve_online_new.C
1 /**************************************************************************
2  * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
3  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
4  * full copyright notice.                                                 *
5  **************************************************************************/
6
7 #include "AliEveEventManager.h"
8 #include "AliEveEventManagerEditor.h"
9 #include "AliEveMultiView.h"
10 #include "AliEveMacroExecutor.h"
11 #include "AliEveMacro.h"
12 #include "AliSysInfo.h"
13
14 #include <TH2D.h>
15 #include <TTimeStamp.h>
16 #include <TROOT.h>
17 #include <TEveManager.h>
18 #include <TEveBrowser.h>
19 #include <TEveMacro.h>
20 #include <TGTab.h>
21 #include <TGFileBrowser.h>
22 #include <TInterpreter.h>
23 #include <TEnv.h>
24 #include <TPRegexp.h>
25 #include <TFolder.h>
26 #include <TSystem.h>
27 #include <TSystemDirectory.h>
28 #include <TList.h>
29
30 #include <iostream>
31
32 using namespace std;
33
34 class TEveProjectionManager;
35 class TEveGeoShape;
36 class TEveUtil;
37 class AliTriggerAnalysis;
38 class AliSysInfo;
39
40 TH2D *V0StateHistogram;
41 Bool_t gCenterProjectionsAtPrimaryVertex = kFALSE;
42
43 Int_t      g_pic_id  = 0;
44 Int_t      g_pic_max = 100;
45 TTimeStamp g_pic_prev(0, 0);
46
47 void alieve_init_import_macros();
48
49 void alieve_online_new()
50 {
51     // set OCDB path:
52     //AliEveEventManager::SetCdbUri("local://$ALICE_ROOT/OCDB"); // default OCDB from aliroot
53     //AliEveEventManager::SetCdbUri("local:///local/OCDB/2013"); // OCDB snapshot for particular run
54     AliEveEventManager::SetCdbUri("local:///local/cdb");         // current OCDB snapshot
55     //AliEveEventManager::SetCdbUri("raw://");                   // reading OCDB from alien
56     
57     cout<<"Creating multiview...";
58     AliEveMultiView *multiView = new AliEveMultiView(kTRUE);
59     cout<<"created"<<endl;
60     
61     Info("alieve_init", "Adding standard macros.");
62     TString  hack = gSystem->pwd(); // Problem with TGFileBrowser cding
63     alieve_init_import_macros();
64     gSystem->cd(Form("%s/../src/",gSystem->Getenv("ALICE_ROOT")));
65     gROOT->ProcessLine(".L saveViews.C+");
66     gROOT->ProcessLine(".L geom_gentle.C+");
67     gROOT->ProcessLine(".L geom_gentle_muon.C+");
68     gROOT->ProcessLine(".L geom_gentle_trd.C+");
69     TEveUtil::LoadMacro("saveViews.C");
70     gSystem->cd(hack);
71     cout<<"Standard macros added"<<endl;
72     
73
74     new AliEveEventManager("online", -1);
75     gEve->AddEvent(AliEveEventManager::GetMaster());
76     cout<<"Event manager created"<<endl;
77     
78     TEveUtil::AssertMacro("VizDB_scan.C");
79     gSystem->ProcessEvents();
80     cout<<"VizDB_scan loaded"<<endl;
81     TEveBrowser *browser = gEve->GetBrowser();
82     browser->ShowCloseTab(kFALSE);
83     cout<<"browser created"<<endl;
84     
85     TEveUtil::LoadMacro("geom_gentle.C");
86     cout<<"geom gentle loaded"<<endl;
87     
88     multiView->InitGeomGentle(geom_gentle(),
89                               geom_gentle_rphi(),
90                               geom_gentle_rhoz(),
91                               geom_gentle_rhoz());
92     cout<<"geom gentl inited"<<endl;
93     
94     TEveUtil::LoadMacro("geom_gentle_trd.C");
95     multiView->InitGeomGentleTrd(geom_gentle_trd());
96
97     TEveUtil::LoadMacro("geom_gentle_muon.C");
98     multiView->InitGeomGentleMuon(geom_gentle_muon(), kFALSE, kFALSE, kTRUE);
99  
100     //============================================================================
101     // Standard macros to execute -- not all are enabled by default.
102     //============================================================================
103     
104     printf("============ Setting macro executor ============\n");
105     
106     AliEveMacroExecutor *exec = AliEveEventManager::GetMaster()->GetExecutor();
107     printf("exec created\n");
108     /*
109     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX",         "primary_vertex.C", "primary_vertex",             "",                kTRUE));
110     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Ellipse", "primary_vertex.C", "primary_vertex_ellipse",     "",                kTRUE));
111     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Box",     "primary_vertex.C", "primary_vertex_box",         "kFALSE, 3, 3, 3", kFALSE));
112     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX",         "primary_vertex.C", "primary_vertex_spd",         "",                kTRUE));
113     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Ellipse", "primary_vertex.C", "primary_vertex_ellipse_spd", "",                kTRUE));
114     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Box",     "primary_vertex.C", "primary_vertex_box_spd",     "kFALSE, 3, 3, 3", kFALSE));
115     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX",         "primary_vertex.C", "primary_vertex_tpc",         "",                kFALSE));
116     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Ellipse", "primary_vertex.C", "primary_vertex_ellipse_tpc", "",                kFALSE));
117     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC PVTX Box",     "primary_vertex.C", "primary_vertex_box_tpc",     "kFALSE, 3, 3, 3", kFALSE));
118     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC Track",      "esd_tracks.C",        "esd_tracks",             "", kFALSE));
119     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC Track",      "esd_tracks.C",        "esd_tracks_MI",          "", kFALSE));
120     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC Track MUON", "esd_muon_tracks.C", "esd_muon_tracks",        "kTRUE,kFALSE", kTRUE));
121     */
122     // these macros were leaking:
123     exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC Track","esd_tracks.C",        "esd_tracks_by_category", "", kTRUE));// just a little
124     //exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC FMD",        "fmd_esd.C",           "fmd_esd",                "", kTRUE));//huge leak
125     //
126     
127     // ???
128     // exec->AddMacro(new AliEveMacro(AliEveMacro::kESD, "REC TRD", "trd_detectors.C", "trd_detectors",         "", kFALSE));
129     // trd_tracks disabled due to memory leaks
130     cout<<"macros added to exec"<<endl;
131
132     //----------------------------------------------------------------------------
133     /* something is wrong here:
134     slot = TEveWindow::CreateWindowInTab(browser->GetTabRight());
135     slot->StartEmbedding();
136     AliEveMacroExecutorWindow* exewin = new AliEveMacroExecutorWindow(exec);
137     slot->StopEmbedding("DataSelection");
138     exewin->PopulateMacros();
139     */
140     //============================================================================
141     // Final GUI setup
142     //============================================================================
143     
144     browser->GetTabRight()->SetTab(1);
145     browser->StartEmbedding(TRootBrowser::kBottom);
146     new AliEveEventManagerWindow(AliEveEventManager::GetMaster());
147     browser->StopEmbedding("EventCtrl");
148     
149     browser->MoveResize(0, 0, gClient->GetDisplayWidth(),gClient->GetDisplayHeight() - 32);
150     
151     gEve->FullRedraw3D(kTRUE);
152     gSystem->ProcessEvents();
153     
154     // move and rotate sub-views
155     TGLViewer *glv1 = multiView->Get3DView()->GetGLViewer();
156     TGLViewer *glv2 = multiView->GetRPhiView()->GetGLViewer();
157     TGLViewer *glv3 = multiView->GetRhoZView()->GetGLViewer();
158     
159     glv1->CurrentCamera().RotateRad(-0.4, 0.6);
160     glv2->CurrentCamera().Dolly(90, kFALSE, kFALSE);
161     glv3->CurrentCamera().Dolly(1700, kFALSE, kFALSE);
162     
163     AliEveEventManager::GetMaster()->AddNewEventCommand("alieve_online_on_new_event();");
164     gEve->FullRedraw3D();
165     gSystem->ProcessEvents();
166     gEve->Redraw3D(kTRUE);
167     
168     // set autoload by default
169     AliEveEventManager::GetMaster()->SetAutoLoad(true);
170 }
171
172 void alieve_online_on_new_event()
173 {
174     if (AliEveEventManager::HasESD())
175       {
176         Double_t x[3] = { 0, 0, 0 };
177
178         AliESDEvent* esd = AliEveEventManager::AssertESD();
179         esd->GetPrimaryVertex()->GetXYZ(x);
180         
181         TTimeStamp ts(esd->GetTimeStamp());
182         TString win_title("Eve Main Window -- Timestamp: ");
183         win_title += ts.AsString("s");
184         win_title += "; Event # in ESD file: ";
185         win_title += esd->GetEventNumberInFile();
186         gEve->GetBrowser()->SetWindowName(win_title);
187     
188     
189         TEveElement* top = gEve->GetCurrentEvent();
190     
191         AliEveMultiView *mv = AliEveMultiView::Instance();
192     
193         mv->DestroyEventRPhi();
194         if (gCenterProjectionsAtPrimaryVertex){
195           mv->SetCenterRPhi(x[0], x[1], x[2]);
196         }
197         mv->ImportEventRPhi(top);
198     
199         mv->DestroyEventRhoZ();
200         if (gCenterProjectionsAtPrimaryVertex){
201           mv->SetCenterRhoZ(x[0], x[1], x[2]);
202         }
203         mv->ImportEventRhoZ(top);
204     
205         if (gCenterProjectionsAtPrimaryVertex)
206           mv->SetCenterMuon(x[0], x[1], x[2]);
207         mv->ImportEventMuon(top);
208     
209         
210         // Register image to amore.
211           TString id;      id.Form("online-viz-%03d", g_pic_id);
212           TString pic(id); pic += ".png";
213      
214           //printf("In image dump: file='%s'.\n", pic.Data());
215      
216           gEve->GetBrowser()->RaiseWindow();
217           gEve->FullRedraw3D();
218           gSystem->ProcessEvents();
219  
220           // create screenshots from OpenGL views
221           saveViews(pic.Data());
222      
223           // send screenshot to AMORE
224           int status = gSystem->Exec(Form("SendImageToAmore %s %s %d",id.Data(),pic.Data(),esd->GetRunNumber()));
225             printf("Post AMORE reg -- status=%d, run=%d.\n", status, esd->GetRunNumber());
226      
227           if (++g_pic_id >= g_pic_max) g_pic_id = 0;
228           g_pic_prev.Set();
229       }
230 }
231
232 void alieve_init_import_macros()
233 {
234     // Put macros in the list of browsables, add a macro browser to
235     // top-level GUI.
236     
237   TString macdir("$(ALICE_ROOT)/EVE/alice-macros");
238
239     if (gSystem->Getenv("ALICE_ROOT") != 0)
240     {
241         gInterpreter->AddIncludePath(Form("%s/MUON", gSystem->Getenv("ALICE_ROOT")));
242         gInterpreter->AddIncludePath(Form("%s/MUON/mapping", gSystem->Getenv("ALICE_ROOT")));
243         gSystem->ExpandPathName(macdir);
244     }
245     
246     
247     TFolder* f = gEve->GetMacroFolder();
248     void* dirhandle = gSystem->OpenDirectory(macdir.Data());
249     if (dirhandle != 0)
250     {
251         char* filename;
252         TPMERegexp re("\\.C$");
253         TObjArray names;
254         while ((filename = (char*)(gSystem->GetDirEntry(dirhandle))) != 0)
255         {
256             if (re.Match(filename))
257                 names.AddLast(new TObjString(filename));
258         }
259         names.Sort();
260         
261         for (Int_t ii=0; ii<names.GetEntries(); ++ii)
262         {
263             TObjString * si = (TObjString*) names.At(ii);
264             f->Add(new TEveMacro(Form("%s/%s", macdir.Data(), (si->GetString()).Data())));
265         }
266     }
267     gSystem->FreeDirectory(dirhandle);
268     
269     gROOT->GetListOfBrowsables()->Add(new TSystemDirectory(macdir.Data(), macdir.Data()));
270     
271     {
272         TEveBrowser   *br = gEve->GetBrowser();
273         TGFileBrowser *fb = 0;
274         fb = br->GetFileBrowser();
275         fb->GotoDir(macdir);
276         {
277             br->StartEmbedding(0);
278             fb = br->MakeFileBrowser();
279             fb->BrowseObj(f);
280             fb->Show();
281             br->StopEmbedding();
282             br->SetTabTitle("Macros", 0);
283             br->SetTab(0, 0);
284         }
285     }
286 }