CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_1/src/Alignment/CommonAlignmentAlgorithm/plugins/ApeSettingAlgorithm.cc

Go to the documentation of this file.
00001 
00009 /*
00010  *# Parameters:
00011  *#    saveApeToASCII -- Do we write out an APE text file?
00012  *#    saveComposites -- Do we write APEs for composite detectors?
00013  *#    saveLocalNotGlobal -- Do we write the APEs in the local or global coordinates?
00014  *#    apeASCIISaveFile -- The name of the save-file.
00015  *#    readApeFromASCII -- Do we read in APEs from a text file?
00016  *#    readLocalNotGlobal -- Do we read APEs in the local or the global frame?
00017  *#    readFullLocalMatrix -- Do we read the full local matrix or just the diagonal elements?
00018  *#                        -- Always write full matrix
00019  *# Full matrix format: DetID dxx dxy dyy dxz dyz dzz
00020  *# Diagonal element format: DetID sqrt(dxx) sqrt(dyy) sqrt(dzz)
00021  *#    setComposites -- Do we set the APEs for composite detectors or just ignore them?
00022  *#    apeASCIIReadFile -- Input file name.
00023  *# Also note:
00024  *#    process.AlignmentProducer.saveApeToDB -- to save as an sqlite file
00025  *# and associated entries in _cfg.py
00026  */
00027 
00028 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmPluginFactory.h"
00029 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentAlgorithmBase.h"
00030 #include "Alignment/CommonAlignment/interface/AlignableModifier.h"
00031 
00032 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00033 
00034 #include "CondFormats/Alignment/interface/AlignmentErrors.h" 
00035 #include "DataFormats/GeometrySurface/interface/GloballyPositioned.h"
00036 #include "CLHEP/Matrix/SymMatrix.h"
00037 
00038 #include <fstream>
00039 #include <string>
00040 #include <set>
00041 
00042 #include "DataFormats/TrackingRecHit/interface/AlignmentPositionError.h"
00043 #include "Alignment/CommonAlignmentAlgorithm/interface/AlignmentParameterStore.h"
00044 
00045 #include "Alignment/CommonAlignment/interface/AlignableNavigator.h"
00046 #include "Alignment/CommonAlignment/interface/AlignableDetOrUnitPtr.h"
00047 #include "Alignment/CommonAlignment/interface/Alignable.h"
00048 
00049 // includes to make known that they inherit from Alignable:
00050 #include "Alignment/TrackerAlignment/interface/AlignableTracker.h"
00051 #include "Alignment/MuonAlignment/interface/AlignableMuon.h"
00052 #include "Alignment/CommonAlignment/interface/AlignableExtras.h"
00053 
00054 #include "DataFormats/CLHEP/interface/AlgebraicObjects.h"
00055 
00056 class ApeSettingAlgorithm : public AlignmentAlgorithmBase
00057 {
00058  public:
00060   ApeSettingAlgorithm(const edm::ParameterSet &cfg);
00061 
00063   virtual ~ApeSettingAlgorithm();
00064 
00066   virtual void initialize(const edm::EventSetup &setup, 
00067                           AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras,
00068                           AlignmentParameterStore *store);
00069 
00071   virtual void terminate();
00072 
00074   virtual void run(const edm::EventSetup &setup, const EventInfo &eventInfo);
00075 
00076  private:
00077   edm::ParameterSet         theConfig;
00078   AlignableNavigator       *theAlignableNavigator;
00079   AlignableTracker         *theTracker;
00080   bool                     saveApeToAscii_,readApeFromAscii_,readFullLocalMatrix_;
00081   bool                     readLocalNotGlobal_,saveLocalNotGlobal_;
00082   bool                     setComposites_,saveComposites_;
00083 };
00084 
00085 //____________________________________________________
00086 //____________________________________________________
00087 //____________________________________________________
00088 //____________________________________________________
00089 
00090 
00091 // Constructor ----------------------------------------------------------------
00092 //____________________________________________________
00093 ApeSettingAlgorithm::ApeSettingAlgorithm(const edm::ParameterSet &cfg) :
00094   AlignmentAlgorithmBase(cfg), theConfig(cfg),
00095   theAlignableNavigator(0)
00096 {
00097   edm::LogInfo("Alignment") << "@SUB=ApeSettingAlgorithm" << "Start.";
00098   saveApeToAscii_ = theConfig.getUntrackedParameter<bool>("saveApeToASCII");
00099   saveComposites_ = theConfig.getUntrackedParameter<bool>("saveComposites");
00100   saveLocalNotGlobal_ = theConfig.getUntrackedParameter<bool>("saveLocalNotGlobal");
00101   readApeFromAscii_ = theConfig.getParameter<bool>("readApeFromASCII");
00102   readLocalNotGlobal_ = theConfig.getParameter<bool>("readLocalNotGlobal");
00103   readFullLocalMatrix_ = theConfig.getParameter<bool>("readFullLocalMatrix");
00104   setComposites_ = theConfig.getParameter<bool>("setComposites");
00105   
00106 }
00107 
00108 // Destructor ----------------------------------------------------------------
00109 //____________________________________________________
00110 ApeSettingAlgorithm::~ApeSettingAlgorithm()
00111 {
00112   delete theAlignableNavigator;
00113 }
00114 
00115 // Call at beginning of job ---------------------------------------------------
00116 //____________________________________________________
00117 void ApeSettingAlgorithm::initialize(const edm::EventSetup &setup, 
00118                                      AlignableTracker *tracker, AlignableMuon *muon, AlignableExtras *extras,
00119                                      AlignmentParameterStore *store)
00120 {
00121  theAlignableNavigator = new AlignableNavigator(tracker, muon);
00122  theTracker = tracker;
00123  
00124  if (readApeFromAscii_)
00125    { std::ifstream apeReadFile(theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath().c_str()); //requires <fstream>
00126    if (!apeReadFile.good())
00127      { edm::LogInfo("Alignment") << "@SUB=initialize" <<"Problem opening APE file: skipping"
00128                                  << theConfig.getParameter<edm::FileInPath>("apeASCIIReadFile").fullPath();
00129      return;
00130      }
00131    std::set<int> apeList; //To avoid duplicates
00132    while (!apeReadFile.eof())
00133      { int apeId=0; double x11,x21,x22,x31,x32,x33;
00134      if (!readLocalNotGlobal_ || readFullLocalMatrix_) 
00135        { apeReadFile>>apeId>>x11>>x21>>x22>>x31>>x32>>x33>>std::ws;}
00136      else
00137        { apeReadFile>>apeId>>x11>>x22>>x33>>std::ws;}
00138      //idr What sanity checks do we need to put here?
00139      if (apeId != 0) //read appears valid?
00140        { if (apeList.find(apeId) == apeList.end()) //Not previously done
00141          {  DetId id(apeId);
00142          AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(id)); //NULL if none
00143          if (alidet)
00144            { if ((alidet->components().size()<1) || setComposites_) //the problem with glued dets...
00145              { GlobalError globErr;
00146              if (readLocalNotGlobal_)
00147                { AlgebraicSymMatrix33 as; 
00148                if (readFullLocalMatrix_)
00149                  { as[0][0]=x11; as[1][0]=x21; as[1][1]=x22;
00150                  as[2][0]=x31; as[2][1]=x32; as[2][2]=x33;
00151                  }
00152                else
00153                  { as[0][0]=x11*x11; as[1][1]=x22*x22; as[2][2]=x33*x33;} //local cov.
00154                align::RotationType rt=alidet->globalRotation();
00155                AlgebraicMatrix33 am;
00156                am[0][0]=rt.xx(); am[0][1]=rt.xy(); am[0][2]=rt.xz();
00157                am[1][0]=rt.yx(); am[1][1]=rt.yy(); am[1][2]=rt.yz();
00158                am[2][0]=rt.zx(); am[2][1]=rt.zy(); am[2][2]=rt.zz();
00159                globErr = GlobalError(ROOT::Math::SimilarityT(am,as));
00160                }
00161              else
00162                {
00163                  globErr = GlobalError(x11,x21,x22,x31,x32,x33);
00164                }
00165              alidet->setAlignmentPositionError(globErr, false); // do not propagate down!
00166              apeList.insert(apeId); //Flag it's been set
00167              }
00168            else
00169              { edm::LogInfo("Alignment") << "@SUB=initialize" << "Not Setting APE for Composite DetId "<<apeId;
00170              }
00171            }
00172          }
00173        else
00174          { edm::LogInfo("Alignment") << "@SUB=initialize" << "Skipping duplicate APE for DetId "<<apeId;
00175          }
00176        }
00177      }
00178    apeReadFile.close();
00179    edm::LogInfo("Alignment") << "@SUB=initialize" << "Set "<<apeList.size()<<" APE values.";
00180    }
00181 }
00182  
00183 
00184 // Call at end of job ---------------------------------------------------------
00185 //____________________________________________________
00186 void ApeSettingAlgorithm::terminate()
00187 {
00188   if (saveApeToAscii_)
00189     { AlignmentErrors* aliErr=theTracker->alignmentErrors();
00190     int theSize=aliErr->m_alignError.size();
00191     std::ofstream apeSaveFile(theConfig.getUntrackedParameter<std::string>("apeASCIISaveFile").c_str()); //requires <fstream>
00192     for (int i=0; i < theSize; ++i)
00193       { int id= aliErr->m_alignError[i].rawId();
00194       AlignableDetOrUnitPtr alidet(theAlignableNavigator->alignableFromDetId(DetId(id))); //NULL if none
00195       if (alidet && ((alidet->components().size()<1) || saveComposites_))
00196         { apeSaveFile<<id;
00197         CLHEP::HepSymMatrix sm = aliErr->m_alignError[i].matrix();
00198         if (saveLocalNotGlobal_)
00199           { align::RotationType rt=alidet->globalRotation();
00200           AlgebraicMatrix am(3,3);
00201           am[0][0]=rt.xx(); am[0][1]=rt.xy(); am[0][2]=rt.xz();
00202           am[1][0]=rt.yx(); am[1][1]=rt.yy(); am[1][2]=rt.yz();
00203           am[2][0]=rt.zx(); am[2][1]=rt.zy(); am[2][2]=rt.zz();
00204           sm=sm.similarity(am); //symmetric matrix
00205           } //transform to local
00206         for (int j=0; j < 3; ++j)
00207           for (int k=0; k <= j; ++k)
00208             apeSaveFile<<"  "<<sm[j][k]; //always write full matrix
00209         
00210         apeSaveFile<<std::endl;
00211         }
00212       }
00213     delete aliErr;
00214     apeSaveFile.close();
00215     }
00216   // clean up at end:  // FIXME: should we delete here or in destructor?
00217   delete theAlignableNavigator;
00218   theAlignableNavigator = 0;
00219 }
00220 
00221 // Run the algorithm on trajectories and tracks -------------------------------
00222 //____________________________________________________
00223 void ApeSettingAlgorithm::run(const edm::EventSetup &setup, const EventInfo &eventInfo)
00224 {
00225   // nothing to do here?
00226 }
00227 
00228 // Plugin definition for the algorithm
00229 DEFINE_EDM_PLUGIN(AlignmentAlgorithmPluginFactory,
00230                    ApeSettingAlgorithm, "ApeSettingAlgorithm");
00231 
00232 
00233