CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
AlignmentPrescaler.cc
Go to the documentation of this file.
29 
30 #include "TFile.h"
31 #include "TTree.h"
32 #include "TRandom3.h"
33 #include "TH1F.h"
34 #include <string>
35 
36 class TrackerTopology;
37 
39 public:
41  ~AlignmentPrescaler() override;
42  void beginJob() override;
43  void endJob() override;
44  void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) override;
46 
47 private:
48  // tokens
51 
52  std::string prescfilename_; //name of the file containing the TTree with the prescaling factors
53  std::string presctreename_; //name of the TTree with the prescaling factors
54 
55  TFile* fpresc_;
56  TTree* tpresc_;
57  TRandom3* myrand_;
58 
59  int layerFromId(const DetId& id, const TrackerTopology* tTopo) const;
60 
61  unsigned int detid_;
64 };
65 
66 using namespace std;
67 
69  : tracksToken_(consumes(iConfig.getParameter<edm::InputTag>("src"))),
70  aliClusterToken_(consumes(iConfig.getParameter<edm::InputTag>("assomap"))),
71  prescfilename_(iConfig.getParameter<std::string>("PrescFileName")),
72  presctreename_(iConfig.getParameter<std::string>("PrescTreeName")) {
73  // issue the produce<>
74  produces<AliClusterValueMap>();
75  produces<AliTrackTakenClusterValueMap>();
76 }
77 
79 
81  //
82  edm::LogPrint("AlignmentPrescaler") << "in AlignmentPrescaler::beginJob" << std::flush;
83  fpresc_ = new TFile(prescfilename_.c_str(), "READ");
84  tpresc_ = (TTree*)fpresc_->Get(presctreename_.c_str());
85  tpresc_->BuildIndex("DetId");
86  tpresc_->SetBranchStatus("*", false);
87  tpresc_->SetBranchStatus("DetId", true);
88  tpresc_->SetBranchStatus("PrescaleFactor", true);
89  tpresc_->SetBranchStatus("PrescaleFactorOverlap", true);
90  edm::LogPrint("AlignmentPrescaler") << " Branches activated " << std::flush;
91  detid_ = 0;
92  hitPrescFactor_ = 99.0;
93  overlapPrescFactor_ = 88.0;
94 
95  tpresc_->SetBranchAddress("DetId", &detid_);
96  tpresc_->SetBranchAddress("PrescaleFactor", &hitPrescFactor_);
97  tpresc_->SetBranchAddress("PrescaleFactorOverlap", &overlapPrescFactor_);
98  edm::LogPrint("AlignmentPrescaler") << " addressed " << std::flush;
99  myrand_ = new TRandom3();
100  // myrand_->SetSeed();
101  edm::LogPrint("AlignmentPrescaler") << " ok ";
102 }
103 
105  delete tpresc_;
106  fpresc_->Close();
107  delete fpresc_;
108  delete myrand_;
109 }
110 
112  LogDebug("AlignmentPrescaler") << "\n\n#################\n### Starting the AlignmentPrescaler::produce ; Event: "
113  << iEvent.id().run() << ", " << iEvent.id().event() << std::endl;
114 
116  iEvent.getByToken(tracksToken_, Tracks);
117 
118  //take HitAssomap
119  AliClusterValueMap InValMap = iEvent.get(aliClusterToken_);
120 
121  //prepare the output of the ValueMap flagging tracks
122  std::vector<int> trackflags(Tracks->size(), 0);
123 
124  int npxlhits = 0;
125 
126  //loop on tracks
127  for (std::vector<reco::Track>::const_iterator ittrk = Tracks->begin(), edtrk = Tracks->end(); ittrk != edtrk;
128  ++ittrk) {
129  //loop on tracking rechits
130  LogDebug("AlignmentPrescaler") << "Loop on hits of track #" << (ittrk - Tracks->begin()) << std::endl;
131  int nhit = 0;
132  int ntakenhits = 0;
133  bool firstTakenHit = false;
134 
135  for (auto const& hit : ittrk->recHits()) {
136  if (!hit->isValid()) {
137  nhit++;
138  continue;
139  }
140  uint32_t tmpdetid = hit->geographicalId().rawId();
141  tpresc_->GetEntryWithIndex(tmpdetid);
142 
143  //-------------
144  //decide whether to take this hit or not
145  bool takeit = false;
146  int subdetId = hit->geographicalId().subdetId();
147 
148  //check first if the cluster is also in the overlap asso map
149  bool isOverlapHit = false;
150  // bool first=true;
151  //ugly...
152  const SiPixelRecHit* pixelhit = dynamic_cast<const SiPixelRecHit*>(hit);
153  const SiStripRecHit1D* stripHit1D = dynamic_cast<const SiStripRecHit1D*>(hit);
154  const SiStripRecHit2D* stripHit2D = dynamic_cast<const SiStripRecHit2D*>(hit);
155 
156  AlignmentClusterFlag tmpflag(hit->geographicalId());
157  int stripType = 0;
158  if (subdetId > 2) { // SST case
159  const std::type_info& type = typeid(*hit);
160  if (type == typeid(SiStripRecHit1D))
161  stripType = 1;
162  else if (type == typeid(SiStripRecHit2D))
163  stripType = 2;
164  else
165  stripType = 3;
166 
167  if (stripType == 1) {
168  // const SiStripRecHit1D* stripHit1D = dynamic_cast<const SiStripRecHit1D*>(hit);
169 
170  if (stripHit1D != nullptr) {
171  SiStripRecHit1D::ClusterRef stripclust(stripHit1D->cluster());
172  tmpflag = InValMap[stripclust];
173  tmpflag.SetDetId(hit->geographicalId());
174  if (tmpflag.isOverlap())
175  isOverlapHit = true;
176  LogDebug("AlignmentPrescaler")
177  << "~*~*~* Prescale (1D) for module " << tmpflag.detId().rawId() << "("
178  << InValMap[stripclust].detId().rawId() << ") is " << hitPrescFactor_ << std::flush;
179  if (tmpflag.isOverlap())
180  LogDebug("AlignmentPrescaler") << " (it is Overlap)";
181  } //end if striphit1D!=0
182  } else if (stripType == 2) {
183  //const SiStripRecHit2D* stripHit2D = dynamic_cast<const SiStripRecHit2D*>(hit);
184  if (stripHit2D != nullptr) {
185  SiStripRecHit2D::ClusterRef stripclust(stripHit2D->cluster());
186  tmpflag = InValMap[stripclust];
187  tmpflag.SetDetId(hit->geographicalId());
188  if (tmpflag.isOverlap())
189  isOverlapHit = true;
190  LogDebug("AlignmentPrescaler")
191  << "~*~*~* Prescale (2D) for module " << tmpflag.detId().rawId() << "("
192  << InValMap[stripclust].detId().rawId() << ") is " << hitPrescFactor_ << std::flush;
193  if (tmpflag.isOverlap())
194  LogDebug("AlignmentPrescaler") << " (it is Overlap)" << endl;
195  } //end if striphit2D!=0
196  }
197  } //end if is a strip hit
198  else {
199  // const SiPixelRecHit* pixelhit= dynamic_cast<const SiPixelRecHit*>(hit);
200  if (pixelhit != nullptr) {
201  npxlhits++;
202  SiPixelClusterRefNew pixclust(pixelhit->cluster());
203  tmpflag = InValMap[pixclust];
204  tmpflag.SetDetId(hit->geographicalId());
205  if (tmpflag.isOverlap())
206  isOverlapHit = true;
207  }
208  } //end else is a pixel hit
209  // tmpflag.SetDetId(hit->geographicalId());
210 
211  if (isOverlapHit) {
212  LogDebug("AlignmentPrescaler") << " DetId=" << tmpdetid << " is Overlap! " << flush;
213  takeit = (float(myrand_->Rndm()) <= overlapPrescFactor_);
214  }
215  if (!takeit) {
216  float rr = float(myrand_->Rndm());
217  takeit = (rr <= hitPrescFactor_);
218  }
219  if (takeit) { //HIT TAKEN !
220  LogDebug("AlignmentPrescaler") << " DetId=" << tmpdetid << " taken!" << flush;
221  tmpflag.SetTakenFlag();
222 
223  if (subdetId > 2) {
224  if (stripType == 1) {
225  SiStripRecHit1D::ClusterRef stripclust(stripHit1D->cluster());
226  InValMap[stripclust] = tmpflag; //.SetTakenFlag();
227  } else if (stripType == 2) {
228  SiStripRecHit1D::ClusterRef stripclust(stripHit2D->cluster());
229  InValMap[stripclust] = tmpflag; //.SetTakenFlag();
230  } else
231  std::cout << "Unknown type of strip hit" << std::endl;
232  } else {
233  SiPixelClusterRefNew pixclust(pixelhit->cluster());
234  InValMap[pixclust] = tmpflag; //.SetTakenFlag();
235  }
236 
237  if (!firstTakenHit) {
238  firstTakenHit = true;
239  LogDebug("AlignmentPrescaler") << "Index of the track iterator is " << ittrk - Tracks->begin();
240  }
241  ntakenhits++;
242  } //end if take this hit
243  nhit++;
244  } //end loop on RecHits
245  trackflags[ittrk - Tracks->begin()] = ntakenhits;
246  } //end loop on tracks
247 
248  //save the asso map, tracks...
249  // prepare output
250  auto OutVM = std::make_unique<AliClusterValueMap>();
251  *OutVM = InValMap;
252 
253  iEvent.put(std::move(OutVM));
254 
255  auto trkVM = std::make_unique<AliTrackTakenClusterValueMap>();
256  AliTrackTakenClusterValueMap::Filler trkmapfiller(*trkVM);
257  trkmapfiller.insert(Tracks, trackflags.begin(), trackflags.end());
258  trkmapfiller.fill();
259  iEvent.put(std::move(trkVM));
260 
261 } //end produce
262 
263 int AlignmentPrescaler::layerFromId(const DetId& id, const TrackerTopology* tTopo) const {
264  if (uint32_t(id.subdetId()) == PixelSubdetector::PixelBarrel) {
265  return tTopo->pxbLayer(id);
266  } else if (uint32_t(id.subdetId()) == PixelSubdetector::PixelEndcap) {
267  return tTopo->pxfDisk(id) + (3 * (tTopo->pxfSide(id) - 1));
268  } else if (id.subdetId() == StripSubdetector::TIB) {
269  return tTopo->tibLayer(id);
270  } else if (id.subdetId() == StripSubdetector::TOB) {
271  return tTopo->tobLayer(id);
272  } else if (id.subdetId() == StripSubdetector::TEC) {
273  return tTopo->tecWheel(id) + (9 * (tTopo->pxfSide(id) - 1));
274  } else if (id.subdetId() == StripSubdetector::TID) {
275  return tTopo->tidWheel(id) + (3 * (tTopo->tidSide(id) - 1));
276  }
277  return -1;
278 } //end layerfromId
279 
282  desc.setComment("Prescale Tracker Alignment hits for alignment procedures");
283  desc.add<edm::InputTag>("src", edm::InputTag("generalTracks"));
284  desc.add<edm::InputTag>("assomap", edm::InputTag("OverlapAssoMap"));
285  desc.add<std::string>("PrescFileName", "PrescaleFactors.root");
286  desc.add<std::string>("PrescTreeName", "AlignmentHitMap");
287  descriptions.addWithDefaultLabel(desc);
288 }
289 
290 // ========= MODULE DEF ==============
RunNumber_t run() const
Definition: EventID.h:38
ClusterRef cluster() const
EventNumber_t event() const
Definition: EventID.h:40
static constexpr auto TEC
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
unsigned int tibLayer(const DetId &id) const
AlignmentPrescaler(const edm::ParameterSet &iConfig)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
unsigned int pxfDisk(const DetId &id) const
unsigned int tidWheel(const DetId &id) const
~AlignmentPrescaler() override
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
void beginJob() override
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
unsigned int tidSide(const DetId &id) const
def move
Definition: eostools.py:511
bool get(ProductID const &oid, Handle< PROD > &result) const
Definition: Event.h:346
ClusterRef cluster() const
static constexpr auto TOB
edm::EDGetTokenT< AliClusterValueMap > aliClusterToken_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
static void fillDescriptions(edm::ConfigurationDescriptions &)
Log< level::Warning, true > LogPrint
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
unsigned int pxbLayer(const DetId &id) const
Definition: DetId.h:17
static constexpr auto TIB
ClusterRef cluster() const
Definition: SiPixelRecHit.h:47
void endJob() override
edm::EventID id() const
Definition: EventBase.h:59
unsigned int pxfSide(const DetId &id) const
tuple cout
Definition: gather_cfg.py:144
unsigned int tecWheel(const DetId &id) const
static constexpr auto TID
int layerFromId(const DetId &id, const TrackerTopology *tTopo) const
unsigned int tobLayer(const DetId &id) const
Our base class.
Definition: SiPixelRecHit.h:23
#define LogDebug(id)