HighLAND
VersionManager.cxx
1 #include "VersionManager.hxx"
2 #include "BasicTypes.hxx"
3 #include <stdio.h>
4 #include <iostream>
5 
6  /// The static member pointer to the singleton.
7 Versioning* Versioning::_versioning = NULL;
8 
9 ClassImp(PackageVersion)
10 
11 //***********************************************************
13 //***********************************************************
14 
15  _name = "";
16  _version = "";
17 }
18 
19 //***********************************************************
20 PackageVersion::PackageVersion(const std::string& name, const std::string& version){
21 //***********************************************************
22 
23  _name = name;
24  _version = version;
25 }
26 
27 //********************************************************************
28 Versioning& ND::versioning(){
29 //********************************************************************
30 
31  return Versioning::Get();
32 }
33 
34 //*****************************************************************************
36  //*****************************************************************************
37 
38  if (!_versioning) {
39  _versioning = new Versioning;
40  }
41 
42  return *_versioning;
43 }
44 
45 
46 //********************************************************************
47 Versioning::Versioning(): HLClonesArray("config","VERSION","PackageVersion",NMAXPACKAGES){
48 //********************************************************************
49 
50  _packages.clear();
51 }
52 
53 //********************************************************************
54 void Versioning::AddProduction(ProdId_h prodId, const std::string& prodName, const std::string& prodLowVersion, const std::string& prodHighVersion){
55 //********************************************************************
56 
57  _prodId.push_back(prodId);
58  _prodName.push_back(prodName);
59  _prodLowVersion.push_back(prodLowVersion);
60  _prodHighVersion.push_back(prodHighVersion);
61 }
62 
63 //********************************************************************
64 ProdId_h Versioning::GetProduction(const std::string softVersion){
65 //********************************************************************
66 
67  for (UInt_t i=0;i<_prodId.size();i++){
68  if( !CheckBeforeVersion(softVersion, _prodLowVersion[i]) && CheckBeforeVersion(softVersion, _prodHighVersion[i]))
69  return _prodId[i];
70  }
71 
72  return UNKNOWN_PRODUCTION;
73 }
74 
75 //********************************************************************
76 std::string Versioning::GetProductionName(const std::string softVersion){
77 //********************************************************************
78 
79  return ConvertProduction(GetProduction(softVersion));
80 }
81 
82 //********************************************************************
83 std::string Versioning::ConvertProduction(ProdId_h prod){
84 //********************************************************************
85 
86  for (UInt_t i=0;i<_prodId.size();i++){
87  if (prod == _prodId[i]) return _prodName[i];
88  }
89  return "unknown";
90 }
91 
92 //********************************************************************
93 bool Versioning::GetSoftwareVersionRangeForProduction(ProdId_h prodId, std::string& lowVersion, std::string& highVersion){
94 //********************************************************************
95 
96  for (UInt_t i=0;i<_prodId.size();i++){
97  if (_prodId[i] == prodId){
98  lowVersion = _prodLowVersion[i];
99  highVersion= _prodHighVersion[i];
100  return true;
101  }
102  }
103 
104  return false;
105 }
106 
107 
108 //********************************************************************
109 bool Versioning::CheckVersionCompatibility(const std::string& version, ProdId_h prodND280){
110 //********************************************************************
111 
112  bool ok = true;
113  std::string prodLow, prodHigh;
114 
115  // Get the version ranges for a given production
116  GetSoftwareVersionRangeForProduction(prodND280, prodLow, prodHigh);
117 
118  // Get the production name from the software version of the input file
119  ProdId_h prodInputFile = GetProduction(version);
120 
121 
122  std::cerr << "nd280AnalysisTools compiled with " << ConvertProduction(prodND280) << " (software version: " << prodLow << " - " << prodHigh << ") file. " << std::endl;
123  std::cerr << "Checking for compatibility version with current file (software version: " << version << ")" << std::endl;
124  if (prodInputFile != prodND280){
125  ok = false;
126  std::cerr << "*************************************************" << std::endl;
127  std::cerr << "*************************************************" << std::endl;
128  std::cerr << "WARNING: Running over a " << ConvertProduction(prodInputFile) << " file !!!!!" << std::endl;
129  std::cerr << "oaAnalysis data classes definition could be incompatible !!!!" << std::endl;
130  std::cerr << "You should compile nd280AnalysisTools with the proper file and then recompile highland2 from scratch !!!" << std::endl;
131  std::cerr << "You could also disable version check with command line option -v" << std::endl;
132  std::cerr << "*************************************************" << std::endl;
133  std::cerr << "*************************************************" << std::endl;
134  }
135  else
136  std::cout << "---> OK. Running over a " << ConvertProduction(prodND280) << " file" << std::endl;
137 
138  return ok;
139 
140 }
141 
142 //********************************************************************
143 bool Versioning::CheckBeforeVersion(const std::string& version, const std::string& version0){
144 //********************************************************************
145 
146  int v,r,p;
147  int v0,r0,p0;
148  ParseSoftwareVersion(version, v, r, p);
149  ParseSoftwareVersion(version0, v0,r0,p0);
150 
151  if (v<v0) return true;
152  else if (v==v0){
153  if (r<r0) return true;
154  else if (r==r0){
155  if (p<p0) return true;
156  }
157  }
158 
159  return false;
160 }
161 
162 
163 //********************************************************************
164 void Versioning::ParseSoftwareVersion(const std::string& version, int& v, int& r, int& p) {
165 //********************************************************************
166 
167  int vloc = version.find("v");
168  int rloc = version.find("r");
169  int ploc = version.find("p");
170  int size = version.length();
171 
172  if ((size_t)vloc == std::string::npos) {
173  std::cerr << "*************************************************" << std::endl;
174  std::cerr << "*************************************************" << std::endl;
175  std::cerr << "WARNING: SoftwareVersion '" << version << "' is not a valid format. This is probably and OLD highland FlatTree !" << std::endl;
176  std::cerr << "You could disable version check with command line option -v" << std::endl;
177  std::cerr << "*************************************************" << std::endl;
178  std::cerr << "*************************************************" << std::endl;
179  exit(3);
180  }
181 
182  if ((size_t)rloc == std::string::npos) {
183  // vX
184  v = std::atoi(version.substr(vloc+1, size - vloc-1).c_str());
185  r = 0;
186  p = 0;
187  } else if ((size_t)ploc == std::string::npos) {
188  // vXrY
189  v = std::atoi(version.substr(vloc+1, rloc - vloc-1).c_str());
190  r = std::atoi(version.substr(rloc+1, size - rloc-1).c_str());
191  p = 0;
192  } else {
193  // vXrYpZ
194  v = std::atoi(version.substr(vloc+1, rloc - vloc-1).c_str());
195  r = std::atoi(version.substr(rloc+1, ploc - rloc-1).c_str());
196  p = std::atoi(version.substr(ploc+1, size - ploc-1).c_str());
197  }
198 }
199 
200 //***********************************************************
201 void Versioning::AddPackage(const std::string& name, const std::string& version){
202 //***********************************************************
203 
204  for (UInt_t i=0;i<_packages.size();i++){
205  if (_packages[i]->Name() == name){
206  if (_packages[i]->Version() != version){
207  std::cerr << "*************************************************" << std::endl;
208  std::cerr << "*************************************************" << std::endl;
209  std::cerr << "ERROR: Package '" << name << "' has been added twice with two different versions "<< _packages[i]->Version() << " and " << version << " !!!!" << std::endl;
210  std::cerr << "*************************************************" << std::endl;
211  std::cerr << "*************************************************" << std::endl;
212  exit(3);
213  }
214  else return;
215  }
216  }
217 
218  PackageVersion* pack = new PackageVersion(name, version);
219  _packages.push_back(pack);
220  (*_objects)[_NObjects++] = pack;
221 }
222 
223 //********************************************************************
224 void Versioning::ReadVersions(const std::string& file){
225 //********************************************************************
226 
227  // Reset the vectors
228  _packages.clear();
229 
230  ReadClonesArray(file);
231 
232  // Add them to the map of categories
233  for (int i=0;i<_NObjects;i++){
234  PackageVersion* pack = (PackageVersion*)(*_objects)[i];
235  _packages.push_back(pack);
236  }
237 }
238 
239 //********************************************************************
241 //********************************************************************
242 
243  std::cout << " ------ List of highland2 package Versions ---------" << std::endl;
244  char out[256];
245  sprintf(out,"%3s: %-30s %-15s", "#", "package", "version");
246  std::cout << out << "\n" << std::endl;
247 
248  for (UInt_t i=0;i<_packages.size();i++){
249  PackageVersion* pack = _packages[i];
250  sprintf(out,"%3d: %-30s %-15s", i, pack->Name().c_str(), pack->Version().c_str());
251  std::cout << out << std::endl;
252  }
253  std::cout << " ----------------------------------------------------" << std::endl;
254 }
255 
bool CheckVersionCompatibility(const std::string &versionInput, ProdId_h prodND280)
Check compatibility between input file software version and file used to compile nd280AnalysisTools.
const std::string & Version() const
Return the version of this package.
void ReadVersions(const std::string &file)
Read package names and versions use to produce this file.
void AddPackage(const std::string &name, const std::string &version)
Add a package.
std::string _version
The name of this package.
ProdId_h GetProduction(const std::string softVersion)
Get the enum of the production corresponding to a given sofware version.
static Versioning & Get(void)
return the singleton
void DumpVersions()
dump package names and versions
std::string _name
The name of this package.
void AddProduction(ProdId_h prodId, const std::string &prodName, const std::string &prodLowVersion, const std::string &prodHighVersion)
Add a production.
const std::string & Name() const
Return the name of this package.
std::string GetProductionName(const std::string softVersion)
Get the name of the production corresponding to a given sofware version.