EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairRunInfo.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairRunInfo.cxx
1 #include "FairRunInfo.h"
2 #include "FairLogger.h"
3 
4 #include "TSystem.h"
5 #include "TFile.h"
6 #include "TList.h"
7 #include "TH1.h"
8 #include "TDirectory.h"
9 
10 #include <iostream>
11 #include <algorithm>
12 
14 
15 //_____________________________________________________________________________
17  :TObject(),
18  fTimeStamp(),
19  fCpuInfo(),
20  fMemInfo(),
21  fProcInfo(),
22  fTimeDiff(),
23  fTime(),
24  fResidentMemory(),
25  fVirtualMemory(),
26  fLogger(FairLogger::GetLogger())
27 {
28 }
29 //_____________________________________________________________________________
31 {
32 }
33 //_____________________________________________________________________________
35 {
36  // Extract the Information about the used memory from the system.
37  // Stores this info together with the timedifference between now
38  // and the last call in container arrays for later usage.
39  // gSystem->GetCpuInfo(&cpuInfo, 10);
40  // gSystem->GetMemInfo(&memInfo);
41 
42  GetInfo();
43 
45 
46  PrintInfo();
47 }
48 //_____________________________________________________________________________
50 {
51  // Set the TimeStamp to the actual time and store it
52  fTimeStamp.Set();
53  // fTime.push_back(fTimeStamp.GetSec());
54  fTime.push_back(fTimeStamp.AsDouble());
55 
56  gSystem->GetProcInfo(&fProcInfo);
57  fResidentMemory.push_back(fProcInfo.fMemResident/1024);
58  fVirtualMemory.push_back(fProcInfo.fMemVirtual/1024);
59 }
60 //_____________________________________________________________________________
62 {
63  // Calculates the time difference between now and the last call
64 
65  Int_t lastElement = fTime.size()-1;
66  fTimeDiff.push_back( fTime.at(lastElement) -
67  fTime.at(lastElement-1) );
68 }
69 //_____________________________________________________________________________
71 {
72 
73  fLogger->Debug(MESSAGE_ORIGIN,"Time to execute 1 event: %f s",
74  fTimeDiff.back());
75  fLogger->Debug(MESSAGE_ORIGIN,"Used resident memory: %i MB",
76  fResidentMemory.back());
77  fLogger->Debug(MESSAGE_ORIGIN,"Used virtual memory: %i MB",
78  fVirtualMemory.back());
79 }
80 //_____________________________________________________________________________
82 {
83  TList* histoList = new TList();
84  CreateAndFillHistograms(histoList);
85  // CreateAndFillHistograms(histoList);
86  WriteHistosToFile(histoList);
87 }
88 //_____________________________________________________________________________
90 {
91  Int_t entries = fTime.size();
92  Double_t timePeriod = fTime.back()-fTime.front();
93 
94  TH1F* ResidentMemoryVsEvent = new TH1F("ResidentMemoryVsEvent","Resident Memory as function of Eventnumber;Event;Memory [MB]",entries, 0, entries);
95  TH1F* VirtualMemoryVsEvent = new TH1F("VirtualMemoryVsEvent","Virtual Memory as function of Eventnumber;Event;Memory [MB]",entries, 0, entries);
96  TH1F* ResidentMemoryVsTime = new TH1F("ResidentMemoryVsTime","Resident memory as function of Runtime;Time [s];Memory [MB]",(Int_t)(timePeriod*10), 0, (Int_t)timePeriod);
97  TH1F* VirtualMemoryVsTime = new TH1F("VirtualMemoryVsTime","Virtual memory as function of Runtime;Time [s];Memory [MB]",(Int_t)(timePeriod*10), 0, (Int_t)timePeriod);
98  TH1F* EventtimeVsEvent = new TH1F("EventtimeVsEvent","Runtime per Event as function of Event number;Event;Time [s]",entries-1, 1, entries);
99 
100  std::vector<Double_t> timeDiffSorted(fTimeDiff);
101  std::vector<Double_t>::iterator it;
102  std::sort(timeDiffSorted.begin(), timeDiffSorted.end());
103 
104  Double_t minTime = timeDiffSorted.front();
105  Double_t maxTime = timeDiffSorted.back();
106  timePeriod = maxTime - minTime;
107 
108  TH1F* TimePerEvent = new TH1F("TimePerEvent","Runtime;Time [s];Events",(Int_t)((timePeriod+20)*10), (Int_t)minTime-10, (Int_t)maxTime+10);
109 
110  Int_t counter = 0;
111  std::vector<Long_t>::iterator lit;
112  Double_t timeOffset = fTime.front()+1.;
113  for(lit=fResidentMemory.begin(); lit<fResidentMemory.end(); lit++) {
114  ResidentMemoryVsEvent->Fill(counter, *lit);
115  ResidentMemoryVsTime->Fill(fTime.at(counter)-timeOffset, *lit);
116  counter++;
117  }
118  histoList->AddLast(ResidentMemoryVsEvent);
119  histoList->AddLast(ResidentMemoryVsTime);
120 
121  counter = 0;
122  for(lit=fVirtualMemory.begin(); lit<fVirtualMemory.end(); lit++) {
123  VirtualMemoryVsEvent->Fill(counter, *lit);
124  VirtualMemoryVsTime->Fill(fTime.at(counter)-timeOffset, *lit);
125  counter++;
126  }
127  histoList->AddLast(VirtualMemoryVsEvent);
128  histoList->AddLast(VirtualMemoryVsTime);
129 
130  counter = 1;
131  for(it=fTimeDiff.begin(); it<fTimeDiff.end(); it++) {
132  TimePerEvent->Fill(*it);
133  EventtimeVsEvent->Fill(counter, *it);
134  counter++;
135  }
136  histoList->AddLast(TimePerEvent);
137  histoList->AddLast(EventtimeVsEvent);
138 }
139 //_____________________________________________________________________________
140 
141 #include <FairRun.h>
142 
143 void FairRunInfo::WriteHistosToFile(TList* histoList)
144 {
145  // If the file size is larger then approx. 2GB then the histos
146  // can't be read any longer. Because of this problem the histos
147  // are written to a separate file instead
148 
149 #if _OLD_
150  TFile* oldfile = gFile;
151 
152  TString directory = gFile->GetName();
153  fLogger->Debug(MESSAGE_ORIGIN,"Name %s:",
154  gFile->GetName());
155  Ssiz_t posLastSlash = directory.Last('/');
156  directory.Remove(posLastSlash+1, directory.Length()-posLastSlash-1);
157  TString filename = "";
158  if ( directory.EndsWith("/") ) {
159  filename += directory;
160  }
161 
162 
163  directory = gFile->GetName();
164  fLogger->Debug(MESSAGE_ORIGIN,"Name: %s",
165  directory.Data());
166  posLastSlash = directory.Last('/');
167  directory.Remove(0, posLastSlash+1);
168  directory.ReplaceAll(".root","");
169  fLogger->Debug(MESSAGE_ORIGIN,"Name: %s",
170  directory.Data());
171 
172 
173 
174  Int_t pid = gSystem->GetPid();
175  filename += "FairRunInfo_";
176  filename += directory;
177  filename += ".root";
178  fLogger->Debug(MESSAGE_ORIGIN,"FileName: %s",
179  filename.Data());
180 
181  TFile* f1 = new TFile(filename, "recreate");
182  f1->cd();
183 
184  TIterator* listIter=histoList->MakeIterator();
185  listIter->Reset();
186  TObject* obj = NULL;
187  while((obj=listIter->Next())) {
188  obj->Write();
189  }
190 
191  delete listIter;
192  histoList->Delete();
193  delete histoList;
194 
195  f1->Close();
196  f1->Delete();
197  gFile=oldfile;
198 #else
199  FairRun *fRun = FairRun::Instance();
200 
201  // I hope there is no need to save/restore current directory here?;
202  fRun->GetOutputFile()->cd();
203 
204  TIterator* listIter=histoList->MakeIterator();
205  listIter->Reset();
206  TObject* obj = NULL;
207  while((obj=listIter->Next())) {
208  obj->Write();
209  }
210 
211  delete listIter;
212  histoList->Delete();
213  delete histoList;
214 #endif
215 
216  /*
217  // create a new subdirectory in the output file and
218  // write all histograms
219  TDirectory* currentDir = gDirectory;
220 
221  gDirectory->cd();
222  gDirectory->mkdir("FairRunInfo");
223  gDirectory->cd("FairRunInfo");
224 
225  TIterator* listIter=histoList->MakeIterator();
226  listIter->Reset();
227  TObject* obj = NULL;
228  while((obj=listIter->Next())) {
229  obj->Write();
230  }
231 
232  gDirectory=currentDir;
233  */
234 }
235 //_____________________________________________________________________________
236 
238 {
239  GetInfo();
240 }
241