CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CalibTracker/SiPixelLorentzAngle/src/SiPixelLorentzAngle.cc

Go to the documentation of this file.
00001 
00002 #include <memory>
00003 #include <string>
00004 #include <iostream>
00005 #include <fstream>
00006 #include <TMath.h>
00007 #include "CalibTracker/SiPixelLorentzAngle/interface/SiPixelLorentzAngle.h"
00008 
00009 #include "DataFormats/TrajectorySeed/interface/TrajectorySeedCollection.h"
00010 #include "FWCore/Framework/interface/ESHandle.h"
00011 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
00012 #include "DataFormats/GeometryVector/interface/LocalVector.h"
00013 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00014 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
00015 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00016 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHit.h"
00017 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00018 #include "DataFormats/SiStripDetId/interface/StripSubdetector.h"
00019 #include "DataFormats/TrackerRecHit2D/interface/SiStripMatchedRecHit2D.h"
00020 #include "Geometry/CommonTopologies/interface/StripTopology.h"
00021 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00022 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00023 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00024 
00025 #include "TrackingTools/Records/interface/TransientRecHitRecord.h"
00026 #include "Geometry/TrackerGeometryBuilder/interface/GluedGeomDet.h"
00027 #include "TrackingTools/TrackFitters/interface/TrajectoryStateCombiner.h"
00028 #include "TrackingTools/PatternTools/interface/TrajTrackAssociation.h"
00029 #include "TrackingTools/TransientTrack/interface/TransientTrack.h"
00030 int lower_bin_;
00031 
00032 using namespace std;
00033 using namespace edm;
00034 using namespace reco;
00035 
00036 SiPixelLorentzAngle::SiPixelLorentzAngle(edm::ParameterSet const& conf) : 
00037   conf_(conf), filename_(conf.getParameter<std::string>("fileName")), filenameFit_(conf.getParameter<std::string>("fileNameFit")), ptmin_(conf.getParameter<double>("ptMin")), simData_(conf.getParameter<bool>("simData")),    normChi2Max_(conf.getParameter<double>("normChi2Max")), clustSizeYMin_(conf.getParameter<int>("clustSizeYMin")), residualMax_(conf.getParameter<double>("residualMax")), clustChargeMax_(conf.getParameter<double>("clustChargeMax")),hist_depth_(conf.getParameter<int>("binsDepth")), hist_drift_(conf.getParameter<int>("binsDrift"))
00038 {
00039   //    anglefinder_=new  TrackLocalAngle(conf);
00040   hist_x_ = 50;
00041   hist_y_ = 100;
00042   min_x_ = -500.;
00043   max_x_ = 500.;
00044   min_y_ = -1500.;
00045   max_y_ = 500.;
00046   width_ = 0.0285;
00047   min_depth_ = -100.;
00048   max_depth_ = 400.;
00049   min_drift_ = -1000.; //-200.;(conf.getParameter<double>("residualMax"))
00050   max_drift_ = 1000.; //400.;
00051 
00052 }
00053 
00054 // Virtual destructor needed.
00055 SiPixelLorentzAngle::~SiPixelLorentzAngle() {  }  
00056 
00057 void SiPixelLorentzAngle::beginJob()
00058 {
00059 
00060   //    cout << "started SiPixelLorentzAngle" << endl;
00061   hFile_ = new TFile (filename_.c_str(), "RECREATE" );
00062   int bufsize = 64000;
00063   // create tree structure
00064   SiPixelLorentzAngleTree_ = new TTree("SiPixelLorentzAngleTree_","SiPixel LorentzAngle tree", bufsize);
00065   SiPixelLorentzAngleTree_->Branch("run", &run_, "run/I", bufsize);
00066   SiPixelLorentzAngleTree_->Branch("event", &event_, "event/I", bufsize);
00067   SiPixelLorentzAngleTree_->Branch("module", &module_, "module/I", bufsize);
00068   SiPixelLorentzAngleTree_->Branch("ladder", &ladder_, "ladder/I", bufsize);
00069   SiPixelLorentzAngleTree_->Branch("layer", &layer_, "layer/I", bufsize);
00070   SiPixelLorentzAngleTree_->Branch("isflipped", &isflipped_, "isflipped/I", bufsize);
00071   SiPixelLorentzAngleTree_->Branch("pt", &pt_, "pt/F", bufsize);
00072   SiPixelLorentzAngleTree_->Branch("eta", &eta_, "eta/F", bufsize);
00073   SiPixelLorentzAngleTree_->Branch("phi", &phi_, "phi/F", bufsize);
00074   SiPixelLorentzAngleTree_->Branch("chi2", &chi2_, "chi2/D", bufsize);
00075   SiPixelLorentzAngleTree_->Branch("ndof", &ndof_, "ndof/D", bufsize);
00076   SiPixelLorentzAngleTree_->Branch("trackhit", &trackhit_, "x/F:y/F:alpha/D:beta/D:gamma_/D", bufsize);
00077   SiPixelLorentzAngleTree_->Branch("simhit", &simhit_, "x/F:y/F:alpha/D:beta/D:gamma_/D", bufsize);
00078   SiPixelLorentzAngleTree_->Branch("npix", &pixinfo_.npix, "npix/I", bufsize);
00079   SiPixelLorentzAngleTree_->Branch("rowpix", pixinfo_.row, "row[npix]/F", bufsize);
00080   SiPixelLorentzAngleTree_->Branch("colpix", pixinfo_.col, "col[npix]/F", bufsize);
00081   SiPixelLorentzAngleTree_->Branch("adc", pixinfo_.adc, "adc[npix]/F", bufsize);
00082   SiPixelLorentzAngleTree_->Branch("xpix", pixinfo_.x, "x[npix]/F", bufsize);
00083   SiPixelLorentzAngleTree_->Branch("ypix", pixinfo_.y, "y[npix]/F", bufsize);
00084   SiPixelLorentzAngleTree_->Branch("clust", &clust_, "x/F:y/F:charge/F:size_x/I:size_y/I:maxPixelCol/I:maxPixelRow:minPixelCol/I:minPixelRow/I", bufsize);
00085   SiPixelLorentzAngleTree_->Branch("rechit", &rechit_, "x/F:y/F", bufsize);
00086         
00087   SiPixelLorentzAngleTreeForward_ = new TTree("SiPixelLorentzAngleTreeForward_","SiPixel LorentzAngle tree forward", bufsize);
00088   SiPixelLorentzAngleTreeForward_->Branch("run", &run_, "run/I", bufsize);
00089   SiPixelLorentzAngleTreeForward_->Branch("event", &event_, "event/I", bufsize);
00090   SiPixelLorentzAngleTreeForward_->Branch("side", &sideF_, "side/I", bufsize);
00091   SiPixelLorentzAngleTreeForward_->Branch("disk", &diskF_, "disk/I", bufsize);
00092   SiPixelLorentzAngleTreeForward_->Branch("blade", &bladeF_, "blade/I", bufsize);
00093   SiPixelLorentzAngleTreeForward_->Branch("panel", &panelF_, "panel/I", bufsize);
00094   SiPixelLorentzAngleTreeForward_->Branch("module", &moduleF_, "module/I", bufsize);
00095   SiPixelLorentzAngleTreeForward_->Branch("pt", &pt_, "pt/F", bufsize);
00096   SiPixelLorentzAngleTreeForward_->Branch("eta", &eta_, "eta/F", bufsize);
00097   SiPixelLorentzAngleTreeForward_->Branch("phi", &phi_, "phi/F", bufsize);
00098   SiPixelLorentzAngleTreeForward_->Branch("chi2", &chi2_, "chi2/D", bufsize);
00099   SiPixelLorentzAngleTreeForward_->Branch("ndof", &ndof_, "ndof/D", bufsize);
00100   SiPixelLorentzAngleTreeForward_->Branch("trackhit", &trackhitF_, "x/F:y/F:alpha/D:beta/D:gamma_/D", bufsize);
00101   SiPixelLorentzAngleTreeForward_->Branch("simhit", &simhitF_, "x/F:y/F:alpha/D:beta/D:gamma_/D", bufsize);
00102   SiPixelLorentzAngleTreeForward_->Branch("npix", &pixinfoF_.npix, "npix/I", bufsize);
00103   SiPixelLorentzAngleTreeForward_->Branch("rowpix", pixinfoF_.row, "row[npix]/F", bufsize);
00104   SiPixelLorentzAngleTreeForward_->Branch("colpix", pixinfoF_.col, "col[npix]/F", bufsize);
00105   SiPixelLorentzAngleTreeForward_->Branch("adc", pixinfoF_.adc, "adc[npix]/F", bufsize);
00106   SiPixelLorentzAngleTreeForward_->Branch("xpix", pixinfoF_.x, "x[npix]/F", bufsize);
00107   SiPixelLorentzAngleTreeForward_->Branch("ypix", pixinfoF_.y, "y[npix]/F", bufsize);
00108   SiPixelLorentzAngleTreeForward_->Branch("clust", &clustF_, "x/F:y/F:charge/F:size_x/I:size_y/I:maxPixelCol/I:maxPixelRow:minPixelCol/I:minPixelRow/I", bufsize);
00109   SiPixelLorentzAngleTreeForward_->Branch("rechit", &rechitF_, "x/F:y/F", bufsize);
00110         
00111         
00112   //book histograms
00113   char name[128];
00114   for(int i_module = 1; i_module<=8; i_module++){
00115     for(int i_layer = 1; i_layer<=3; i_layer++){
00116       sprintf(name, "h_drift_depth_adc_layer%i_module%i", i_layer, i_module); 
00117       _h_drift_depth_adc_[i_module + (i_layer -1) * 8] = new TH2F(name,name,hist_drift_ , min_drift_, max_drift_, hist_depth_, min_depth_, max_depth_);
00118       sprintf(name, "h_drift_depth_adc2_layer%i_module%i", i_layer, i_module); 
00119       _h_drift_depth_adc2_[i_module + (i_layer -1) * 8] = new TH2F(name,name,hist_drift_ , min_drift_, max_drift_, hist_depth_, min_depth_, max_depth_);
00120       sprintf(name, "h_drift_depth_noadc_layer%i_module%i", i_layer, i_module); 
00121       _h_drift_depth_noadc_[i_module + (i_layer -1) * 8] = new TH2F(name,name,hist_drift_ , min_drift_, max_drift_, hist_depth_, min_depth_, max_depth_);
00122       sprintf(name, "h_drift_depth_layer%i_module%i", i_layer, i_module); 
00123       _h_drift_depth_[i_module + (i_layer -1) * 8] = new TH2F(name,name,hist_drift_ , min_drift_, max_drift_, hist_depth_, min_depth_, max_depth_);
00124       sprintf(name, "h_mean_layer%i_module%i", i_layer, i_module); 
00125       _h_mean_[i_module + (i_layer -1) * 8] = new TH1F(name,name,hist_depth_, min_depth_, max_depth_);
00126     }
00127   }
00128         
00129   // just for some expaining plots
00130   h_cluster_shape_adc_  = new TH2F("h_cluster_shape_adc","cluster shape with adc weight", hist_x_, min_x_, max_x_, hist_y_, min_y_, max_y_);
00131   h_cluster_shape_noadc_  = new TH2F("h_cluster_shape_noadc","cluster shape without adc weight", hist_x_, min_x_, max_x_, hist_y_, min_y_, max_y_);
00132   h_cluster_shape_  = new TH2F("h_cluster_shape","cluster shape", hist_x_, min_x_, max_x_, hist_y_, min_y_, max_y_);
00133   h_cluster_shape_adc_rot_  = new TH2F("h_cluster_shape_adc_rot","cluster shape with adc weight", hist_x_, min_x_, max_x_, hist_y_, -max_y_, -min_y_);
00134   h_cluster_shape_noadc_rot_  = new TH2F("h_cluster_shape_noadc_rot","cluster shape without adc weight", hist_x_, min_x_, max_x_, hist_y_, -max_y_, -min_y_);
00135   h_cluster_shape_rot_  = new TH2F("h_cluster_shape_rot","cluster shape", hist_x_, min_x_, max_x_, hist_y_, -max_y_, -min_y_);
00136   h_tracks_ = new TH1F("h_tracks","h_tracks",2,0.,2.);
00137   event_counter_ = 0;
00138   trackEventsCounter_ = 0;
00139   //    trackcounter_ = 0;
00140   hitCounter_ = 0;
00141   usedHitCounter_ = 0;
00142   pixelTracksCounter_ = 0;
00143 //   edm::ESHandle<TrackerGeometry> estracker; //this block should not be in beginJob()
00144 //   c.get<TrackerDigiGeometryRecord>().get(estracker);
00145 //   tracker=&(* estracker);
00146 }
00147 
00148 
00149 // Functions that gets called by framework every event
00150 void SiPixelLorentzAngle::analyze(const edm::Event& e, const edm::EventSetup& es)
00151 {  
00152   event_counter_++;
00153   //    if(event_counter_ % 500 == 0) cout << "event number " << event_counter_ << endl;
00154   cout << "event number " << event_counter_ << endl;
00155 
00156   edm::ESHandle<TrackerGeometry> estracker;
00157   es.get<TrackerDigiGeometryRecord>().get(estracker);
00158   tracker=&(* estracker);
00159 
00160   TrackerHitAssociator* associate;
00161   if(simData_) associate = new TrackerHitAssociator(e); else associate = 0; 
00162   // restet values
00163   module_=-1;
00164   layer_=-1;
00165   ladder_ = -1;
00166   isflipped_ = -1;
00167   pt_ = -999;
00168   eta_ = 999;
00169   phi_ = 999;
00170   pixinfo_.npix = 0;
00171 
00172   run_       = e.id().run();
00173   event_     = e.id().event();
00174         
00175   // get the association map between tracks and trajectories
00176   edm::Handle<TrajTrackAssociationCollection> trajTrackCollectionHandle;
00177   e.getByLabel(conf_.getParameter<std::string>("src"),trajTrackCollectionHandle);
00178   if(trajTrackCollectionHandle->size() >0){
00179     trackEventsCounter_++;
00180     for(TrajTrackAssociationCollection::const_iterator it = trajTrackCollectionHandle->begin(); it!=trajTrackCollectionHandle->end();++it){
00181       const Track&      track = *it->val;
00182       const Trajectory& traj  = *it->key;
00183                 
00184       // get the trajectory measurements
00185       std::vector<TrajectoryMeasurement> tmColl = traj.measurements(); 
00186       //                        TrajectoryStateOnSurface tsos = tsoscomb( itTraj->forwardPredictedState(), itTraj->backwardPredictedState() );
00187       pt_ = track.pt();
00188       eta_ = track.eta();
00189       phi_ = track.phi();
00190       chi2_ = traj.chiSquared();
00191       ndof_ = traj.ndof();
00192       if(pt_ < ptmin_) continue;
00193       // iterate over trajectory measurements
00194       std::vector<PSimHit> matched;
00195       h_tracks_->Fill(0);
00196       bool pixeltrack = false;
00197       for(std::vector<TrajectoryMeasurement>::const_iterator itTraj = tmColl.begin(); itTraj != tmColl.end(); itTraj++) {
00198         if(! itTraj->updatedState().isValid()) continue;
00199         TransientTrackingRecHit::ConstRecHitPointer recHit = itTraj->recHit();
00200         if(! recHit->isValid() || recHit->geographicalId().det() != DetId::Tracker ) continue;
00201         unsigned int subDetID = (recHit->geographicalId().subdetId());
00202         if( subDetID == PixelSubdetector::PixelBarrel || subDetID == PixelSubdetector::PixelEndcap){
00203           if(!pixeltrack){
00204             h_tracks_->Fill(1);
00205             pixelTracksCounter_++;
00206           }
00207           pixeltrack = true;
00208         }
00209         if( subDetID == PixelSubdetector::PixelBarrel){
00210                                 
00211           hitCounter_++;
00212                                 
00213           DetId detIdObj = recHit->geographicalId();
00214           const PixelGeomDetUnit * theGeomDet = dynamic_cast<const PixelGeomDetUnit*> ( tracker->idToDet(detIdObj) );
00215           if(!theGeomDet) continue;
00216 
00217           const PixelTopology * topol = &(theGeomDet->specificTopology());
00218 
00219           if(!topol) continue;
00220           PXBDetId pxbdetIdObj(detIdObj);
00221           layer_ = pxbdetIdObj.layer();
00222           ladder_ = pxbdetIdObj.ladder();
00223           module_ = pxbdetIdObj.module();
00224           float tmp1 = theGeomDet->surface().toGlobal(Local3DPoint(0.,0.,0.)).perp();
00225           float tmp2 = theGeomDet->surface().toGlobal(Local3DPoint(0.,0.,1.)).perp();
00226           if ( tmp2<tmp1 ) isflipped_ = 1;
00227           else isflipped_ = 0;
00228           const SiPixelRecHit * recHitPix = dynamic_cast<const SiPixelRecHit *>((*recHit).hit());
00229           if(!recHitPix) continue;
00230           rechit_.x  = recHitPix->localPosition().x();
00231           rechit_.y  = recHitPix->localPosition().y();
00232           SiPixelRecHit::ClusterRef const& cluster = recHitPix->cluster();      
00233         
00234           // fill entries in clust_
00235           clust_.x = (cluster)->x();
00236           clust_.y = (cluster)->y();
00237           clust_.charge = (cluster->charge())/1000.;
00238           clust_.size_x = cluster->sizeX();
00239           clust_.size_y = cluster->sizeY();
00240           clust_.maxPixelCol = cluster->maxPixelCol();
00241           clust_.maxPixelRow = cluster->maxPixelRow();
00242           clust_.minPixelCol = cluster->minPixelCol();
00243           clust_.minPixelRow = cluster->minPixelRow();
00244           // fill entries in pixinfo_:
00245           fillPix(*cluster ,topol, pixinfo_);
00246           // fill the trackhit info
00247           TrajectoryStateOnSurface tsos=itTraj->updatedState();
00248           if(!tsos.isValid()){
00249             cout << "tsos not valid" << endl;
00250             continue;   
00251           }     
00252           LocalVector trackdirection=tsos.localDirection();
00253           LocalPoint trackposition=tsos.localPosition();
00254         
00255           if(trackdirection.z()==0) continue;                           
00256           // the local position and direction
00257           trackhit_.alpha = atan2(trackdirection.z(),trackdirection.x());
00258           trackhit_.beta = atan2(trackdirection.z(),trackdirection.y());
00259           trackhit_.gamma = atan2(trackdirection.x(),trackdirection.y());
00260           trackhit_.x = trackposition.x();
00261           trackhit_.y = trackposition.y();
00262                                         
00263         
00264           // fill entries in simhit_:   
00265           if(simData_){
00266             matched.clear();        
00267             matched = associate->associateHit((*recHitPix));    
00268             float dr_start=9999.;
00269             for (std::vector<PSimHit>::iterator isim = matched.begin(); isim != matched.end(); ++isim){
00270               DetId simdetIdObj((*isim).detUnitId());
00271               if (simdetIdObj == detIdObj) {
00272                 float sim_x1 = (*isim).entryPoint().x(); // width (row index, in col direction)
00273                 float sim_y1 = (*isim).entryPoint().y(); // length (col index, in row direction)
00274                 float sim_x2 = (*isim).exitPoint().x();
00275                 float sim_y2 = (*isim).exitPoint().y();
00276                 float sim_xpos = 0.5*(sim_x1+sim_x2);
00277                 float sim_ypos = 0.5*(sim_y1+sim_y2);
00278                 float sim_px = (*isim).momentumAtEntry().x();
00279                 float sim_py = (*isim).momentumAtEntry().y();
00280                 float sim_pz = (*isim).momentumAtEntry().z();
00281                                                 
00282                 float dr = (sim_xpos-(recHitPix->localPosition().x()))*(sim_xpos-recHitPix->localPosition().x()) +
00283                   (sim_ypos-recHitPix->localPosition().y())*(sim_ypos-recHitPix->localPosition().y());
00284                 if(dr<dr_start) {
00285                   simhit_.x     = sim_xpos;
00286                   simhit_.y     = sim_ypos;
00287                   simhit_.alpha = atan2(sim_pz, sim_px);
00288                   simhit_.beta  = atan2(sim_pz, sim_py);
00289                   simhit_.gamma = atan2(sim_px, sim_py);
00290                   dr_start = dr;
00291                 }
00292               }
00293             } // end of filling simhit_
00294           }
00295           // is one pixel in cluster a large pixel ? (hit will be excluded)
00296           bool large_pix = false;
00297           for (int j = 0; j <  pixinfo_.npix; j++){
00298             int colpos = static_cast<int>(pixinfo_.col[j]);
00299             if (pixinfo_.row[j] == 0 || pixinfo_.row[j] == 79 || pixinfo_.row[j] == 80 || pixinfo_.row[j] == 159 || colpos % 52 == 0 || colpos % 52 == 51 ){
00300               large_pix = true; 
00301             }
00302           }
00303                                         
00304           double residual = TMath::Sqrt( (trackhit_.x - rechit_.x) * (trackhit_.x - rechit_.x) + (trackhit_.y - rechit_.y) * (trackhit_.y - rechit_.y) );
00305         
00306           SiPixelLorentzAngleTree_->Fill();
00307           if( !large_pix && (chi2_/ndof_) < normChi2Max_ && cluster->sizeY() >= clustSizeYMin_ && residual < residualMax_ && (cluster->charge() < clustChargeMax_)){
00308             usedHitCounter_++;
00309             // iterate over pixels in hit
00310             for (int j = 0; j <  pixinfo_.npix; j++){
00311               // use trackhits
00312               float dx = (pixinfo_.x[j]  - (trackhit_.x - width_/2. / TMath::Tan(trackhit_.alpha))) * 10000.;
00313               float dy = (pixinfo_.y[j]  - (trackhit_.y - width_/2. / TMath::Tan(trackhit_.beta))) * 10000.;
00314               float depth = dy * tan(trackhit_.beta);
00315               float drift = dx - dy * tan(trackhit_.gamma);
00316               _h_drift_depth_adc_[module_ + (layer_ -1) * 8]->Fill(drift, depth, pixinfo_.adc[j]);
00317               _h_drift_depth_adc2_[module_ + (layer_ -1) * 8]->Fill(drift, depth, pixinfo_.adc[j]*pixinfo_.adc[j]);
00318               _h_drift_depth_noadc_[module_ + (layer_ -1) * 8]->Fill(drift, depth);             
00319               if( layer_ == 3 && module_==1 && isflipped_){
00320                 float dx_rot = dx * TMath::Cos(trackhit_.gamma) + dy * TMath::Sin(trackhit_.gamma);
00321                 float dy_rot = dy * TMath::Cos(trackhit_.gamma) - dx * TMath::Sin(trackhit_.gamma) ;
00322                 h_cluster_shape_adc_->Fill(dx, dy, pixinfo_.adc[j]);
00323                 h_cluster_shape_noadc_->Fill(dx, dy);
00324                 h_cluster_shape_adc_rot_->Fill(dx_rot, dy_rot, pixinfo_.adc[j]);
00325                 h_cluster_shape_noadc_rot_->Fill(dx_rot, dy_rot);
00326               }
00327             }
00328           }
00329         } else if (subDetID == PixelSubdetector::PixelEndcap) {
00330           DetId detIdObj = recHit->geographicalId();
00331           const PixelGeomDetUnit * theGeomDet = dynamic_cast<const PixelGeomDetUnit*> ( tracker->idToDet(detIdObj) );
00332           if(!theGeomDet) continue;
00333           
00334           const PixelTopology * topol = &(theGeomDet->specificTopology());
00335 
00336           if(!topol) continue;
00337           PXFDetId pxfdetIdObj(detIdObj);
00338           sideF_ = pxfdetIdObj.side();
00339           diskF_ = pxfdetIdObj.disk();
00340           bladeF_ = pxfdetIdObj.blade();
00341           panelF_ = pxfdetIdObj.panel();
00342           moduleF_ = pxfdetIdObj.module();
00343           //                                    float tmp1 = theGeomDet->surface().toGlobal(Local3DPoint(0.,0.,0.)).perp();
00344           //                                    float tmp2 = theGeomDet->surface().toGlobal(Local3DPoint(0.,0.,1.)).perp();
00345           //                                    if ( tmp2<tmp1 ) isflipped_ = 1;
00346           //                                    else isflipped_ = 0;
00347           const SiPixelRecHit * recHitPix = dynamic_cast<const SiPixelRecHit *>((*recHit).hit());
00348           if(!recHitPix) continue;
00349           rechitF_.x  = recHitPix->localPosition().x();
00350           rechitF_.y  = recHitPix->localPosition().y();
00351           SiPixelRecHit::ClusterRef const& cluster = recHitPix->cluster();      
00352         
00353           // fill entries in clust_
00354           clustF_.x = (cluster)->x();
00355           clustF_.y = (cluster)->y();
00356           clustF_.charge = (cluster->charge())/1000.;
00357           clustF_.size_x = cluster->sizeX();
00358           clustF_.size_y = cluster->sizeY();
00359           clustF_.maxPixelCol = cluster->maxPixelCol();
00360           clustF_.maxPixelRow = cluster->maxPixelRow();
00361           clustF_.minPixelCol = cluster->minPixelCol();
00362           clustF_.minPixelRow = cluster->minPixelRow();
00363           // fill entries in pixinfo_:
00364           fillPix(*cluster ,topol, pixinfoF_);
00365           // fill the trackhit info
00366           TrajectoryStateOnSurface tsos=itTraj->updatedState();
00367           if(!tsos.isValid()){
00368             cout << "tsos not valid" << endl;
00369             continue;   
00370           }     
00371           LocalVector trackdirection=tsos.localDirection();
00372           LocalPoint trackposition=tsos.localPosition();
00373         
00374           if(trackdirection.z()==0) continue;                           
00375           // the local position and direction
00376           trackhitF_.alpha = atan2(trackdirection.z(),trackdirection.x());
00377           trackhitF_.beta = atan2(trackdirection.z(),trackdirection.y());
00378           trackhitF_.gamma = atan2(trackdirection.x(),trackdirection.y());
00379           trackhitF_.x = trackposition.x();
00380           trackhitF_.y = trackposition.y();
00381                                         
00382         
00383           // fill entries in simhit_:   
00384           if(simData_){
00385             matched.clear();        
00386             matched = associate->associateHit((*recHitPix));    
00387             float dr_start=9999.;
00388             for (std::vector<PSimHit>::iterator isim = matched.begin(); isim != matched.end(); ++isim){
00389               DetId simdetIdObj((*isim).detUnitId());
00390               if (simdetIdObj == detIdObj) {
00391                 float sim_x1 = (*isim).entryPoint().x(); // width (row index, in col direction)
00392                 float sim_y1 = (*isim).entryPoint().y(); // length (col index, in row direction)
00393                 float sim_x2 = (*isim).exitPoint().x();
00394                 float sim_y2 = (*isim).exitPoint().y();
00395                 float sim_xpos = 0.5*(sim_x1+sim_x2);
00396                 float sim_ypos = 0.5*(sim_y1+sim_y2);
00397                 float sim_px = (*isim).momentumAtEntry().x();
00398                 float sim_py = (*isim).momentumAtEntry().y();
00399                 float sim_pz = (*isim).momentumAtEntry().z();
00400                                                 
00401                 float dr = (sim_xpos-(recHitPix->localPosition().x()))*(sim_xpos-recHitPix->localPosition().x()) +
00402                   (sim_ypos-recHitPix->localPosition().y())*(sim_ypos-recHitPix->localPosition().y());
00403                 if(dr<dr_start) {
00404                   simhitF_.x     = sim_xpos;
00405                   simhitF_.y     = sim_ypos;
00406                   simhitF_.alpha = atan2(sim_pz, sim_px);
00407                   simhitF_.beta  = atan2(sim_pz, sim_py);
00408                   simhitF_.gamma = atan2(sim_px, sim_py);
00409                   dr_start = dr;
00410                 }
00411               }
00412             } // end of filling simhit_
00413           }
00414           SiPixelLorentzAngleTreeForward_->Fill();
00415         }
00416       } //end iteration over trajectory measurements
00417     } //end iteration over trajectories
00418   }
00419 
00420         
00421         
00422         
00423         
00424 }
00425 
00426 void SiPixelLorentzAngle::endJob()
00427 {
00428   // produce histograms with the average adc counts
00429   for(int i_ring = 1; i_ring<=24; i_ring++){
00430     _h_drift_depth_[i_ring]->Divide(_h_drift_depth_adc_[i_ring], _h_drift_depth_noadc_[i_ring]);
00431   }
00432         
00433   h_drift_depth_adc_slice_ = new TH1F("h_drift_depth_adc_slice", "slice of adc histogram", hist_drift_ , min_drift_, max_drift_);
00434 
00435   TF1 *f1 = new TF1("f1","[0] + [1]*x",50., 235.); 
00436   f1->SetParName(0,"p0");
00437   f1->SetParName(1,"p1");
00438   f1->SetParameter(0,0);
00439   f1->SetParameter(1,0.4);
00440   ofstream fLorentzFit( filenameFit_.c_str(), ios::trunc );
00441   cout.precision( 4 );
00442   fLorentzFit << "module" << "\t" << "layer" << "\t" << "offset" << "\t" << "error" << "\t" << "slope" << "\t" << "error" << "\t" "rel.err" << "\t" "pull" << "\t" << "chi2" << "\t" << "prob" << endl;
00443   //loop over modlues and layers to fit the lorentz angle
00444   for( int i_layer = 1; i_layer<=3; i_layer++){
00445     for(int i_module = 1; i_module<=8; i_module++){
00446       //loop over bins in depth (z-local-coordinate) (in order to fit slices)
00447       for( int i = 1; i <= hist_depth_; i++){
00448         findMean(i, (i_module + (i_layer - 1) * 8));    
00449       }// end loop over bins in depth 
00450       _h_mean_[i_module + (i_layer - 1) * 8]->Fit(f1,"ERQ");
00451       double p0 = f1->GetParameter(0);
00452       double e0 = f1->GetParError(0);
00453       double p1 = f1->GetParameter(1);
00454       double e1 = f1->GetParError(1);
00455       double chi2 = f1->GetChisquare();
00456       double prob = f1->GetProb();
00457       fLorentzFit << setprecision( 4 ) << i_module << "\t" << i_layer << "\t" << p0 << "\t" << e0 << "\t" << p1 << setprecision( 3 ) << "\t" << e1 << "\t" << e1 / p1 *100. << "\t"<< (p1 - 0.424) / e1 << "\t"<< chi2 << "\t" << prob << endl;
00458     }
00459   } // end loop over modules and layers
00460   fLorentzFit.close(); 
00461   hFile_->cd();
00462   for(int i_module = 1; i_module<=8; i_module++){
00463     for(int i_layer = 1; i_layer<=3; i_layer++){
00464       _h_drift_depth_adc_[i_module + (i_layer -1) * 8]->Write();
00465       _h_drift_depth_adc2_[i_module + (i_layer -1) * 8]->Write();
00466       _h_drift_depth_noadc_[i_module + (i_layer -1) * 8]->Write();
00467       _h_drift_depth_[i_module + (i_layer -1) * 8]->Write();
00468       _h_mean_[i_module + (i_layer -1) * 8]->Write();
00469     }
00470   }
00471   h_cluster_shape_adc_->Write();
00472   h_cluster_shape_noadc_->Write();
00473   h_cluster_shape_adc_rot_->Write();
00474   h_cluster_shape_noadc_rot_->Write();
00475   h_tracks_->Write();
00476         
00477   hFile_->Write();
00478   hFile_->Close();
00479   cout << "events: " << event_counter_ << endl;
00480   cout << "events with tracks: " << trackEventsCounter_ << endl;        
00481   cout << "events with pixeltracks: " << pixelTracksCounter_ << endl;   
00482   cout << "hits in the pixel: " << hitCounter_ << endl;
00483   cout << "number of used Hits: " << usedHitCounter_ << endl;
00484 }
00485 
00486 inline void SiPixelLorentzAngle::fillPix(const SiPixelCluster & LocPix, const PixelTopology * topol, Pixinfo& pixinfo)
00487 
00488 {
00489   const std::vector<SiPixelCluster::Pixel>& pixvector = LocPix.pixels();
00490   pixinfo.npix = 0;
00491   for(std::vector<SiPixelCluster::Pixel>::const_iterator itPix = pixvector.begin(); itPix != pixvector.end(); itPix++){
00492     //  for(pixinfo.npix = 0; pixinfo.npix < static_cast<int>(pixvector.size()); ++pixinfo.npix) {
00493     pixinfo.row[pixinfo.npix] = itPix->x;
00494     pixinfo.col[pixinfo.npix] = itPix->y;
00495     pixinfo.adc[pixinfo.npix] = itPix->adc;
00496     LocalPoint lp = topol->localPosition(MeasurementPoint(itPix->x + 0.5, itPix->y+0.5));
00497     pixinfo.x[pixinfo.npix] = lp.x();
00498     pixinfo.y[pixinfo.npix]= lp.y();
00499     pixinfo.npix++;
00500   }
00501 }
00502 
00503 void SiPixelLorentzAngle::findMean(int i, int i_ring)
00504 {
00505   double nentries = 0;
00506         
00507   h_drift_depth_adc_slice_->Reset("ICE");
00508         
00509   // determine sigma and sigma^2 of the adc counts and average adc counts
00510   //loop over bins in drift width
00511   for( int j = 1; j<= hist_drift_; j++){
00512     if(_h_drift_depth_noadc_[i_ring]->GetBinContent(j, i) >= 1){
00513       double adc_error2 = (_h_drift_depth_adc2_[i_ring]->GetBinContent(j,i) - _h_drift_depth_adc_[i_ring]->GetBinContent(j,i)*_h_drift_depth_adc_[i_ring]->GetBinContent(j, i) / _h_drift_depth_noadc_[i_ring]->GetBinContent(j, i)) /  _h_drift_depth_noadc_[i_ring]->GetBinContent(j, i);
00514       _h_drift_depth_adc_[i_ring]->SetBinError(j, i, sqrt(adc_error2));
00515       double error2 = adc_error2 / (_h_drift_depth_noadc_[i_ring]->GetBinContent(j,i) - 1.);
00516       _h_drift_depth_[i_ring]->SetBinError(j,i,sqrt(error2));
00517     } 
00518     else{
00519       _h_drift_depth_[i_ring]->SetBinError(j,i,0);
00520       _h_drift_depth_adc_[i_ring]->SetBinError(j, i, 0);
00521     }
00522     h_drift_depth_adc_slice_->SetBinContent(j, _h_drift_depth_adc_[i_ring]->GetBinContent(j,i));
00523     h_drift_depth_adc_slice_->SetBinError(j, _h_drift_depth_adc_[i_ring]->GetBinError(j,i));
00524     nentries += _h_drift_depth_noadc_[i_ring]->GetBinContent(j,i);      
00525   } // end loop over bins in drift width
00526                 
00527   double mean = h_drift_depth_adc_slice_->GetMean(1); 
00528   double error = 0;
00529   if(nentries != 0){
00530     error = h_drift_depth_adc_slice_->GetRMS(1) / sqrt(nentries);
00531   }
00532                 
00533   _h_mean_[i_ring]->SetBinContent(i, mean);
00534   _h_mean_[i_ring]->SetBinError(i, error);
00535 
00536 }