Add macro to save all OpenGL views in a cool and modern look. Used in Online Event...
[u/mrichter/AliRoot.git] / EVE / macros / saveViews.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  // Author: Mihai Niculescu 2013
7  
8  
9  /*
10  * This script creates a single picture containing all OpenGL views from a running AliEve
11  *
12  * Considering a given size (width, height) for the final picture, the size for all OpenGL 
13  * views are computed using the same aspect ratio (width/height) as the main 3D View
14  */
15
16 #include <TASImage.h>
17 #include <TGLViewer.h>
18 #include <TEveViewer.h>
19 #include <TEveManager.h>
20 #include <TMath.h>
21 #include <TSystem.h>
22 #include <TTimeStamp.h>
23
24 #include <STEER/ESD/AliESDEvent.h>
25 #include <STEER/ESD/AliESDRun.h>
26 #include <STEER/STEER/AliGRPObject.h>
27 #include <STEER/CDB/AliCDBEntry.h>
28 #include <STEER/CDB/AliCDBManager.h>
29 #include <RAW/AliRawReader.h>
30 #include <RAW/AliRawEventHeaderBase.h>
31 #include <EVE/EveBase/AliEveEventManager.h>
32
33 TString getEventInfo();
34
35 /* composite picture final size
36         compositeImgFileName - save final image to this file    
37         width - of the final image
38         height -of the final image
39 */
40 void saveViews(const char* compositeImgFileName="views.png", Int_t width = 1440, Int_t height= 900)
41 {
42         Info("saveViews.C", "saving views to [%s]", compositeImgFileName);
43
44         Int_t heightInfoBar = 65; // hold height of the Information bar
45         
46         TASImage* compositeImg=0; // this holds the final image
47         TASImage* tempImg=0; // temporary used for loading images
48         
49         TEveViewerList* viewers = gEve->GetViewers();
50         Int_t Nviewers = viewers->NumChildren()-2; // remark: 3D view is counted twice
51                 
52         compositeImg = new TASImage(width, height);
53                 
54         // 3D View size 
55         Int_t width3DView = TMath::FloorNint((float)Nviewers*width/(float)(Nviewers+1)); // the width of the 3D view
56         Int_t height3DView= height-heightInfoBar; // the height of the 3D view
57         Float_t aspectRatio = (float)width3DView/(float)height3DView; // 3D View aspect ratio
58                 
59         // Children View Size
60         Int_t heightChildView = TMath::FloorNint((float)height3DView/(float)Nviewers);
61         Int_t widthChildView  = TMath::FloorNint(aspectRatio*heightChildView); // has the same aspect ratio as the 3D view
62         
63         int index=0; // iteration counter
64         int x = width3DView; // x position of the child view
65         int y = 0;// y position of the child view
66         TString viewFilename; // save view to this file
67         for(TEveElement::List_i i = (++viewers->BeginChildren()); i != viewers->EndChildren(); i++){ // NB: this skips the first children (first 3D View)
68                 TEveViewer* view = ((TEveViewer*)*i);
69                 viewFilename = Form("view-%d.png", index);
70
71     // Save OpenGL views in files
72     if(index==0){
73                         view->GetGLViewer()->SavePictureUsingFBO(viewFilename, width3DView, height3DView);
74                 }
75                 else {
76                         view->GetGLViewer()->SavePictureUsingFBO(viewFilename, widthChildView, heightChildView);
77                 }
78                 
79                 tempImg = new TASImage(viewFilename);
80                 
81                 // copy view image in the composite image
82                 if(index==0){
83                         tempImg->CopyArea(compositeImg, 0,0, width3DView, height3DView);
84                 }
85                 else {
86                         tempImg->CopyArea(compositeImg, 0,0, widthChildView, heightChildView, x,y);
87                     
88     // draw a border around child views
89     compositeImg->DrawRectangle(x,y, widthChildView, heightChildView);
90     //compositeImg->FillRectangle("#CFEDF0", x+1,y+1, widthChildView-1, 40);
91     //compositeImg->DrawText(x+5,y+10, view->GetName(), 28, "#000000", "verdana.ttf", TImage::kOutlineBelow);
92     
93     }
94     
95     /*
96      final touches inside loop
97     */
98     delete tempImg;
99     if(index>0) // skip 3D View
100         y+=heightChildView;
101         
102     index++;
103    }
104
105  
106  // show LIVE bar
107         TTimeStamp ts;
108         TString tNow = ts.AsString("s"); // display date & time
109  
110         compositeImg->Gradient( 90, "#EAEAEA #D2D2D2 #FFFFFF", 0, 30, 0, 234, 75);
111         compositeImg->Gradient( 90, "#D6D6D6 #242424 #000000", 0, 35, 40, 224, 30);
112         compositeImg->BeginPaint();
113         compositeImg->DrawRectangle(30,0, 234, 75);
114         compositeImg->DrawText(35, 4, "ALICE", 40, "#000000", "FreeSans.otf");
115         compositeImg->DrawText(162, 6, "LIVE", 40, "#FF2D00", "FreeSansBold.otf");
116         compositeImg->DrawText(59, 48, tNow, 20, "#FFFFFF", "arial.ttf");
117         compositeImg->EndPaint();
118         
119         // show Information bar
120         TString stringInfo;
121         stringInfo = getEventInfo();
122         compositeImg->Gradient( 90, "#1B58BF #1D5CDF #0194FF", 0, 0, height-heightInfoBar, width, heightInfoBar);
123         compositeImg->BeginPaint();
124         compositeImg->DrawText(10, height-heightInfoBar+15, stringInfo, 28, "#FFFFFF", "FreeSansBold.otf");
125         compositeImg->EndPaint();
126                 
127         
128         // write composite image to disk
129         compositeImg->CopyArea(compositeImg, 0,0, width, height);
130         compositeImg->WriteImage(compositeImgFileName);
131         
132         delete compositeImg;
133         
134         return;
135 }
136
137 TString getEventInfo()
138 {
139         TString rawInfo, esdInfo;
140
141   if (!AliEveEventManager::HasRawReader())
142   {
143     rawInfo = "";
144   }
145   else
146   {
147         AliRawReader* rawReader = AliEveEventManager::AssertRawReader();
148                 if(!rawReader) return "";
149                 rawInfo.Form("Run: %d  Event#: %d (%s)",
150                 rawReader->GetRunNumber(),
151                 AliEveEventManager::CurrentEventId(),
152                 AliRawEventHeaderBase::GetTypeName(rawReader->GetType())
153                 );
154                 
155          return rawInfo;
156   }
157
158   if (!AliEveEventManager::HasESD())
159   {
160     esdInfo = "";
161   }
162   else
163   {
164                 AliESDEvent* esd =  AliEveEventManager::AssertESD();
165
166                 esdInfo.Form("Colliding: %s Run: %d  Event: %d (%s)",
167                 esd->GetESDRun()->GetBeamType(),
168                 esd->GetRunNumber(),
169                 AliEveEventManager::CurrentEventId(),
170                 AliRawEventHeaderBase::GetTypeName(esd->GetEventType())
171                 );
172   }
173
174   return esdInfo;
175 }