LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
plotG.cc File Reference
#include "CanvasInTab.hh"
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <iomanip>
#include <locale>
#include <cstring>
#include <TApplication.h>
#include <TGApplication.h>
#include <TTree.h>
#include <TNtuple.h>
#include <TBranch.h>
#include <TProfile.h>
#include <TGraph.h>
#include <TAxis.h>
#include <TGraphErrors.h>
#include <TROOT.h>
#include <TCanvas.h>
#include <TFile.h>
#include <TGFileBrowser.h>
#include <TGFileDialog.h>
#include <TChain.h>
#include <TColor.h>

Go to the source code of this file.

Classes

struct  SpeciesInfoAOS
 
struct  SpeciesInfoSOA
 

Macros

#define USE_CANVASINTAB
 
#define _PROCESS_ONE_FILE_   ProcessSingleFile
 

Functions

const TGFileInfo * OpenRootFile ()
 
void ProcessSingleFile (TFile *file)
 
int ProcessSingleFile (const char *filePath)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

#define _PROCESS_ONE_FILE_   ProcessSingleFile

Definition at line 338 of file plotG.cc.

Referenced by main().

#define USE_CANVASINTAB

Definition at line 26 of file plotG.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 341 of file plotG.cc.

References _PROCESS_ONE_FILE_, and OpenRootFile().

342 {
343  //--------------------------------
344  int initialArgc = argc;
345  vector<char*> initialArgv(argc);
346  for(int i = 0 ; i < argc ; ++i)
347  {
348  initialArgv[i] = argv[i];
349  }
350  //--------------------------------
351 
352  TApplication* rootApp = new TApplication("PlotG",&argc, argv);
353 
354  const char* filePath = 0;
355 
356  if(initialArgc == 1) // no file provided in argument
357  {
358  const TGFileInfo* fileInfo = OpenRootFile();
359  filePath = fileInfo->fFilename;
360  if(fileInfo->fFileNamesList && fileInfo->fFileNamesList->GetSize()>1)
361  {
362  // several files selected
363  // user has to tick "Multiple selection"
364  perror("Multiple selection of files not supported, implement your own!");
365  //
366  // For instance, start from:
367  // TChain* tree = new TChain("species");
368  // tree->AddFileInfoList(fileInfo->fFileNamesList);
369  // Or call ProcessSingleFile for each file,
370  // you'll need to do some adaptation
371  }
372  else
373  {
374  if(_PROCESS_ONE_FILE_(filePath)) return 1;
375  }
376  }
377  else // a file is provided in argument
378  {
379  filePath = initialArgv[1];
380  if(_PROCESS_ONE_FILE_(filePath)) return 1;
381  }
382 
383  rootApp->Run();
384  delete rootApp;
385  return 0;
386 }
#define _PROCESS_ONE_FILE_
Definition: plotG.cc:338
const TGFileInfo * OpenRootFile()
Definition: plotG.cc:64
const TGFileInfo* OpenRootFile ( )

Definition at line 64 of file plotG.cc.

Referenced by main().

65 {
66  const char *gOpenAsTypes[] = {
67  "ROOT files", "*.root",
68  "All files", "*"
69  };
70 
71  static TGFileInfo fi;
72  fi.fFileTypes = gOpenAsTypes;
73  // fi.SetMultipleSelection(kTRUE);
74  // User must check the box "multiple selection" in the dialog box
75  // fi.fIniDir = StrDup(".");
76  new TGFileDialog(gClient->GetRoot(),
77  gClient->GetRoot(), kFDOpen, &fi);
78 
79  return &fi;
80 }
void ProcessSingleFile ( TFile *  file)

Definition at line 158 of file plotG.cc.

References color(), e, evd::details::end(), SpeciesInfoAOS::fG, SpeciesInfoSOA::fG, SpeciesInfoAOS::fG2, SpeciesInfoSOA::fGerr, SpeciesInfoAOS::fName, SpeciesInfoSOA::fName, SpeciesInfoAOS::fNEvent, SpeciesInfoAOS::fNumber, SpeciesInfoSOA::fRelatErr, SpeciesInfoSOA::fTime, nentries, and util::flags::to_string().

Referenced by ProcessSingleFile().

