CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/GeneratorInterface/SherpaInterface/src/SherpackFetcher.cc

Go to the documentation of this file.
00001 /* Based on the cmsGetFnConnect and fn-fileget implementation by Dave Dykstra 
00002  * CMSSW/ FWCore/ Services/ bin/ cmsGetFnConnect.cc
00003  * http://cdcvs.fnal.gov/cgi-bin/public-cvs/cvsweb-public.cgi/~checkout~/frontier/client/fn-fileget.c?rev=1.1&content-type=text/plain
00004 */
00005 
00006 
00007 //~ #include "SherpackFetcher.h"
00008 #include "GeneratorInterface/SherpaInterface/interface/SherpackFetcher.h"
00009 
00010 
00011 //~ #include <libtar.h>
00012 namespace spf {
00013 
00014 SherpackFetcher::SherpackFetcher(edm::ParameterSet const& pset)
00015 {
00016                 if (!pset.exists("SherpaProcess")) SherpaProcess="";
00017                   else SherpaProcess=pset.getParameter<std::string>("SherpaProcess");
00018                 if (!pset.exists("SherpackLocation")) SherpackLocation="";
00019                   else SherpackLocation=pset.getParameter<std::string>("SherpackLocation");
00020                 if (!pset.exists("SherpackChecksum")) SherpackChecksum="";
00021                   else SherpackChecksum=pset.getParameter<std::string>("SherpackChecksum");
00022                 if (!pset.exists("FetchSherpack")) FetchSherpack=false;
00023                   else FetchSherpack=pset.getParameter<bool>("FetchSherpack");
00024                 if (!pset.exists("SherpaPath")) SherpaPath="";
00025                   else SherpaPath=pset.getParameter<std::string>("SherpaPath");
00026 
00027 }
00028 
00029 int SherpackFetcher::Fetch()
00030 {
00031 
00032   std::string option  = "-c";
00033   std::string constr = "`cmsGetFnConnect frontier://smallfiles`";
00034   std::string sherpack = "sherpa_" + SherpaProcess + "_MASTER.tgz";
00035   std::string sherpackunzip = "sherpa_" + SherpaProcess + "_MASTER.tar";
00036   std::string path = SherpackLocation + "/" + sherpack;
00037   
00038   //create the command line
00039    
00040   //~ //fn-fileget -c "`cmsGetFnConnect frontier://smallfiles`" slc5_ia32_gcc434/sherpa/1.2.2-cms3/8TeV/EWK/sherpa_8TeV_ewk_Zleptons5jetsincl_50_mll_8000_MASTER.tgz 
00041   if(FetchSherpack == true){
00042                 std::cout << "SherpackFetcher: Trying to fetch the Sherpack " << sherpack << std::endl;
00043                 int res =FnFileGet(path);
00044                 if (res!=1)  {
00045                                 std::cout <<"SherpackFetcher: Fetching of Sherpack did not succeed, terminating" << std::endl;
00046                                 return -1;
00047                 }
00048                 std::cout << "SherpackFetcher: Fetching successful" << std::endl;
00049   }
00050   
00051   std::ifstream my_file(sherpack.c_str());
00052         if (!my_file.good())
00053         {
00054       std::cout << "SherpackFetcher: No Sherpack found" << std::endl;
00055       return -2;
00056         }
00057   my_file.close();
00058   std::cout << "SherpackFetcher: Sherpack found" << std::endl;
00059   
00060   if(SherpackChecksum!=""){
00061           char md5checksum[33]; 
00062           spu::md5_File(sherpack, md5checksum);
00063           for (int k=0; k<33; k++){
00064                 if (md5checksum[k]!= SherpackChecksum[k]) {
00065                                 std::cout << "SherpackFetcher: failure, calculated and specified checksums differ!" << std::endl;
00066                                 return -3;
00067                 }
00068           }
00069           std::cout << "SherpackFetcher: Calculated checksum of the Sherpack is " << md5checksum << " and matches" << std::endl;
00070   } else {
00071           std::cout << "SherpackFetcher: Ignoring Checksum" << std::endl;
00072   }
00073   
00074   
00075   std::cout << "SherpackFetcher: Trying to unzip the Sherpack" << std::endl;
00076   int res=spu::Unzip(sherpack,sherpackunzip);
00077   if (res!=0) {
00078                 std::cout << "SherpackFetcher: Decompressing failed " << std::endl;
00079                 return -4;
00080   }
00081   std::cout << "SherpackFetcher: Decompressing successful " << std::endl;
00082   
00083   FILE *file = fopen(const_cast<char*>(sherpackunzip.c_str()),"r");
00084   if( file ) {
00085                 std::cout << "SherpackFetcher: Decompressed Sherpack exists with name " << sherpackunzip << " starting to untar it"<<std::endl;                 
00086                 spu::Untar(file,SherpaPath.c_str());
00087   } else {
00088                 std::cout << "SherpackFetcher: Could not open decompressed Sherpack" << std::endl;
00089                 return -5;
00090   }
00091   fclose(file);
00092   return 0;
00093 }
00094 
00095 int SherpackFetcher::FnFileGet(std::string pathstring)
00096 {
00097   int ec;
00098   unsigned long channel;
00099   FrontierConfig *config;
00100   
00101   std::string connectstr="";
00102   try {
00103           std::auto_ptr<edm::SiteLocalConfig> slcptr(new edm::service::SiteLocalConfigService(edm::ParameterSet()));
00104           boost::shared_ptr<edm::serviceregistry::ServiceWrapper<edm::SiteLocalConfig> > slc(new edm::serviceregistry::ServiceWrapper<edm::SiteLocalConfig>(slcptr));
00105           edm::ServiceToken slcToken = edm::ServiceRegistry::createContaining(slc);
00106           edm::ServiceRegistry::Operate operate(slcToken);
00107 
00108           edm::Service<edm::SiteLocalConfig> localconfservice;
00109       localconfservice->lookupCalibConnect("frontier://smallfiles");
00110       connectstr=localconfservice->lookupCalibConnect("frontier://smallfiles");
00111   } catch(cms::Exception const& e) {
00112         std::cerr << e.explainSelf() << std::endl;
00113     return 2;
00114   }
00115 
00116   if(frontier_init(malloc,free)!=0)
00117    {
00118     fprintf(stderr,"Error initializing frontier client: %s\n",frontier_getErrorMsg());
00119     return 2;
00120    }
00121   ec=FRONTIER_OK;
00122   //~ config=frontierConfig_get(connectstr,"",&ec); 
00123   config=frontierConfig_get(connectstr.c_str(),"",&ec); 
00124   if(ec!=FRONTIER_OK)
00125    {
00126     fprintf(stderr,"Error getting frontierConfig object: %s\n",frontier_getErrorMsg());
00127     return 2;
00128    }
00129   channel=frontier_createChannel2(config,&ec);
00130   if(ec!=FRONTIER_OK)
00131    {
00132     fprintf(stderr,"Error creating frontier channel: %s\n",frontier_getErrorMsg());
00133     return 2;
00134    }
00135 
00136  
00137     char uribuf[4096];
00138     FrontierRSBlob *frsb;
00139     int fd;
00140     int n;
00141     char *p;
00142     const char *localname;
00143     
00144         const char *path=pathstring.c_str();
00145     snprintf(uribuf,sizeof(uribuf)-1,           "Frontier/type=frontier_file:1:DEFAULT&encoding=BLOB&p1=%s",path);
00146     ec=frontier_getRawData(channel,uribuf);
00147     if(ec!=FRONTIER_OK)
00148      {
00149       fprintf(stderr,"Error getting data for %s: %s\n",path,frontier_getErrorMsg());
00150       return 3;
00151      }
00152     frsb=frontierRSBlob_open(channel,0,1,&ec);
00153     if(ec!=FRONTIER_OK)
00154      {
00155       fprintf(stderr,"Error opening result blob for %s: %s\n",path,frontier_getErrorMsg());
00156       return 3;
00157      }
00158     // ignore the result type, will always be an array
00159     (void)frontierRSBlob_getByte(frsb,&ec);
00160     if(ec!=FRONTIER_OK)
00161      {
00162       fprintf(stderr,"Error getting result type for %s: %s\n",path,frontier_getErrorMsg());
00163       return 3;
00164      }
00165     n=frontierRSBlob_getInt(frsb,&ec);
00166     if(ec!=FRONTIER_OK)
00167      {
00168       fprintf(stderr,"Error getting result size for %s: %s\n",path,frontier_getErrorMsg());
00169       return 3;
00170      }
00171     p=frontierRSBlob_getByteArray(frsb,n,&ec);
00172     if(ec!=FRONTIER_OK)
00173      {
00174       fprintf(stderr,"Error getting result data for %s: %s\n",path,frontier_getErrorMsg());
00175       return 3;
00176      }
00177     localname=strrchr(path,'/');
00178     if(localname==NULL)
00179                 localname=pathstring.c_str();
00180     else
00181       localname++;
00182     fd=open(localname,O_CREAT|O_TRUNC|O_WRONLY,0666);
00183     if(fd==-1)
00184      {
00185       fprintf(stderr,"Error creating %s: %s\n",localname,strerror(errno));
00186       ec=-1;
00187       return 3;
00188      }
00189     if(write(fd,p,n)<0)
00190      {
00191       fprintf(stderr,"Error writing to %s: %s\n",localname,strerror(errno));
00192       ec=-1;
00193       close(fd);
00194       return 3;
00195      }
00196     close(fd);
00197     printf("%d bytes written to %s\n",n,localname);
00198     frontierRSBlob_close(frsb,&ec);
00199    
00200 
00201   frontier_closeChannel(channel);
00202 
00203   return (ec==FRONTIER_OK);
00204 }
00205 
00206 SherpackFetcher::~SherpackFetcher()
00207 {
00208 }
00209 
00210 
00211 
00212 } // end of namespace definition
00213 
00214 //~ using spf::SherpackFetcher;
00215 //~ DEFINE_FWK_MODULE(SherpackFetcher);