CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CalibFormats/SiPixelObjects/interface/PixelConfigFile.h

Go to the documentation of this file.
00001 #ifndef PixelConfigFile_h
00002 #define PixelConfigFile_h
00003 
00010 #include "CalibFormats/SiPixelObjects/interface/PixelConfigBase.h"
00011 #include "CalibFormats/SiPixelObjects/interface/PixelConfigAlias.h"
00012 #include "CalibFormats/SiPixelObjects/interface/PixelConfig.h"
00013 #include "CalibFormats/SiPixelObjects/interface/PixelConfigList.h"
00014 #include "CalibFormats/SiPixelObjects/interface/PixelAliasList.h"
00015 #include "CalibFormats/SiPixelObjects/interface/PixelVersionAlias.h"
00016 #include "CalibFormats/SiPixelObjects/interface/PixelCalibBase.h"
00017 #include "CalibFormats/SiPixelObjects/interface/PixelConfigKey.h"
00018 #include "CalibFormats/SiPixelObjects/interface/PixelTrimBase.h"
00019 #include "CalibFormats/SiPixelObjects/interface/PixelTrimAllPixels.h"
00020 #include "CalibFormats/SiPixelObjects/interface/PixelMaskBase.h"
00021 #include "CalibFormats/SiPixelObjects/interface/PixelMaskAllPixels.h"
00022 #include "CalibFormats/SiPixelObjects/interface/PixelDACSettings.h"
00023 #include "CalibFormats/SiPixelObjects/interface/PixelTBMSettings.h"
00024 #include "CalibFormats/SiPixelObjects/interface/PixelDetectorConfig.h"
00025 #include "CalibFormats/SiPixelObjects/interface/PixelLowVoltageMap.h"
00026 #include "CalibFormats/SiPixelObjects/interface/PixelMaxVsf.h"
00027 #include "CalibFormats/SiPixelObjects/interface/PixelNameTranslation.h"
00028 #include "CalibFormats/SiPixelObjects/interface/PixelFEDCard.h"
00029 #include "CalibFormats/SiPixelObjects/interface/PixelCalibConfiguration.h"
00030 #include "CalibFormats/SiPixelObjects/interface/PixelPortCardConfig.h"
00031 #include "CalibFormats/SiPixelObjects/interface/PixelPortcardMap.h"
00032 #include "CalibFormats/SiPixelObjects/interface/PixelDelay25Calib.h"
00033 #include "CalibFormats/SiPixelObjects/interface/PixelFECConfig.h"
00034 #include "CalibFormats/SiPixelObjects/interface/PixelTKFECConfig.h"
00035 #include "CalibFormats/SiPixelObjects/interface/PixelFEDConfig.h"
00036 #include "CalibFormats/SiPixelObjects/interface/PixelTTCciConfig.h"
00037 #include "CalibFormats/SiPixelObjects/interface/PixelLTCConfig.h"
00038 #include "CalibFormats/SiPixelObjects/interface/PixelFEDTestDAC.h"
00039 #include "CalibFormats/SiPixelObjects/interface/PixelGlobalDelay25.h"
00040 #include <string>
00041 #include <vector>
00042 #include <map>
00043 #include <iostream>
00044 #include <sstream>
00045 #include <typeinfo>
00046 #include <stdexcept>
00047 #include <sys/stat.h>
00048 #include <sys/types.h>
00049 
00050 #define DEBUG_CF_ 0
00051 
00052 namespace pos{
00059   class PixelConfigFile {
00060 
00061   public:
00062 
00063     static std::vector<std::pair<std::string, unsigned int> > getAliases(){
00064       PixelAliasList& aliases=getAlias();
00065       std::vector<std::pair<std::string, unsigned int> > tmp;
00066       for(unsigned int i=0;i<aliases.nAliases();i++){
00067         std::pair<std::string, unsigned int> apair(aliases.name(i),aliases.key(i));
00068         tmp.push_back(apair);  
00069       }
00070       return tmp;
00071     }
00072 
00073     static std::vector<std::string> getVersionAliases(std::string path){
00074       return getAlias().getVersionAliases(path);
00075     }
00076 
00077     static bool getVersionAliases(std::string configAlias,
00078                                   unsigned int &key,
00079                                   std::vector<std::pair<std::string,std::string> > &versionAliases){
00080       PixelConfigAlias* alias=getAlias().versionAliases(configAlias);
00081       if (alias==0) {
00082         return false;
00083       }
00084       key=alias->key();
00085       versionAliases=alias->versionAliases();
00086       return true;
00087     }
00088 
00089 
00090     static std::map<std::string, unsigned int> getAliases_map(){
00091       PixelAliasList& aliases=getAlias();
00092       std::map<std::string, unsigned int> tmp;
00093       for(unsigned int i=0;i<aliases.nAliases();i++){
00094         tmp.insert(make_pair(aliases.name(i), aliases.key(i)));
00095       }
00096       return tmp;
00097     }
00098 
00099 
00100     static PixelConfigList& configList(){
00101       static PixelConfigList theConfigList=getConfig();
00102       return theConfigList;
00103     }
00104 
00105 
00106     static unsigned int getVersion(std::string path,std::string alias){
00107       return getAlias().getVersion(path,alias);
00108     }
00109 
00110     // Added by Dario, May 20th, 2008 =====================================================================
00111     static pos::pathVersionAliasMmap getVersionData(){  
00112       return getAlias().getVersionData();
00113     }
00114     static pos::pathVersionAliasMmap getVersionData(std::string koc){  
00115       return getAlias().getVersionData(koc);
00116     }
00117     
00118     static std::vector<pathAliasPair> getConfigAliases(std::string path) {
00119      return getAlias().getConfigAliases(path) ;
00120     }
00121     // End of Dario's addition ============================================================================
00122 
00123     static void addAlias(std::string alias, unsigned int key){
00124       PixelConfigAlias anAlias(alias,key);
00125       getAlias().insertAlias(anAlias);
00126       getAlias().writefile();
00127     }
00128 
00129 
00130     static void addAlias(std::string alias, unsigned int key,
00131                          std::vector<std::pair<std::string, std::string> > versionaliases){
00132       PixelConfigAlias anAlias(alias,key);
00133       for(unsigned int i=0;i<versionaliases.size();i++){
00134         anAlias.addVersionAlias(versionaliases[i].first,versionaliases[i].second);
00135       }
00136       getAlias().insertAlias(anAlias);
00137       getAlias().writefile();
00138     }
00139 
00140     static std::vector< std::pair< std::string, unsigned int> > getVersions(pos::PixelConfigKey key){
00141 
00142       static PixelConfigList& configs=getConfig();
00143       PixelConfig& theConfig=configs[key.key()];
00144       return theConfig.versions();
00145       
00146     }
00147 
00148     static void addVersionAlias(std::string path, unsigned int version, std::string alias){
00149 
00150       PixelConfigList& configs=getConfig();
00151 
00152       PixelVersionAlias anAlias(path, version, alias);
00153       getAlias().insertVersionAlias(anAlias);
00154       getAlias().updateConfigAlias(path,version,alias,configs);
00155       getAlias().writefile();
00156       configs.writefile();
00157     }
00158 
00159     static unsigned int makeKey(std::vector<std::pair<std::string, unsigned int> > versions){
00160 
00161       PixelConfig config;
00162 
00163       for(unsigned int i=0;i<versions.size();i++){
00164         config.add(versions[i].first,versions[i].second);
00165       }
00166 
00167       PixelConfigList& configs=getConfig();
00168 
00169       unsigned int newkey=configs.add(config);
00170 
00171       configs.writefile();
00172     
00173       return newkey;
00174 
00175     }
00176 
00177     static PixelConfigList& getConfig(){
00178 
00179       static PixelConfigList configs;
00180 
00181       //FIXME 
00182 
00183       static int counter=0;
00184 
00185       if(counter!=0)
00186         {
00187           while(counter!=0)
00188             {
00189               std::cout << __LINE__ << "]\t[PixelConfigFile::getConfig()]\t\t\t\t    Waiting for other thread to complete reading"<<std::endl;
00190               ::sleep(1);
00191             }
00192           return configs;
00193         }
00194       
00195       counter++;
00196 
00197       static std::string directory;
00198       static int first=1;
00199     
00200       directory=getenv("PIXELCONFIGURATIONBASE");
00201       std::string filename=directory+"/configurations.txt";
00202 /* Don't know who put this snippet of code here: this case is already contemplated in the 'else' of the 'if' statement below
00203       if(!first)
00204         {
00205           configs.reload(filename) 
00206         }
00207 */
00208       if (first) 
00209         {
00210           first=0;
00211           configs.readfile(filename);
00212           forceConfigReload(false) ;
00213         }
00214       else
00215         {
00216           //      if( getForceConfigReload() ) {
00217             configs.reload(filename);
00218             forceConfigReload(false) ;
00219             //    }
00220         }
00221 
00222       counter--;
00223 
00224       return configs;
00225 
00226     }
00227 
00228     static PixelAliasList& getAlias(){
00229 
00230       static std::string directory;
00231       static int first=1;
00232     
00233       static PixelAliasList aliases;
00234     
00235       directory=getenv("PIXELCONFIGURATIONBASE");
00236       std::string filename=directory+"/aliases.txt";
00237 
00238       if (first) {
00239         first=0;
00240         aliases.readfile(filename);
00241         
00242         forceAliasesReload(false) ;
00243       } else {
00244         if( getForceAliasesReload() ) {
00245          aliases.readfile(filename);
00246          forceAliasesReload(false) ;
00247         }
00248       }
00249       
00250       return aliases;
00251 
00252     }
00253 
00254     static void forceAliasesReload(bool m){
00255       if(getForceAliasesReload() != m){
00256         getForceAliasesReload() = m;
00257       }
00258     }
00259 
00260     static void forceConfigReload(bool m){
00261       if(getForceConfigReload() != m){
00262         getForceConfigReload() = m;
00263       }
00264     }
00265 
00266     //Returns the path the the configuration data.
00267     static std::string getPath(std::string path, PixelConfigKey key){
00268 
00269       unsigned int theKey=key.key();
00270     
00271       assert(theKey<=getConfig().size());
00272     
00273       unsigned int last=path.find_last_of("/");
00274       assert(last!=std::string::npos);
00275     
00276       std::string base=path.substr(0,last);
00277       std::string ext=path.substr(last+1);
00278     
00279       unsigned int slashpos=base.find_last_of("/");
00280       if (slashpos==std::string::npos) {
00281         std::cout << "[pos::PixelConfigFile::getPath()]\t\t\tOn path:"                <<path               <<std::endl;
00282         std::cout << "[pos::PixelConfigFile::getPath()]\t\t\tRecall that you need a trailing /"            <<std::endl;
00283         ::abort();
00284       }
00285     
00286       std::string dir=base.substr(slashpos+1);
00287     
00288 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted dir:" <<dir <<std::endl;
00289 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted base:"<<base<<std::endl;
00290 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted ext :"<<ext <<std::endl;
00291     
00292       unsigned int version;
00293       int err=getConfig()[theKey].find(dir,version);   
00294       // assert(err==0);
00295       if(0!=err) 
00296         {
00297           return "";
00298         }
00299     
00300       std::ostringstream s1;
00301       s1 << version;
00302       std::string strversion=s1.str();
00303 
00304       static std::string directory;
00305       directory=getenv("PIXELCONFIGURATIONBASE");
00306     
00307       std::string fullpath=directory+"/"+dir+"/"+strversion+"/";
00308     
00309       return fullpath;
00310     }
00311 
00312 
00313   
00314     
00315     //Returns a pointer to the data found in the path with configuration key.
00316     template <class T>
00317       static void get(T* &data, std::string path, PixelConfigKey key){
00318       unsigned int theKey=key.key();
00319       
00320       if (theKey>=configList().size()){
00321         configList()=getConfig();
00322       }
00323 
00324       assert(theKey<=configList().size());
00325     
00326       unsigned int last=path.find_last_of("/");
00327       assert(last!=std::string::npos);
00328     
00329       std::string base=path.substr(0,last);
00330       std::string ext=path.substr(last+1);
00331     
00332       unsigned int slashpos=base.find_last_of("/");
00333       if (slashpos==std::string::npos) {
00334         std::cout << "[pos::PixelConfigFile::get()]\t\t\tAsking for data of type:"<<typeid(data).name()<<std::endl;
00335         std::cout << "[pos::PixelConfigFile::get()]\t\t\tOn path:"                <<path               <<std::endl;
00336         std::cout << "[pos::PixelConfigFile::get()]\t\t\tRecall that you need a trailing /"            <<std::endl;
00337         ::abort();
00338       }
00339     
00340       std::string dir=base.substr(slashpos+1);
00341     
00342 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted dir:" <<dir <<std::endl;
00343 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted base:"<<base<<std::endl;
00344 //      std::cout << "[pos::PixelConfigFile::get()]\t\t\tExtracted ext :"<<ext <<std::endl;
00345     
00346       unsigned int version;
00347       int err=configList()[theKey].find(dir,version);   
00348       // assert(err==0);
00349       if(0!=err) 
00350         {
00351           std::cout<<"[PixelConfigFile.h::get] error loading config list. "<<theKey<<" "<<dir<<" "<<version<<std::endl;
00352           data= 0; 
00353           return;
00354         }
00355     
00356       std::ostringstream s1;
00357       s1 << version;
00358       std::string strversion=s1.str();
00359 
00360       static std::string directory;
00361       directory=getenv("PIXELCONFIGURATIONBASE");
00362     
00363       std::string fullpath=directory+"/"+dir+"/"+strversion+"/";
00364     
00365       //std::cout << "Directory for configuration data:"<<fullpath<<std::endl;
00366 
00367       try {
00368     
00369       if (typeid(data)==typeid(PixelTrimBase*)){
00370         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelTrimBase" << std::endl;
00371         assert(dir=="trim");
00372         data = (T*) new PixelTrimAllPixels(fullpath+"ROC_Trims_module_"+ext+".dat");
00373         return;
00374       }else if (typeid(data)==typeid(PixelMaskBase*)){
00375         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelMaskBase" << std::endl;
00376         assert(dir=="mask");
00377         data = (T*) new PixelMaskAllPixels(fullpath+"ROC_Masks_module_"+ext+".dat");
00378         return;
00379       }else if (typeid(data)==typeid(PixelDACSettings*)){
00380         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelDACSettings" << std::endl;
00381         assert(dir=="dac");
00382         data = (T*) new PixelDACSettings(fullpath+"ROC_DAC_module_"+ext+".dat");
00383         return;
00384       }else if (typeid(data)==typeid(PixelTBMSettings*)){
00385         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelTBMSettings" << std::endl;
00386         assert(dir=="tbm");
00387         data = (T*) new PixelTBMSettings(fullpath+"TBM_module_"+ext+".dat");
00388         return;
00389       }else if (typeid(data)==typeid(PixelDetectorConfig*)){
00390         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelDetectorConfig" << std::endl;
00391         assert(dir=="detconfig");
00392         data = (T*) new PixelDetectorConfig(fullpath+"detectconfig.dat");
00393         return;
00394       }else if (typeid(data)==typeid(PixelLowVoltageMap*)){
00395         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill fetch PixelLowVoltageMap" << std::endl;
00396         assert(dir=="lowvoltagemap");
00397         data = (T*) new PixelLowVoltageMap(fullpath+"lowvoltagemap.dat");
00398         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return the PixelLowVoltageMap" << std::endl;
00399         return;
00400       }else if (typeid(data)==typeid(PixelMaxVsf*)){
00401         //std::cout << "Will fetch PixelMaxVsf" << std::endl;
00402         assert(dir=="maxvsf");
00403         data = (T*) new PixelMaxVsf(fullpath+"maxvsf.dat");
00404         //std::cout << "Will return the PixelMaxVsf" << std::endl;
00405         return;
00406       }else if (typeid(data)==typeid(PixelNameTranslation*)){
00407         //std::cout << __LINE__ << "]\t[pos::PixelConfigFile::get()]\t\t\t    Will return PixelNameTranslation*" << std::endl;
00408         assert(dir=="nametranslation");
00409         data = (T*) new PixelNameTranslation(fullpath+"translation.dat");
00410         return;
00411       }else if (typeid(data)==typeid(PixelFEDCard*)){
00412         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelFEDCard" << std::endl;
00413         assert(dir=="fedcard");
00414         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill open:"<<fullpath+"params_fed_"+ext+".dat"<< std::endl;
00415         data = (T*) new PixelFEDCard(fullpath+"params_fed_"+ext+".dat");
00416         return;
00417       }else if (typeid(data)==typeid(PixelCalibBase*)){
00418         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelCalibBase" << std::endl;
00419         assert(dir=="calib");
00420         std::string calibfile=fullpath+"calib.dat";
00421         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tLooking for file " << calibfile << std::endl;
00422         std::ifstream calibin(calibfile.c_str());
00423         if(calibin.good()){
00424           data = (T*) new PixelCalibConfiguration(calibfile);
00425         }else{
00426           calibfile=fullpath+"delay25.dat";
00427           //std::cout << "[pos::PixelConfigFile::get()]\t\t\tNow looking for file " << calibfile << std::endl;
00428           std::ifstream delayin(calibfile.c_str());
00429           if(delayin.good()){
00430             data = (T*) new PixelDelay25Calib(calibfile);
00431           }else{
00432             calibfile=fullpath+"fedtestdac.dat";
00433             //std::cout << "[pos::PixelConfigFile::get()]\t\t\tNow looking for file " << calibfile << std::endl;
00434             std::ifstream delayin(calibfile.c_str());
00435             if(delayin.good()){
00436               data = (T*) new PixelFEDTestDAC(calibfile);
00437             }else{
00438               throw std::runtime_error("[pos::PixelConfigFile::get()]\t\t\tCan't find calibration file calib.dat or delay25.dat or fedtestdac.dat");    
00439             }
00440           }
00441         }
00442         return;
00443       }else if (typeid(data)==typeid(PixelTKFECConfig*)){
00444         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelTKFECConfig" << std::endl;
00445         assert(dir=="tkfecconfig");
00446         data = (T*) new PixelTKFECConfig(fullpath+"tkfecconfig.dat");
00447         return;
00448       }else if (typeid(data)==typeid(PixelFECConfig*)){
00449         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelFECConfig" << std::endl;
00450         assert(dir=="fecconfig");
00451         data = (T*) new PixelFECConfig(fullpath+"fecconfig.dat");
00452         return;
00453       }else if (typeid(data)==typeid(PixelFEDConfig*)){
00454         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelFEDConfig" << std::endl;
00455         assert(dir=="fedconfig");
00456         data = (T*) new PixelFEDConfig(fullpath+"fedconfig.dat");
00457         return;
00458       }else if (typeid(data)==typeid(PixelPortCardConfig*)){
00459         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelPortCardConfig" << std::endl;
00460         assert(dir=="portcard");
00461         data = (T*) new PixelPortCardConfig(fullpath+"portcard_"+ext+".dat");
00462         return;
00463       }else if (typeid(data)==typeid(PixelPortcardMap*)){
00464         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelPortcardMap" << std::endl;
00465         assert(dir=="portcardmap");
00466         data = (T*) new PixelPortcardMap(fullpath+"portcardmap.dat");
00467         return;
00468       }else if (typeid(data)==typeid(PixelDelay25Calib*)){
00469         //cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelDelay25Calib" << std::endl;
00470         assert(dir=="portcard");
00471         data = (T*) new PixelDelay25Calib(fullpath+"delay25.dat");
00472         return;
00473       }else if (typeid(data)==typeid(PixelTTCciConfig*)){
00474         //cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelTTCciConfig" << std::endl;
00475         assert(dir=="ttcciconfig");
00476         data = (T*) new PixelTTCciConfig(fullpath+"TTCciConfiguration.txt");
00477         return;
00478       }else if (typeid(data)==typeid(PixelLTCConfig*)){
00479         //cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelLTCConfig" << std::endl;
00480         assert(dir=="ltcconfig");
00481         data = (T*) new PixelLTCConfig(fullpath+"LTCConfiguration.txt");
00482         return;
00483       }else if (typeid(data)==typeid(PixelGlobalDelay25*)){
00484         //std::cout << "[pos::PixelConfigFile::get()]\t\t\tWill return PixelGlobalDelay25" << std::endl;
00485         assert(dir=="globaldelay25");
00486         data = (T*) new PixelGlobalDelay25(fullpath+"globaldelay25.dat");
00487         return;
00488       }else{
00489         std::cout << "[pos::PixelConfigFile::get()]\t\t\tNo match" << std::endl;
00490         assert(0);
00491       }
00492       } catch (std::exception & e) {
00493         std::cout<<"[PixelConfigFile::get] Caught exception while constructing configuration object. Will rethrow."<<std::endl;
00494         throw;
00495       }
00496     }
00497 
00498     //----- Method added by Dario (March 10, 2008)
00499     template <class T>
00500       static bool configurationDataExists(T* &data, std::string path, PixelConfigKey key){
00501 
00502       std::string mthn = "]\t[pos::PixelConfigFile::configurationDataExists()]\t    " ;
00503 /*       pos::PixelTimeFormatter * timer = new pos::PixelTimeFormatter("PixelConfigFile::ConfigurationDataExists") ; */
00504       unsigned int theKey=key.key();
00505     
00506 
00507       assert(theKey<=configList().size());
00508       
00509     
00510       unsigned int last=path.find_last_of("/");
00511       assert(last!=std::string::npos);
00512     
00513       std::string base=path.substr(0,last);
00514       std::string ext=path.substr(last+1);
00515     
00516       unsigned int slashpos=base.find_last_of("/");
00517       if (slashpos==std::string::npos) {
00518         std::cout << __LINE__ << mthn << "Asking for data of type:"          << typeid(data).name() <<std::endl;
00519         std::cout << __LINE__ << mthn << "On path:"                      << path                <<std::endl;
00520         std::cout << __LINE__ << mthn << "Recall that you need a trailing /"                    <<std::endl;
00521         ::abort();
00522       }
00523     
00524       std::string dir=base.substr(slashpos+1);
00525 /*       timer->stopTimer() ; */
00526 //      std::cout << __LINE__ << mthn << "Extracted dir:"  << dir  <<std::endl;
00527 //      std::cout << __LINE__ << mthn << "Extracted base:" << base <<std::endl;
00528 //      std::cout << __LINE__ << mthn << "Extracted ext :" << ext  <<std::endl;
00529     
00530       unsigned int version;
00531       int err=configList()[theKey].find(dir,version);   
00532       // assert(err==0);
00533       if(0!=err) 
00534         {
00535           data= 0; 
00536           return false ;
00537         }
00538 /*       timer->stopTimer() ; */
00539 /*       delete timer ; */
00540 
00541       std::ostringstream s1;
00542       s1 << version;
00543       std::string strversion=s1.str();
00544 
00545       static std::string directory;
00546       directory=getenv("PIXELCONFIGURATIONBASE");
00547     
00548       std::string fullpath=directory+"/"+dir+"/"+strversion+"/";
00549     
00550       //std::cout << __LINE__ << mthn << "Directory for configuration data:"<<fullpath<<std::endl;
00551     
00552       std::string fileName ;
00553       if (typeid(data)==typeid(PixelTrimBase*)){
00554         fileName = fullpath+"ROC_Trims_module_"+ext+".dat" ;
00555       }else if (typeid(data)==typeid(PixelMaskBase*)){
00556         fileName = fullpath+"ROC_Masks_module_"+ext+".dat";
00557       }else if (typeid(data)==typeid(PixelDACSettings*)){
00558         fileName = fullpath+"ROC_DAC_module_"+ext+".dat";
00559       }else if (typeid(data)==typeid(PixelTBMSettings*)){
00560         fileName = fullpath+"TBM_module_"+ext+".dat";
00561       }else if (typeid(data)==typeid(PixelDetectorConfig*)){
00562         fileName = fullpath+"detectconfig.dat";
00563       }else if (typeid(data)==typeid(PixelLowVoltageMap*)){
00564         fileName = fullpath+"lowvoltagemap.dat";
00565       }else if (typeid(data)==typeid(PixelMaxVsf*)){
00566         fileName = fullpath+"maxvsf.dat";
00567       }else if (typeid(data)==typeid(PixelNameTranslation*)){
00568         fileName = fullpath+"translation.dat";
00569       }else if (typeid(data)==typeid(PixelFEDCard*)){
00570         fileName = fullpath+"params_fed_"+ext+".dat";
00571       }else if (typeid(data)==typeid(PixelTKFECConfig*)){
00572         fileName = fullpath+"tkfecconfig.dat";
00573       }else if (typeid(data)==typeid(PixelFECConfig*)){
00574         fileName = fullpath+"fecconfig.dat";
00575       }else if (typeid(data)==typeid(PixelFEDConfig*)){
00576         fileName = fullpath+"fedconfig.dat";
00577       }else if (typeid(data)==typeid(PixelPortCardConfig*)){
00578         fileName = fullpath+"portcard_"+ext+".dat";
00579       }else if (typeid(data)==typeid(PixelPortcardMap*)){
00580         fileName = fullpath+"portcardmap.dat";
00581       }else if (typeid(data)==typeid(PixelDelay25Calib*)){
00582         fileName = fullpath+"delay25.dat";
00583       }else if (typeid(data)==typeid(PixelTTCciConfig*)){
00584         fileName = fullpath+"TTCciConfiguration.txt";
00585       }else if (typeid(data)==typeid(PixelLTCConfig*)){
00586         fileName = fullpath+"LTCConfiguration.txt";
00587       }else if (typeid(data)==typeid(PixelGlobalDelay25*)){
00588         fileName = fullpath+"globaldelay25.dat";
00589       }else if (typeid(data)==typeid(PixelCalibBase*)){
00590         assert(dir=="calib");
00591         std::string calibfile=fullpath+"calib.dat";
00592         std::ifstream calibin(calibfile.c_str());
00593         if(calibin.good())
00594         {
00595          std::cout << __LINE__ << mthn << "Found " << calibfile << std::endl ; 
00596          return true ;
00597         }else{
00598          calibfile=fullpath+"delay25.dat";
00599          std::ifstream delayin(calibfile.c_str());
00600          if(delayin.good())
00601          {
00602           std::cout << __LINE__ << mthn << "Found " << calibfile << std::endl ; 
00603           return true ;
00604          }else{
00605           calibfile=fullpath+"fedtestdac.dat";
00606           std::ifstream delayin(calibfile.c_str());
00607           if(delayin.good())
00608           {
00609            std::cout << __LINE__ << mthn << "Found " << calibfile << std::endl ; 
00610            return true ;
00611           }else{
00612            std::cout << mthn << "Can't find calibration file calib.dat or delay25.dat or fedtestdac.dat" << std::endl;
00613            return false  ;
00614           }
00615          }
00616         }
00617       }else{
00618         std::cout << __LINE__ << mthn << "No match of class type" << std::endl;
00619         return false ;
00620       }
00621       /*
00622       struct stat * tmp = NULL ;
00623       if(stat(fileName.c_str(), tmp)==0)
00624         {
00625           std::cout << mthn << "Found(stat) " << fileName << std::endl ; 
00626           return true ;
00627         }
00628       else
00629         {
00630           std::cout << mthn << "Not found(stat) " << fileName << std::endl ; 
00631           return false ;
00632         }
00633       */
00634       std::ifstream in(fileName.c_str());
00635       if (!in.good())
00636       {
00637        std::cout << __LINE__ << mthn << "Not found " << fileName << std::endl ; 
00638        return false ;
00639       }
00640       in.close() ;
00641       if( DEBUG_CF_ ) std::cout << __LINE__ << mthn << "Found " << fileName << std::endl ; 
00642       return true ;
00643       
00644     }
00645     //----- End of method added by Dario (March 10, 2008)
00646 
00647 
00648     //Returns a pointer to the data found in the path with configuration key.
00649     template <class T>
00650       static void get(T* &data, std::string path, unsigned int version){
00651 
00652       std::string mthn = "]\t[pos::PixelConfigFile::get()]\t\t\t\t    " ;
00653 
00654       unsigned int last=path.find_last_of("/");
00655       assert(last!=std::string::npos);
00656     
00657       std::string base=path.substr(0,last);
00658       std::string ext=path.substr(last+1);
00659     
00660       unsigned int slashpos=base.find_last_of("/");
00661       //if (slashpos==std::string::npos) {
00662       //std::cout << __LINE__ << mthn << "Asking for data of type:"          << typeid(data).name() << std::endl;
00663       //std::cout << __LINE__ << mthn << "On path:"                          << path                << std::endl;
00664       //std::cout << __LINE__ << mthn << "Recall that you need a trailing /"                        << std::endl;
00665       //::abort();
00666       //}
00667     
00668       std::string dir=base.substr(slashpos+1);
00669     
00670       //std::cout << __LINE__ << mthn << "Extracted dir :" << dir  <<std::endl;
00671       //std::cout << __LINE__ << mthn << "Extracted base:" << base <<std::endl;
00672       //std::cout << __LINE__ << mthn << "Extracted ext :" << ext  <<std::endl;
00673     
00674       std::ostringstream s1;
00675       s1 << version;
00676       std::string strversion=s1.str();
00677 
00678       static std::string directory;
00679       directory=getenv("PIXELCONFIGURATIONBASE");
00680     
00681       std::string fullpath=directory+"/"+dir+"/"+strversion+"/";
00682     
00683       //std::cout << __LINE__ << mthn << "Directory for configuration data:"<<fullpath<<std::endl;
00684     
00685       if (typeid(data)==typeid(PixelTrimBase*)){
00686         //std::cout << __LINE__ << mthn << "Will return PixelTrimBase" << std::endl;
00687         assert(dir=="trim");
00688         data = (T*) new PixelTrimAllPixels(fullpath+"ROC_Trims_module_"+ext+".dat");
00689         return;
00690       }else if (typeid(data)==typeid(PixelMaskBase*)){
00691         //std::cout << __LINE__ << mthn << "Will return PixelMaskBase" << std::endl;
00692         assert(dir=="mask");
00693         data = (T*) new PixelMaskAllPixels(fullpath+"ROC_Masks_module_"+ext+".dat");
00694         return;
00695       }else if (typeid(data)==typeid(PixelDACSettings*)){
00696         //std::cout << __LINE__ << mthn << "Will return PixelDACSettings" << std::endl;
00697         assert(dir=="dac");
00698         data = (T*) new PixelDACSettings(fullpath+"ROC_DAC_module_"+ext+".dat");
00699         return;
00700       }else if (typeid(data)==typeid(PixelTBMSettings*)){
00701         //std::cout << __LINE__ << mthn << "Will return PixelTBMSettings" << std::endl;
00702         assert(dir=="tbm");
00703         data = (T*) new PixelTBMSettings(fullpath+"TBM_module_"+ext+".dat");
00704         return;
00705       }else if (typeid(data)==typeid(PixelDetectorConfig*)){
00706         //std::cout << __LINE__ << mthn << "Will return PixelDACSettings" << std::endl;
00707         assert(dir=="detconfig");
00708         data = (T*) new PixelDetectorConfig(fullpath+"detectconfig.dat");
00709         return;
00710       }else if (typeid(data)==typeid(PixelLowVoltageMap*)){
00711         //std::cout << __LINE__ << mthn << "Will fetch1 PixelLowVoltageMap" << std::endl;
00712         assert(dir=="lowvoltagemap");
00713         data = (T*) new PixelLowVoltageMap(fullpath+"detectconfig.dat");
00714         //std::cout << __LINE__ << mthn << "Will return1 the PixelLowVoltageMap" << std::endl;
00715         return;
00716       }else if (typeid(data)==typeid(PixelMaxVsf*)){
00717         //std::cout << __LINE__ << mthn << "Will fetch1 PixelMaxVsf" << std::endl;
00718         assert(dir=="maxvsf");
00719         data = (T*) new PixelMaxVsf(fullpath+"maxvsf.dat");
00720         //std::cout << __LINE__ << mthn << "Will return1 the PixelMaxVsf" << std::endl;
00721         return;
00722       }else if (typeid(data)==typeid(PixelNameTranslation*)){
00723         //std::cout << __LINE__ << mthn << "Will return PixelNameTranslation" << std::endl;
00724         assert(dir=="nametranslation");
00725         data = (T*) new PixelNameTranslation(fullpath+"translation.dat");
00726         return;
00727       }else if (typeid(data)==typeid(PixelFEDCard*)){
00728         //std::cout << __LINE__ << mthn << "Will return PixelFEDCard" << std::endl;
00729         assert(dir=="fedcard");
00730         //std::cout << __LINE__ << mthn << "Will open:"<<fullpath+"params_fed_"+ext+".dat"<< std::endl;
00731         data = (T*) new PixelFEDCard(fullpath+"params_fed_"+ext+".dat");
00732         return;
00733       }else if (typeid(data)==typeid(PixelCalibBase*)) {
00734         //std::cout << __LINE__ << mthn << "Will return PixelCalibBase" << std::endl;
00735         assert(base=="calib");
00736         std::string calibfile=fullpath+"calib.dat";
00737         //std::cout << mthn << "Looking for file " << calibfile << std::endl;
00738         std::ifstream calibin(calibfile.c_str());
00739         if(calibin.good()){
00740           data = (T*) new PixelCalibConfiguration(calibfile);
00741         }else{
00742           calibfile=fullpath+"delay25.dat";
00743           //std::cout << __LINE__ << mthn << "Now looking for file " << calibfile << std::endl;
00744           std::ifstream delayin(calibfile.c_str());
00745           if(delayin.good()){
00746             data = (T*) new PixelDelay25Calib(calibfile);
00747           }else{
00748             calibfile=fullpath+"fedtestdac.dat";
00749             //std::cout << __LINE__ << mthn << "Now looking for file " << calibfile << std::endl;
00750             std::ifstream delayin(calibfile.c_str());
00751             if(delayin.good()){
00752               data = (T*) new PixelFEDTestDAC(calibfile);
00753             }else{
00754               std::cout << __LINE__ << mthn << "Can't find calibration file calib.dat or delay25.dat or fedtestdac.dat" << std::endl;
00755               data=0;
00756             }
00757           }
00758         }
00759         return;
00760       }else if (typeid(data)==typeid(PixelTKFECConfig*)){
00761         //std::cout << __LINE__ << mthn << "Will return PixelTKFECConfig" << std::endl;
00762         assert(dir=="tkfecconfig");
00763         data = (T*) new PixelTKFECConfig(fullpath+"tkfecconfig.dat");
00764         return;
00765       }else if (typeid(data)==typeid(PixelFECConfig*)){
00766         //std::cout << __LINE__ << mthn << "Will return PixelFECConfig" << std::endl;
00767         assert(dir=="fecconfig");
00768         data = (T*) new PixelFECConfig(fullpath+"fecconfig.dat");
00769         return;
00770       }else if (typeid(data)==typeid(PixelFEDConfig*)){
00771         //std::cout << __LINE__ << mthn << "Will return PixelFEDConfig" << std::endl;
00772         assert(dir=="fedconfig");
00773         data = (T*) new PixelFEDConfig(fullpath+"fedconfig.dat");
00774         return;
00775       }else if (typeid(data)==typeid(PixelPortCardConfig*)){
00776         //std::cout << __LINE__ << mthn << "Will return PixelPortCardConfig" << std::endl;
00777         assert(dir=="portcard");
00778         data = (T*) new PixelPortCardConfig(fullpath+"portcard_"+ext+".dat");
00779         return;
00780       }else if (typeid(data)==typeid(PixelPortcardMap*)){
00781         //std::cout << __LINE__ << mthn << "Will return PixelPortcardMap" << std::endl;
00782         assert(dir=="portcardmap");
00783         data = (T*) new PixelPortcardMap(fullpath+"portcardmap.dat");
00784         return;
00785       }else if (typeid(data)==typeid(PixelDelay25Calib*)){
00786         //cout << __LINE__ << mthn << "Will return PixelDelay25Calib" << std::endl;
00787         assert(dir=="portcard");
00788         data = (T*) new PixelDelay25Calib(fullpath+"delay25.dat");
00789         return;
00790       }else if (typeid(data)==typeid(PixelTTCciConfig*)){
00791         //cout << __LINE__ << mthn << "Will return PixelTTCciConfig" << std::endl;
00792         assert(dir=="ttcciconfig");
00793         data = (T*) new PixelTTCciConfig(fullpath+"TTCciConfiguration.txt");
00794         return;
00795       }else if (typeid(data)==typeid(PixelLTCConfig*)){
00796         //cout << __LINE__ << mthn << "Will return PixelLTCConfig" << std::endl;
00797         assert(dir=="ltcconfig");
00798         data = (T*) new PixelLTCConfig(fullpath+"LTCConfiguration.txt");
00799         return;
00800       }else if (typeid(data)==typeid(PixelGlobalDelay25*)){
00801         //std::cout << __LINE__ << mthn << "Will return PixelGlobalDelay25" << std::endl;
00802         assert(dir=="globaldelay25");
00803         data = (T*) new PixelGlobalDelay25(fullpath+"globaldelay25.dat");
00804         return;
00805       }else{
00806         std::cout << __LINE__ << mthn << "No class match" << std::endl;
00807         assert(0);
00808         data=0;
00809         return;
00810       }
00811 
00812     }
00813 
00814     template <class T>
00815       static void get(std::map<std::string, T*> &pixelObjects, PixelConfigKey key){
00816 
00817       typename std::map<std::string, T* >::iterator iObject=pixelObjects.begin();
00818 
00819       for(;iObject!=pixelObjects.end();++iObject){
00820         get(iObject->second,iObject->first,key);
00821       }
00822 
00823     }
00824 
00825 
00826     static int makeNewVersion(std::string path, std::string &dir){
00827       //std::cout << "[pos::PixelConfigFile::makeNewVersion()]\t\tInserting data on path:"<<path<<std::endl;
00828       struct stat stbuf;
00829       std::string directory=getenv("PIXELCONFIGURATIONBASE");
00830       directory+="/";
00831       directory+=path;
00832       if (stat(directory.c_str(),&stbuf)!=0){
00833         
00834         std::cout << "[pos::PixelConfigFile::makeNewVersion()]\t\tThe path:"<<path<<" does not exist."<<std::endl;
00835         std::cout << "[pos::PixelConfigFile::makeNewVersion()]\t\tFull path:"<<directory<<std::endl;
00836         return -1;
00837       }
00838       directory+="/";
00839       int version=-1;
00840       do{
00841         version++;
00842         std::ostringstream s1;
00843         s1 << version  ;
00844         std::string strversion=s1.str();
00845         dir=directory+strversion;
00846         //std::cout << "[pos::PixelConfigFile::makeNewVersion()]\t\tWill check for version:"<<dir<<std::endl;
00847       }while(stat(dir.c_str(),&stbuf)==0);
00848       //std::cout << "[pos::PixelConfigFile::makeNewVersion()]\t\tThe new version is:"<<version<<std::endl;
00849       mkdir(dir.c_str(),0777);
00850       return version;
00851     }
00852 
00853 
00854     template <class T>
00855     static int put(const T* object, std::string path){
00856       std::string dir;
00857       int version=makeNewVersion(path,dir);
00858       object->writeASCII(dir);
00859       return version;
00860     }
00861 
00862     template <class T>
00863     static int put(std::vector<T*> objects, std::string path){
00864       std::cout << "[pos::PixelConfigFile::put()]\t\t# of objects to write: "<< objects.size() << std::endl;
00865       std::string dir;
00866       int version=makeNewVersion(path,dir);
00867       for(unsigned int i=0;i<objects.size();i++){
00868         // std::cout << "[pos::PixelConfigFile::put()]\t\t\t\t\tWill write i="<<i<<" ptr: "<<objects[i]<<std::endl;
00869         objects[i]->writeASCII(dir);
00870       }
00871       return version;
00872     }
00873 
00874   private:
00875     static bool& getForceAliasesReload(){
00876       static bool forceAliasesReload = false; 
00877       return forceAliasesReload;
00878     }
00879     static bool& getForceConfigReload(){
00880       static bool forceConfigReload = false; 
00881       return forceConfigReload;
00882     }
00883   
00884  };
00885 
00886 }
00887 #endif