159 {
160  int speciesID;
161  int number;
162  int nEvent;
163  char speciesName[500];
164  double time; // time
165  double sumG; // sum of G over all events
166  double sumG2; // sum of G^2 over all events
167 
168  TTree* tree = (TTree*)file->Get("species");
169  tree->SetBranchAddress("speciesID", &speciesID);
170  tree->SetBranchAddress("number", &number);
171  tree->SetBranchAddress("nEvent", &nEvent);
172  tree->SetBranchAddress("speciesName", &speciesName);
173  tree->SetBranchAddress("time", &time);
174  tree->SetBranchAddress("sumG", &sumG);
175  tree->SetBranchAddress("sumG2", &sumG2);
176 
177  Long64_t nentries = tree->GetEntries();
178  // cout << nentries <<" entries" << endl;
179 
180  if(nentries == 0)
181  {
182  cout << "No entries found in the tree species contained in the file "
183  << file->GetPath() << endl;
184  exit(1);
185  }
186 
187  //----------------------------------------------------------------------------
188  // This first loop is used in case the processed ROOT file is issued from the
189  // accumulation of several ROOT files (e.g. hadd)
190 
191  std::map<int, std::map<double, SpeciesInfoAOS>> speciesTimeInfo;
192 
193  for (int j=0; j < nentries; j++)
194  {
195  tree->GetEntry(j);
196 
197  SpeciesInfoAOS& infoAOS = speciesTimeInfo[speciesID][time];
198 
199  infoAOS.fNumber += number;
200  infoAOS.fG += sumG;
201  infoAOS.fG2 += sumG2;
202  infoAOS.fNEvent += nEvent;
203  infoAOS.fName = speciesName;
204  }
205 
206  //----------------------------------------------------------------------------
207 
208  std::map<int, SpeciesInfoSOA> speciesInfo;
209 
210  auto it_SOA = speciesTimeInfo.begin();
211  auto end_SOA = speciesTimeInfo.end();
212 
213  for (; it_SOA!=end_SOA ; ++it_SOA)
214  {
215  const int _speciesID = it_SOA->first;
216  SpeciesInfoSOA& info = speciesInfo[_speciesID];
217 
218  auto it2 = it_SOA->second.begin();
219  auto end2 = it_SOA->second.end();
220 
221  info.fName = it2->second.fName;
222  const size_t size2 = it_SOA->second.size();
223  info.fG.resize(size2);
224  info.fGerr.resize(size2);
225  info.fTime.resize(size2);
226 
227  for(int i2 = 0 ;it2!=end2;++it2, ++i2)
228  {
229  SpeciesInfoAOS& infoAOS = it2->second;
230 
231  double _SumG2 = infoAOS.fG2;
232  double _MeanG = infoAOS.fG/infoAOS.fNEvent;
233  double _Gerr = sqrt((_SumG2/infoAOS.fNEvent - pow(_MeanG,2))
234  /(infoAOS.fNEvent-1) );
235 
236  info.fG[i2] = _MeanG;
237  info.fGerr[i2] = _Gerr;
238  info.fTime[i2] = it2->first;
239  info.fRelatErr += _Gerr/(_MeanG + 1e-30); // add an epsilon to prevent NAN
240  }
241  }
242 
243  //----------------------------------------------------------------------------
244 
245 #ifdef USE_CANVASINTAB
246  CanvasInTab* myFrame =
247  new CanvasInTab(gClient->GetRoot(), 500, 500);
248 #endif
249 
250  std::map<int, SpeciesInfoSOA>::iterator it = speciesInfo.begin();
251  std::map<int, SpeciesInfoSOA>::iterator end = speciesInfo.end();
252 
253  for (; it != end; ++it)
254  {
255  speciesID = it->first;
256  SpeciesInfoSOA& info = it->second;
257 // if(strstr(info.fName.c_str(), "H2O^") != 0) continue;
258 
259  if(info.fG.empty()) continue;
260 
261  TGraphErrors* gSpecies = new TGraphErrors(info.fG.size(),
262  info.fTime.data(),
263  info.fG.data(),
264  0,
265  info.fGerr.data());
266 
267 #ifdef USE_CANVASINTAB
268  int nCanvas = myFrame->AddCanvas(info.fName.c_str());
269  myFrame->GetCanvas(nCanvas);
270  TCanvas* cSpecies = myFrame->GetCanvas(nCanvas);
271 #else
272  TCanvas* cSpecies = new TCanvas(info.fName.c_str(),
273  info.fName.c_str());
274 #endif
275 
276  cSpecies->cd();
277  int color = (2+speciesID)%TColor::GetNumberOfColors();
278  if(color == 5 || color==10 || color==0) ++color;
279 
280  // cout << info.fName.c_str() << " " << color << endl;
281 
282  gSpecies->SetMarkerStyle(20+speciesID);
283  gSpecies->SetMarkerColor(color);
284  info.fRelatErr /= (double)info.fG.size();
285 
286  gSpecies->SetTitle((info.fName
287  + " - speciesID: "
288  + std::to_string(speciesID)+" rel. Err. "
289  + std::to_string(info.fRelatErr)).c_str() );
290  gSpecies->GetXaxis()->SetTitle("Time [ns]");
291  gSpecies->GetYaxis()->SetTitle("G [molecules/100 eV]");
292  gSpecies->Draw("ap");
293  cSpecies->SetLogx();
294  }
295 
296 #ifdef USE_CANVASINTAB
297  int nCanvas = myFrame->GetNCanvas();
298  for(int i = 0 ; i < nCanvas ; ++i)
299  {
300  myFrame->GetCanvas(i)->Update();
301  }
302 #endif
303 }
std::vector< double > fTime
Definition: plotG.cc:151
double fG2
Definition: plotG.cc:117
intermediate_table::iterator iterator
double fG
Definition: plotG.cc:116
string fName
Definition: plotG.cc:153
std::vector< double > fGerr
Definition: plotG.cc:150
Int_t nentries
Definition: comparison.C:29
std::size_t color(std::string const &procname)
std::string to_string(Flag_t< Storage > const flag)
Convert a flag into a stream (shows its index).
Definition: BitMask.h:187
std::vector< double > fG
Definition: plotG.cc:149
TFile * file
std::vector< evd::details::RawDigitInfo_t >::const_iterator end(RawDigitCacheDataClass const &cache)
Float_t e
Definition: plot.C:34
string fName
Definition: plotG.cc:118
double fRelatErr
Definition: plotG.cc:152
int ProcessSingleFile ( const char *  filePath)

Definition at line 307 of file plotG.cc.

References file, and ProcessSingleFile().

308 {
309  if(filePath == 0 || strlen(filePath) == 0)
310  {
311  perror("You must provide a valid file");
312  return 1;
313  }
314 
315  TFile* file = TFile::Open(filePath);
316 
317  if(file == 0)
318  {
319  perror ("Error opening ntuple file");
320  exit(1);
321  }
322 
323  if(!file-> IsOpen())
324  {
325  perror ("Error opening ntuple file");
326  exit(1);
327  }
328  else
329  {
330  cout << "Opening ntple file " << filePath << endl;
331  }
332  ProcessSingleFile(file);
333  return 0;
334 }
void ProcessSingleFile(TFile *file)
Definition: plotG.cc:158
TFile * file