CMS 3D CMS Logo

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:
40  AlignmentPrescaler(const edm::ParameterSet& iConfig);
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
120 
121  //prepare the output of the ValueMap flagging tracks
122  std::vector<int> trackflags(Tracks->size(), 0);
123 
124  //loop on tracks
125  for (std::vector<reco::Track>::const_iterator ittrk = Tracks->begin(), edtrk = Tracks->end(); ittrk != edtrk;
126  ++ittrk) {
127  //loop on tracking rechits
128  LogDebug("AlignmentPrescaler") << "Loop on hits of track #" << (ittrk - Tracks->begin()) << std::endl;
129  int ntakenhits = 0;
130  bool firstTakenHit = false;
131 
132  for (auto const& hit : ittrk->recHits()) {
133  if (!hit->isValid()) {
134  continue;
135  }
136  uint32_t tmpdetid = hit->geographicalId().rawId();
137  tpresc_->GetEntryWithIndex(tmpdetid);
138 
139  //-------------
140  //decide whether to take this hit or not
141  bool takeit = false;
142  int subdetId = hit->geographicalId().subdetId();
143 
144  //check first if the cluster is also in the overlap asso map
145  bool isOverlapHit = false;
146  // bool first=true;
147  //ugly...
148  const SiPixelRecHit* pixelhit = dynamic_cast<const SiPixelRecHit*>(hit);
149  const SiStripRecHit1D* stripHit1D = dynamic_cast<const SiStripRecHit1D*>(hit);
150  const SiStripRecHit2D* stripHit2D = dynamic_cast<const SiStripRecHit2D*>(hit);
151 
152  AlignmentClusterFlag tmpflag(hit->geographicalId());
153  int stripType = 0;
154  if (subdetId > 2) { // SST case
155  const std::type_info& type = typeid(*hit);
156  if (type == typeid(SiStripRecHit1D))
157  stripType = 1;
158  else if (type == typeid(SiStripRecHit2D))
159  stripType = 2;
160  else
161  stripType = 3;
162 
163  if (stripType == 1) {
164  // const SiStripRecHit1D* stripHit1D = dynamic_cast<const SiStripRecHit1D*>(hit);
165 
166  if (stripHit1D != nullptr) {
167  SiStripRecHit1D::ClusterRef stripclust(stripHit1D->cluster());
168  tmpflag = InValMap[stripclust];
169  tmpflag.SetDetId(hit->geographicalId());
170  if (tmpflag.isOverlap())
171  isOverlapHit = true;
172  LogDebug("AlignmentPrescaler")
173  << "~*~*~* Prescale (1D) for module " << tmpflag.detId().rawId() << "("
174  << InValMap[stripclust].detId().rawId() << ") is " << hitPrescFactor_ << std::flush;
175  if (tmpflag.isOverlap())
176  LogDebug("AlignmentPrescaler") << " (it is Overlap)";
177  } //end if striphit1D!=0
178  } else if (stripType == 2) {
179  //const SiStripRecHit2D* stripHit2D = dynamic_cast<const SiStripRecHit2D*>(hit);
180  if (stripHit2D != nullptr) {
181  SiStripRecHit2D::ClusterRef stripclust(stripHit2D->cluster());
182  tmpflag = InValMap[stripclust];
183  tmpflag.SetDetId(hit->geographicalId());
184  if (tmpflag.isOverlap())
185  isOverlapHit = true;
186  LogDebug("AlignmentPrescaler")
187  << "~*~*~* Prescale (2D) for module " << tmpflag.detId().rawId() << "("
188  << InValMap[stripclust].detId().rawId() << ") is " << hitPrescFactor_ << std::flush;
189  if (tmpflag.isOverlap())
190  LogDebug("AlignmentPrescaler") << " (it is Overlap)" << endl;
191  } //end if striphit2D!=0
192  }
193  } //end if is a strip hit
194  else {
195  // const SiPixelRecHit* pixelhit= dynamic_cast<const SiPixelRecHit*>(hit);
196  if (pixelhit != nullptr) {
197  SiPixelClusterRefNew pixclust(pixelhit->cluster());
198  tmpflag = InValMap[pixclust];
199  tmpflag.SetDetId(hit->geographicalId());
200  if (tmpflag.isOverlap())
201  isOverlapHit = true;
202  }
203  } //end else is a pixel hit
204  // tmpflag.SetDetId(hit->geographicalId());
205 
206  if (isOverlapHit) {
207  LogDebug("AlignmentPrescaler") << " DetId=" << tmpdetid << " is Overlap! " << flush;
208  takeit = (float(myrand_->Rndm()) <= overlapPrescFactor_);
209  }
210  if (!takeit) {
211  float rr = float(myrand_->Rndm());
212  takeit = (rr <= hitPrescFactor_);
213  }
214  if (takeit) { //HIT TAKEN !
215  LogDebug("AlignmentPrescaler") << " DetId=" << tmpdetid << " taken!" << flush;
216  tmpflag.SetTakenFlag();
217 
218  if (subdetId > 2) {
219  if (stripType == 1) {
220  SiStripRecHit1D::ClusterRef stripclust(stripHit1D->cluster());
221  InValMap[stripclust] = tmpflag; //.SetTakenFlag();
222  } else if (stripType == 2) {
223  SiStripRecHit1D::ClusterRef stripclust(stripHit2D->cluster());
224  InValMap[stripclust] = tmpflag; //.SetTakenFlag();
225  } else
226  std::cout << "Unknown type of strip hit" << std::endl;
227  } else {
228  SiPixelClusterRefNew pixclust(pixelhit->cluster());
229  InValMap[pixclust] = tmpflag; //.SetTakenFlag();
230  }
231 
232  if (!firstTakenHit) {
233  firstTakenHit = true;
234  LogDebug("AlignmentPrescaler") << "Index of the track iterator is " << ittrk - Tracks->begin();
235  }
236  ntakenhits++;
237  } //end if take this hit
238  } //end loop on RecHits
239  trackflags[ittrk - Tracks->begin()] = ntakenhits;
240  } //end loop on tracks
241 
242  //save the asso map, tracks...
243  // prepare output
244  auto OutVM = std::make_unique<AliClusterValueMap>();
245  *OutVM = InValMap;
246 
247  iEvent.put(std::move(OutVM));
248 
249  auto trkVM = std::make_unique<AliTrackTakenClusterValueMap>();
250  AliTrackTakenClusterValueMap::Filler trkmapfiller(*trkVM);
251  trkmapfiller.insert(Tracks, trackflags.begin(), trackflags.end());
252  trkmapfiller.fill();
253  iEvent.put(std::move(trkVM));
254 
255 } //end produce
256 
257 int AlignmentPrescaler::layerFromId(const DetId& id, const TrackerTopology* tTopo) const {
258  if (uint32_t(id.subdetId()) == PixelSubdetector::PixelBarrel) {
259  return tTopo->pxbLayer(id);
260  } else if (uint32_t(id.subdetId()) == PixelSubdetector::PixelEndcap) {
261  return tTopo->pxfDisk(id) + (3 * (tTopo->pxfSide(id) - 1));
262  } else if (id.subdetId() == StripSubdetector::TIB) {
263  return tTopo->tibLayer(id);
264  } else if (id.subdetId() == StripSubdetector::TOB) {
265  return tTopo->tobLayer(id);
266  } else if (id.subdetId() == StripSubdetector::TEC) {
267  return tTopo->tecWheel(id) + (9 * (tTopo->pxfSide(id) - 1));
268  } else if (id.subdetId() == StripSubdetector::TID) {
269  return tTopo->tidWheel(id) + (3 * (tTopo->tidSide(id) - 1));
270  }
271  return -1;
272 } //end layerfromId
273 
276  desc.setComment("Prescale Tracker Alignment hits for alignment procedures");
277  desc.add<edm::InputTag>("src", edm::InputTag("generalTracks"));
278  desc.add<edm::InputTag>("assomap", edm::InputTag("OverlapAssoMap"));
279  desc.add<std::string>("PrescFileName", "PrescaleFactors.root");
280  desc.add<std::string>("PrescTreeName", "AlignmentHitMap");
281  descriptions.addWithDefaultLabel(desc);
282 }
283 
284 // ========= MODULE DEF ==============
static constexpr auto TEC
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
unsigned int tobLayer(const DetId &id) const
unsigned int pxbLayer(const DetId &id) const
int layerFromId(const DetId &id, const TrackerTopology *tTopo) const
AlignmentPrescaler(const edm::ParameterSet &iConfig)
unsigned int tidSide(const DetId &id) const
unsigned int tidWheel(const DetId &id) const
unsigned int tecWheel(const DetId &id) const
~AlignmentPrescaler() override
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup) override
ClusterRef cluster() const
void beginJob() override
int iEvent
Definition: GenABIO.cc:224
unsigned int pxfDisk(const DetId &id) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static constexpr auto TOB
edm::EDGetTokenT< AliClusterValueMap > aliClusterToken_
static void fillDescriptions(edm::ConfigurationDescriptions &)
Log< level::Warning, true > LogPrint
edm::EDGetTokenT< reco::TrackCollection > tracksToken_
Definition: DetId.h:17
unsigned int pxfSide(const DetId &id) const
static constexpr auto TIB
void endJob() override
HLT enums.
unsigned int tibLayer(const DetId &id) const
ClusterRef cluster() const
static constexpr auto TID
def move(src, dest)
Definition: eostools.py:511
Our base class.
Definition: SiPixelRecHit.h:23
#define LogDebug(id)