OutputConvention.cxx
1 #include "OutputConvention.h"
2 
3 
4 
5 
6 
7 //---------------------------------------------------------------------------------------------------------
14 Acclaim::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  TString theArgv0 = TString::Format("%s", argv[0]);
44  TObjArray* tkns = theArgv0.Tokenize("/");
45  if(tkns->GetSize() > 0){
46  TObjString* lastPartOfName = (TObjString*) tkns->At(tkns->GetLast());
47  theArgv0 = lastPartOfName->GetString();
48  }
49 
50  // outFileName += theArgv0 + "Plots";
51  outFileName += theArgv0;
52 
53  // Excutable args
54  if(argc > 1){
55  for(int argInd=1; argInd < argc; argInd++){
56  outFileName += TString::Format("_%s", argv[argInd]);
57  }
58  }
59 
60  // Date and time of running executable
61  outFileName += getDateTimeSuffix();
62 
63  if(ext==""){
64  // ROOT suffix
65  outFileName += ".root";
66  }
67  else{
68  if(strncmp(ext.Data(), ".", 1)!=0){
69  ext = TString::Format(".%s", ext.Data());
70  }
71  outFileName += ext;
72  }
73  }
74 
75 
76  return outFileName;
77 
78 
79 }
80 
81 
82 
90  TString outFileName = getOutputFileName();
91  TFile* outFile = new TFile(outFileName, "recreate");
92  if(outFile->IsZombie()){
93  std::cerr << "Error! Unable to open output file " << outFileName.Data() << std::endl;
94  outFile = NULL;
95  }
96  return outFile;
97 }
98 
99 
100 
101 
102 
103 //---------------------------------------------------------------------------------------------------------
109 TString Acclaim::OutputConvention::getDateTimeSuffix(){
110  if(dateTimeSuffix==""){
111  // dateTimeSuffix = TString::Format("_%d-%d-%d_%d-%d-%d",
112  // dateTime.GetYear(),
113  // dateTime.GetMonth(),
114  // dateTime.GetDay(),
115  // dateTime.GetHour(),
116  // dateTime.GetMinute(),
117  // dateTime.GetSecond()
118  // );
119  dateTimeSuffix = TString::Format("_%d", dateTime.GetYear());
120 
121  Int_t m = dateTime.GetMonth();
122  if(m < 10){
123  dateTimeSuffix += TString::Format("-0%d", m);
124  }
125  else{
126  dateTimeSuffix += TString::Format("-%d", m);
127  }
128 
129  Int_t d = dateTime.GetDay();
130  if(d < 10){
131  dateTimeSuffix += TString::Format("-0%d", d);
132  }
133  else{
134  dateTimeSuffix += TString::Format("-%d", d);
135  }
136 
137  Int_t h = dateTime.GetHour();
138  if(h < 10){
139  dateTimeSuffix += TString::Format("_0%d", h);
140  }
141  else{
142  dateTimeSuffix += TString::Format("_%d", h);
143  }
144 
145  Int_t m2 = dateTime.GetMinute();
146  if(m2 < 10){
147  dateTimeSuffix += TString::Format("-0%d", m2);
148  }
149  else{
150  dateTimeSuffix += TString::Format("-%d", m2);
151  }
152 
153  Int_t s = dateTime.GetSecond();
154  if(s < 10){
155  dateTimeSuffix += TString::Format("-0%d", s);
156  }
157  else{
158  dateTimeSuffix += TString::Format("-%d", s);
159  }
160  }
161  return dateTimeSuffix;
162 }
163 
164 
165 
166 
167 
168 //---------------------------------------------------------------------------------------------------------
175 
176  outputDir = "";
177  const char* outputDirPoss = getenv("OUTPUT_DIR");
178  if(outputDirPoss!=NULL){
179  outputDir += TString::Format("%s/", outputDirPoss); // Add trailing forward slash...
180  }
181 
182  return outputDir;
183 }
184 
185 
186 
187 
188 //---------------------------------------------------------------------------------------------------------
198 TFile* Acclaim::OutputConvention::getFile(TString fileNameWithWildcards){
199  // This might be my favourite little bit of stand alone code.
200 
201  TFile* theFile = NULL;
202 
203  TChain* tempChain = new TChain("tempChain");
204  tempChain->Add(fileNameWithWildcards);
205 
206  TObjArray* fileList = tempChain->GetListOfFiles();
207 
208  const int numFiles = fileList->GetEntries();
209  if(numFiles > 0){
210  // std::cerr << "numFiles = " << numFiles << std::endl;
211  std::vector<TString> fileNames(numFiles, "");;
212 
213  for(int fileInd=0; fileInd < numFiles; fileInd++){
214  fileNames.at(fileInd) = TString::Format("%s", fileList->At(fileInd)->GetTitle());
215  }
216  std::sort(fileNames.begin(), fileNames.end(), std::greater<TString>());
217  for(int fileInd=0; fileInd < numFiles; fileInd++){
218  // std::cerr << fileInd << "\t" << fileNames.at(fileInd) << std::endl;
219  }
220  theFile = TFile::Open(fileNames.at(0));
221  }
222  delete tempChain;
223 
224  return theFile;
225 
226 }
OutputConvention(int argcIn, char *argvIn[])
Constructor.
static TFile * getFile(TString fileNameWithWildcards)
Opens matching file with the most recent suffix.
TFile * makeFile()
Create the new output file with proper name.
TString getOutputFileName(TString ext="")
Get the name of the output file from the program name (and system time).
TString getOutputDir()
Looks for an environment variable called OUTPUT_DIR and if it exists, sets it as the output dir...