CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Alignment/CommonAlignmentProducer/bin/aligncond_split_iov.cc

Go to the documentation of this file.
00001 #include "CondCore/DBCommon/interface/DbSession.h"
00002 #include "CondCore/DBCommon/interface/DbScopedTransaction.h"
00003 #include "CondCore/DBCommon/interface/DbTransaction.h"
00004 #include "CondCore/DBCommon/interface/Exception.h"
00005 
00006 #include "CondCore/MetaDataService/interface/MetaData.h"
00007 #include "CondCore/IOVService/interface/IOVEditor.h"
00008 
00009 #include "CondCore/DBCommon/interface/IOVInfo.h"
00010 #include "CondCore/DBCommon/interface/Logger.h"
00011 #include "CondCore/DBCommon/interface/LogDBEntry.h"
00012 #include "CondCore/DBCommon/interface/UserLogInfo.h"
00013 #include "CondCore/DBCommon/interface/TagInfo.h"
00014 #include "CondCore/Utilities/interface/Utilities.h"
00015 
00016 #include "CondCore/ORA/interface/Object.h"
00017 #include "CondCore/DBCommon/interface/DbTransaction.h"
00018 #include "CondCore/DBCommon/interface/Exception.h"
00019 #include "CondCore/MetaDataService/interface/MetaData.h"
00020 
00021 #include "CondCore/DBCommon/interface/Time.h"
00022 #include "CondFormats/Common/interface/TimeConversions.h"
00023 
00024 #include "CondCore/IOVService/interface/IOVProxy.h"
00025 
00026 #include "CondFormats/Alignment/interface/Alignments.h"
00027 #include "CondFormats/Alignment/interface/AlignmentErrors.h"
00028 #include "CondFormats/Alignment/interface/AlignmentSurfaceDeformations.h"
00029 
00030 #include <iterator>
00031 #include <iostream>
00032 #include <sstream>
00033 
00034 namespace cond {
00035   class AlignSplitIOV : public Utilities {
00036   public:
00037     AlignSplitIOV();
00038     ~AlignSplitIOV();
00039     int execute();
00040 
00041     template<class T>
00042     std::string processPayloadContainer(cond::DbSession &sourcedb,
00043                                         cond::DbSession &destdb, 
00044                                         const std::string &token,
00045                                         const std::string &containerName);
00046   };
00047 }
00048 
00049 cond::AlignSplitIOV::AlignSplitIOV()
00050  :Utilities("aligncond_split_iov")
00051 {
00052   addConnectOption("sourceConnect","s","source connection string(required)");
00053   addConnectOption("destConnect","d","destionation connection string(required)");
00054   addOption<std::string>("sourceTag","i","tag to export( default = destination tag)");
00055   addOption<std::string>("destTag","t","destination tag (required)");
00056   addAuthenticationOptions();
00057   addOption<bool>("verbose","v","verbose");
00058 }
00059 
00060 cond::AlignSplitIOV::~AlignSplitIOV()
00061 {
00062 
00063 }
00064 
00065 int cond::AlignSplitIOV::execute()
00066 {
00067   initializePluginManager();
00068  
00069   std::string sourceConnect = getOptionValue<std::string>("sourceConnect");
00070   std::string destConnect = getOptionValue<std::string>("destConnect");
00071   
00072   std::string destTag = getOptionValue<std::string>("destTag");
00073   std::string sourceTag(destTag);
00074   if (hasOptionValue("sourceTag"))
00075     sourceTag = getOptionValue<std::string>("sourceTag");
00076   bool verbose = hasOptionValue("verbose");
00077   
00078   cond::Time_t since = std::numeric_limits<cond::Time_t>::min();
00079   cond::Time_t till = std::numeric_limits<cond::Time_t>::max();
00080 
00081   std::string sourceiovtoken;
00082   cond::TimeType sourceiovtype;
00083 
00084   std::string destiovtoken;
00085 
00086   cond::DbSession sourcedb = openDbSession("sourceConnect", false);
00087   cond::DbSession destdb = openDbSession("destConnect");
00088 
00089   sourcedb.transaction().start(false);
00090   cond::MetaData sourceMetadata(sourcedb);
00091   sourceiovtoken = sourceMetadata.getToken(sourceTag);
00092   if (sourceiovtoken.empty()) 
00093     throw std::runtime_error(std::string("tag ") + sourceTag + std::string(" not found"));
00094 
00095   if (verbose)
00096     std::cout << "source iov token: " << sourceiovtoken << std::endl;
00097 
00098   cond::IOVProxy iov(sourcedb, sourceiovtoken);
00099   sourceiovtype = iov.timetype();
00100   if (verbose)
00101     std::cout << "source iov type " << sourceiovtype << std::endl;
00102 
00103   since = std::max(since, cond::timeTypeSpecs[sourceiovtype].beginValue);
00104   till  = std::min(till,  cond::timeTypeSpecs[sourceiovtype].endValue);
00105   
00106   unsigned int counter = 0;
00107   for (cond::IOVProxy::const_iterator ioviterator = iov.begin();
00108        ioviterator != iov.end();
00109        ++ioviterator) {
00110 
00111     std::stringstream newTag;
00112     newTag << destTag << "_" << counter;
00113     
00114     std::cout << "iov " << counter << ":\t" 
00115               << ioviterator->since() << " \t "
00116               << ioviterator->till() << std::endl;
00117     
00118     if (verbose)
00119       std::cout << "\t" << ioviterator->token() << std::endl;
00120 
00121     cond::DbScopedTransaction transaction(destdb);
00122     transaction.start(false);
00123     std::string payloadContainerName = sourcedb.classNameForItem(ioviterator->token());
00124     std::string objToken;
00125     if (payloadContainerName=="Alignments")
00126       objToken = processPayloadContainer<Alignments>(sourcedb, destdb, 
00127                                                      ioviterator->token(), payloadContainerName);
00128     else if (payloadContainerName=="AlignmentErrors")
00129       objToken = processPayloadContainer<AlignmentErrors>(sourcedb, destdb,
00130                                                           ioviterator->token(), payloadContainerName);
00131     else if (payloadContainerName=="AlignmentSurfaceDeformations")
00132       objToken = processPayloadContainer<AlignmentSurfaceDeformations>(sourcedb, destdb,
00133                                                                        ioviterator->token(), payloadContainerName);
00134     else {
00135       return 1;
00136     }
00137 
00138     cond::IOVEditor editor(destdb);
00139     editor.create(iov.timetype(), cond::timeTypeSpecs[sourceiovtype].endValue);
00140     editor.append(cond::timeTypeSpecs[sourceiovtype].beginValue, objToken);
00141     std::string iovToken = editor.token();
00142     editor.stamp(cond::userInfo(),false);
00143  
00144     cond::MetaData metadata(destdb);
00145     metadata.addMapping(newTag.str(), iovToken, sourceiovtype);
00146     transaction.commit();
00147     
00148     ::sleep(1);
00149 
00150     ++counter;
00151   }
00152   
00153   std::cout << "Total # of payload objects: " << counter << std::endl;
00154   
00155   return 0;
00156 }
00157 
00158 template<class T>
00159 std::string cond::AlignSplitIOV::processPayloadContainer(cond::DbSession &sourcedb,
00160                                                          cond::DbSession &destdb, 
00161                                                          const std::string &token,
00162                                                          const std::string &containerName)
00163 {
00164   boost::shared_ptr<T> object = sourcedb.getTypedObject<T>(token);
00165   destdb.createDatabase();
00166   return destdb.storeObject(object.get(), containerName);
00167 }
00168 
00169 int main( int argc, char** argv )
00170 {
00171   cond::AlignSplitIOV utilities;
00172   return utilities.run(argc,argv);
00173 }