CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
TkAlCaSkimTreeMerger.cc
Go to the documentation of this file.
1 
2 #ifndef TrackerAlignment_TkAlCaSkimTreeMerger_H
3 #define TrackerAlignment_TkAlCaSkimTreeMerger_H
4 
5 #include <Riostream.h>
6 #include <string>
7 #include <fstream>
8 #include <map>
9 
18 
19 
20 #include "TFile.h"
21 #include "TString.h"
22 #include "TChain.h"
23 #include "TStopwatch.h"
24 
26 
27  public:
30  void beginJob();
31  void endJob();
32  void analyze(const edm::Event&, const edm::EventSetup&);
33 
34  private:
35  TTree *out_;//TTree containing the merged result
36  TTree *firsttree_;//first tree of the list; this gives the structure to all the others
37  TChain *ch_;//chain containing all the tree you want to merge
38  std::string filelist_;//text file containing the list of input files
40  std::string treename_;//name of the tree you want to merge (contained in the file)
41  std::string outfilename_;//name of the file where you want to save the output
42 
43  //Hit Population
44  typedef map<uint32_t,uint32_t>DetHitMap;
47  int maxhits_;//above this number, the hit population is prescaled. Configurable for each subdet
50 
51 
52  TStopwatch myclock;
53 
54 };
55 
56 
57 
58 
59 #endif
60 
61 
63  filelist_(iConfig.getParameter<string>("FileList")),
64  treename_(iConfig.getParameter<string>("TreeName")),
65  outfilename_(iConfig.getParameter<string>("OutputFile")),
66  // maxhits_(iConfig.getParameter<vint>("NhitsMaxLimit"))
67  maxhits_(iConfig.getParameter<int32_t>("NhitsMaxLimit")),
68  maxhitsSet_(iConfig.getParameter<edm::ParameterSet>("NhitsMaxSet"))
69 {
70  maxPXBhits_=maxhitsSet_.getParameter<int32_t>("PXBmaxhits");
71  maxPXFhits_=maxhitsSet_.getParameter<int32_t>("PXFmaxhits");
72  maxTIBhits_=maxhitsSet_.getParameter<int32_t>("TIBmaxhits");
73  maxTIDhits_=maxhitsSet_.getParameter<int32_t>("TIDmaxhits");
74  maxTOBhits_=maxhitsSet_.getParameter<int32_t>("TOBmaxhits");
75  maxTEChits_=maxhitsSet_.getParameter<int32_t>("TECmaxhits");
76  //anything you want to do for initializing
77  cout<<"\n\n*** MAX N HITS = "<<maxhits_<<endl<<endl;
78  out_=0;
79  firsttree_=0;
80  ch_=0;
81 }
82 
83 
85  //default destructor
86  // delete out_;
87  // delete firsttree_;
88 
89  delete ch_;
90  cout<<"finished."<<endl;
91 }
92 
93 // ------------ method called before analyzing the first event ------------
95 
96  myclock.Start();
97 
98  //prepare the chain
99  ch_=new TChain(treename_.c_str());
100  cout<<"The chain contains "<<ch_->GetNtrees()<<" trees"<<endl;
101 
102  //load the trees into the chain
103  ifstream flist(filelist_.c_str(),ios::in);
105  std::string firstfilename;
106  bool first=true;
107  while(!flist.eof()){
108  filename="";
109  flist>>filename;
110  if(filename.empty())continue;
111  //cout<<"Adding "<<filename<<endl;
112  ch_->Add(filename.c_str());
113  if(first){
115  first=false;
116  }
117 
118  }
119  cout<<"Now the chain contains "<<ch_->GetNtrees()<<" trees ("<<ch_->GetEntries()<<" entries)"<<endl;
120 
121 
122  unsigned int id_ch=0;
123  uint32_t nhits_ch=0,noverlaps_ch=0;
124  ch_->SetBranchAddress("DetId", &id_ch);
125  ch_->SetBranchAddress("Nhits", &nhits_ch);
126  ch_->SetBranchAddress("Noverlaps",&noverlaps_ch);
127 
128  ch_->SetBranchStatus("SubDet",0);
129  ch_->SetBranchStatus("Layer",0);
130  ch_->SetBranchStatus("is2D",0);
131  ch_->SetBranchStatus("isStereo",0);
132  ch_->SetBranchStatus("posX",0);
133  ch_->SetBranchStatus("posY",0);
134  ch_->SetBranchStatus("posZ",0);
135  ch_->SetBranchStatus("posR",0);
136  ch_->SetBranchStatus("posEta",0);
137  ch_->SetBranchStatus("posPhi",0);//now only id, nhits and noverlaps are on...
138 
139 
140  int totnhits(0),totnoverlaps(0);
141 
142 
143  //look if you find this detid in the map
144  DetHitMap::iterator mapiter;
145  DetHitMap::iterator overlapiter;
146 
147  for(int ent=0;ent<ch_->GetEntries();++ent){
148  // for(int ent=0;ent<100;++ent){
149  ch_->GetEntry(ent);
150  totnhits+=nhits_ch;
151  totnoverlaps+=noverlaps_ch;
152 
153  mapiter= hitmap_.find(id_ch);
154  if(mapiter!=hitmap_.end() ){//present, increase its value
155  hitmap_[id_ch]=hitmap_[id_ch]+nhits_ch;
156  }
157  else{//not present, let's add this key to the map with value=1
158  hitmap_.insert(pair<uint32_t, uint32_t>(id_ch, nhits_ch));
159  }
160  //do the same for overlaps
161  overlapiter= overlapmap_.find(id_ch);
162  if(overlapiter!=overlapmap_.end() ){
163  overlapmap_[id_ch]=overlapmap_[id_ch]+noverlaps_ch;
164  }
165  else{
166  overlapmap_.insert(pair<uint32_t, uint32_t>(id_ch, noverlaps_ch));
167  }
168 
169  }//end loop on ent - entries in the chain
170 
171 
172  cout<<"Nhits in the chain: "<<totnhits<<endl;
173  cout<<"NOverlaps in the chain: "<<totnoverlaps<<endl;
174 
175 
176  myclock.Stop();
177  cout<<"Finished beginJob after "<<myclock.RealTime()<<" s (real time) / "<<myclock.CpuTime()<<" s (cpu time)"<<endl;
178  myclock.Continue();
179 }//end beginJob
180 
181 
182 // ------------ method called to for each event ------------
184  // cout<<firsttree_->GetEntries()<<endl;
185 }//end analyze
186 
187 // ------------ method called after having analyzed all the events ------------
189 
190 
191  //address variables in the first tree and in the chain
192  TFile *firstfile=new TFile(firstfilename_.c_str(),"READ");
193  firsttree_=(TTree*)firstfile->Get(treename_.c_str());
194  cout<<"the first tree has "<<firsttree_->GetEntries() <<" entries"<<endl;
195  unsigned int id=0;
196  uint32_t nhits=0,noverlaps=0;
197  float posX(-99999.0),posY(-77777.0),posZ(-88888.0);
198  float posEta(-6666.0),posPhi(-5555.0),posR(-4444.0);
199  int subdet=0;
200  unsigned int layer=0;
201  // bool is2D=false,isStereo=false;
202  firsttree_->SetBranchAddress("DetId", &id);
203  firsttree_->SetBranchAddress("Nhits", &nhits);
204  firsttree_->SetBranchAddress("Noverlaps",&noverlaps);
205  firsttree_->SetBranchAddress("SubDet", &subdet);
206  firsttree_->SetBranchAddress("Layer", &layer);
207  // firsttree_->SetBranchAddress("is2D" , &is2D);
208  // firsttree_->SetBranchAddress("isStereo", &isStereo);
209  firsttree_->SetBranchAddress("posX", &posX);
210  firsttree_->SetBranchAddress("posY", &posY);
211  firsttree_->SetBranchAddress("posZ", &posZ);
212  firsttree_->SetBranchAddress("posR", &posR);
213  firsttree_->SetBranchAddress("posEta", &posEta);
214  firsttree_->SetBranchAddress("posPhi", &posPhi);
215 
216 
217  //create and book the output
218 
219 
220  TFile *outfile=new TFile(outfilename_.c_str(),"RECREATE");
221  out_=new TTree(treename_.c_str(),"AlignmentHitMapsTOTAL");
222  unsigned int id_out=0;
223  uint32_t nhits_out=0,noverlaps_out=0;
224  float posX_out(-99999.0),posY_out(-77777.0),posZ_out(-88888.0);
225  float posEta_out(-6666.0),posPhi_out(-5555.0),posR_out(-4444.0);
226  int subdet_out=0;
227  unsigned int layer_out=0;
228  bool is2D_out=false,isStereo_out=false;
229  float prescfact_out=1.0;
230  float prescfact_overlap_out=1.0;
231 
232  out_->Branch("DetId", &id_out , "DetId/i");
233  out_->Branch("Nhits", &nhits_out , "Nhits/i");
234  out_->Branch("Noverlaps",&noverlaps_out,"Noverlaps/i");
235  out_->Branch("SubDet", &subdet_out, "SubDet/I");
236  out_->Branch("Layer", &layer_out, "Layer/i");
237  out_->Branch("is2D" , &is2D_out, "is2D/B");
238  out_->Branch("isStereo", &isStereo_out, "isStereo/B");
239  out_->Branch("posX", &posX_out, "posX/F");
240  out_->Branch("posY", &posY_out, "posY/F");
241  out_->Branch("posZ", &posZ_out, "posZ/F");
242  out_->Branch("posR", &posR_out, "posR/F");
243  out_->Branch("posEta", &posEta_out, "posEta/F");
244  out_->Branch("posPhi", &posPhi_out, "posPhi/F");
245  out_->Branch("PrescaleFactor",&prescfact_out,"PrescaleFact/F");
246  out_->Branch("PrescaleFactorOverlap",&prescfact_overlap_out,"PrescaleFactOverlap/F");
247 
248 
249  //look if you find this detid in the map
250  DetHitMap::iterator mapiter;
251 
252  for(int mod=0;mod<firsttree_->GetEntries();mod++){
253  //for(int mod=0;mod<100;++mod){
254  // nhits_out=0;
255  // noverlaps_out=0;
256 
257  firsttree_->GetEntry(mod);
258  nhits_out=hitmap_[id];
259  noverlaps_out=overlapmap_[id];
260  // if(mod<25)cout<<"Nhits 1st tree: "<<nhits<<"\tTotal nhits chain: "<<nhits_out<<endl;
261  id_out=id;
262  subdet_out=subdet;
263  layer_out=layer;
264  posX_out=posX;
265  posY_out=posY;
266  posZ_out=posZ;
267  posR_out=posR;
268  posEta_out=posEta;
269  posPhi_out=posPhi;
270 
271  //calculate prescaling factor
272  int subdetmax=-1;
273  if(subdet_out==1)subdetmax=maxPXBhits_;
274  else if(subdet_out==2)subdetmax=maxPXFhits_;
275  else if(subdet_out==3)subdetmax=maxTIBhits_;
276  else if(subdet_out==4)subdetmax=maxTIDhits_;
277  else if(subdet_out==5)subdetmax=maxTOBhits_;
278  else if(subdet_out==6)subdetmax=maxTEChits_;
279  else subdetmax=-9999;
280 
281  if(maxhits_>-1){
282  if(int(nhits_out)>maxhits_){
283  prescfact_out=float(maxhits_)/float(nhits_out);
284  }
285  if(int(noverlaps_out)>maxhits_){
286  prescfact_overlap_out=float(maxhits_)/float(noverlaps_out);
287  }
288  }
289  else if(subdetmax>0){//calculate different prescaling factors for each subdet
290  if(int(nhits_out)>subdetmax){
291  prescfact_out=float(subdetmax/nhits_out);
292  }
293  if(int(noverlaps_out)>subdetmax){
294  prescfact_overlap_out=float(subdetmax)/float(noverlaps_out);
295  }
296  }
297  else{
298  prescfact_out=1.0;
299  prescfact_overlap_out=1.0;
300  }
301  out_->Fill();
302 
303  }//end loop on mod - first tree modules
304 
305 
306  myclock.Stop();
307  cout<<"Finished endJob after "<<myclock.RealTime()<<" s (real time) / "<<myclock.CpuTime()<<" s (cpu time)"<<endl;
308  cout<<"Ending the tree merging."<<endl;
309  out_->Write();
310  cout<<"Deleting..."<<flush;
311  delete firstfile;
312  delete outfile;
313 
314 
315 }//end endJob
316 
317 
318 // ========= MODULE DEF ==============
322 
T getParameter(std::string const &) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
TkAlCaSkimTreeMerger(const edm::ParameterSet &iConfig)
list outfile
Definition: EdgesToViz.py:91
bool first
Definition: L1TdeRCT.cc:94
map< uint32_t, uint32_t > DetHitMap
edm::ParameterSet maxhitsSet_
tuple filename
Definition: lut2db_cfg.py:20
void analyze(const edm::Event &, const edm::EventSetup &)
tuple cout
Definition: gather_cfg.py:121
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4