OutputConvention.cxx
1 #include "OutputConvention.h"
2 
3 
4 
5 
6 
7 //---------------------------------------------------------------------------------------------------------
14 OutputConvention::OutputConvention(int argcIn, char* argvIn[]){
15  dateTime = TDatime();
16  argc = argcIn;
17  argv = argvIn;
18  outFileName = "";
19  dateTimeSuffix = "";
20  outputDir = "";
21  // subDir = "";
22 }
23 
24 
25 
26 
27 
28 //---------------------------------------------------------------------------------------------------------
36 
37  if(outFileName==""){
38 
39  // Directory
40  outFileName = getOutputDir();
41 
42  // Excutable name
43  outFileName += TString::Format("%sPlots", argv[0]);
44 
45  // Excutable args
46  if(argc > 1){
47  for(int argInd=1; argInd < argc; argInd++){
48  outFileName += TString::Format("_%s", argv[argInd]);
49  }
50  }
51 
52  // Date and time of running executable
53  outFileName += getDateTimeSuffix();
54 
55  if(ext==""){
56  // ROOT suffix
57  outFileName += ".root";
58  }
59  else{
60  if(strncmp(ext.Data(), ".", 1)!=0){
61  ext = TString::Format(".%s", ext.Data());
62  }
63  outFileName += ext;
64  }
65  }
66 
67 
68  return outFileName;
69 
70 
71 }
72 
73 
74 
75 
76 //---------------------------------------------------------------------------------------------------------
82 TString OutputConvention::getDateTimeSuffix(){
83  if(dateTimeSuffix==""){
84  // dateTimeSuffix = TString::Format("_%d-%d-%d_%d-%d-%d",
85  // dateTime.GetYear(),
86  // dateTime.GetMonth(),
87  // dateTime.GetDay(),
88  // dateTime.GetHour(),
89  // dateTime.GetMinute(),
90  // dateTime.GetSecond()
91  // );
92  dateTimeSuffix = TString::Format("_%d", dateTime.GetYear());
93 
94  Int_t m = dateTime.GetMonth();
95  if(m < 10){
96  dateTimeSuffix += TString::Format("-0%d", m);
97  }
98  else{
99  dateTimeSuffix += TString::Format("-%d", m);
100  }
101 
102  Int_t d = dateTime.GetDay();
103  if(d < 10){
104  dateTimeSuffix += TString::Format("-0%d", d);
105  }
106  else{
107  dateTimeSuffix += TString::Format("-%d", d);
108  }
109 
110  Int_t h = dateTime.GetHour();
111  if(h < 10){
112  dateTimeSuffix += TString::Format("_0%d", h);
113  }
114  else{
115  dateTimeSuffix += TString::Format("_%d", h);
116  }
117 
118  Int_t m2 = dateTime.GetMinute();
119  if(m2 < 10){
120  dateTimeSuffix += TString::Format("-0%d", m2);
121  }
122  else{
123  dateTimeSuffix += TString::Format("-%d", m2);
124  }
125 
126  Int_t s = dateTime.GetSecond();
127  if(s < 10){
128  dateTimeSuffix += TString::Format("-0%d", s);
129  }
130  else{
131  dateTimeSuffix += TString::Format("-%d", s);
132  }
133  }
134  return dateTimeSuffix;
135 }
136 
137 
138 
139 
140 
141 //---------------------------------------------------------------------------------------------------------
147 TString OutputConvention::getOutputDir(){
148 
149  outputDir = "";
150  const char* outputDirPoss = getenv("OUTPUT_DIR");
151  if(outputDirPoss!=NULL){
152  outputDir += TString::Format("%s/", outputDirPoss); // Add trailing forward slash...
153  }
154 
155  return outputDir;
156 }
157 
158 
159 
160 
161 //---------------------------------------------------------------------------------------------------------
171 TFile* OutputConvention::getFile(TString fileNameWithWildcards){
172  // This might be my favourite little bit of stand alone code.
173 
174  TFile* theFile = NULL;
175 
176  TChain* tempChain = new TChain("tempChain");
177  tempChain->Add(fileNameWithWildcards);
178 
179  TObjArray* fileList = tempChain->GetListOfFiles();
180 
181  const int numFiles = fileList->GetEntries();
182  if(numFiles > 0){
183  // std::cerr << "numFiles = " << numFiles << std::endl;
184  std::vector<TString> fileNames(numFiles, "");;
185 
186  for(int fileInd=0; fileInd < numFiles; fileInd++){
187  fileNames.at(fileInd) = TString::Format("%s", fileList->At(fileInd)->GetTitle());
188  }
189  std::sort(fileNames.begin(), fileNames.end(), std::greater<TString>());
190  for(int fileInd=0; fileInd < numFiles; fileInd++){
191  // std::cerr << fileInd << "\t" << fileNames.at(fileInd) << std::endl;
192  }
193  theFile = TFile::Open(fileNames.at(0));
194  }
195  delete tempChain;
196 
197  return theFile;
198 
199 }
200 
TString getOutputFileName(TString ext="")
Get the name of the output file from the program name (and system time).
OutputConvention(int argcIn, char *argvIn[])
Constructor.
static TFile * getFile(TString fileNameWithWildcards)
Opens a TFile matching a fileName with wildcards. If multiple matches gets the "greatest" TString...