EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairMultiLinkedData.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairMultiLinkedData.cxx
1 /*
2  * FairMultiLinkedData.cxx
3  *
4  * Created on: Dec 7, 2009
5  * Author: stockman
6  */
7 
8 #include "FairMultiLinkedData.h"
9 #include "TClonesArray.h"
10 
11 #include<algorithm>
12 
14 
16  :TObject(),
17  fLinks(),
18  fPersistanceCheck(kTRUE),
19  fVerbose(0),
20  fDefaultType(0)
21 
22 {
23 }
24 
25 FairMultiLinkedData::FairMultiLinkedData(std::set<FairLink> links, Bool_t persistanceCheck)
26  :TObject(),
27  fLinks(links),
28  fPersistanceCheck(persistanceCheck),
29  fVerbose(0),
30  fDefaultType(0)
31 {
32 }
33 
34 FairMultiLinkedData::FairMultiLinkedData(TString dataType, std::vector<Int_t> links, Int_t fileId, Int_t evtId, Bool_t persistanceCheck, Bool_t bypass, Float_t mult)
35  :TObject(),
36  fLinks(),
37  fPersistanceCheck(persistanceCheck),
38  fVerbose(0),
39  fDefaultType(0)
40 
41 {
42  SimpleAddLinks(fileId, evtId, FairRootManager::Instance()->GetBranchId(dataType), links, bypass, mult);
43 // FairMultiLinkedData(ioman->GetBranchId(dataType), links, persistanceCheck, bypass, addup, mult);
44 }
45 
46 FairMultiLinkedData::FairMultiLinkedData(Int_t dataType, std::vector<Int_t> links, Int_t fileId, Int_t evtId, Bool_t persistanceCheck, Bool_t bypass, Float_t mult)
47  :TObject(),
48  fLinks(),
49  fPersistanceCheck(persistanceCheck),
50  fVerbose(0),
51  fDefaultType(0)
52 
53 {
54  SimpleAddLinks(fileId, evtId, dataType, links, bypass, mult);
55 }
56 
58 {
59  if (pos < (Int_t)fLinks.size()) {
60  std::set<FairLink>::iterator it = fLinks.begin();
61  for (int i = 0; i < pos; i++) { it++; }
62  return *it;
63  } else {
64  std::cout << "-E- FairMultiLinkedData:GetLink(pos) pos " << pos << " outside range " << fLinks.size() << std::endl;
65  return FairLink();
66  }
67 }
68 
70 {
71  fLinks = links.GetLinks();
72  MultiplyAllWeights(mult);
73 }
74 
75 #ifndef __APPLE__
76 inline
77 #else
78 #endif
79 void FairMultiLinkedData::SetLink(FairLink link, Bool_t bypass, Float_t mult)
80 {
81  fLinks.clear();
82  Float_t weight = link.GetWeight() * mult;
83  link.SetWeight(weight);
84  AddLink(link, bypass);
85 }
86 
87 
89 {
90  std::set<FairLink> myLinks = links.GetLinks();
91  for (std::set<FairLink>::iterator it = myLinks.begin(); it != myLinks.end(); it++) {
92  FairLink myLink = *it;
93  myLink.SetWeight(myLink.GetWeight()*mult);
94  AddLink(myLink);
95  }
96 }
97 
98 #ifndef __APPLE__
99 inline
100 #else
101 #endif
102 void FairMultiLinkedData::AddLink(FairLink link, Bool_t bypass, Float_t mult)
103 {
104 
105  Float_t weight = link.GetWeight() * mult;
106  link.SetWeight(weight);
107  //std::cout << fVerbose << std::endl;
108 
110 
111  if (ioman == 0) {
112  std::cout << "-W- no IOManager present!" << std::endl;
113  fPersistanceCheck = kFALSE;
114  }
115  if (fVerbose > 1) {
116  std::cout << "Add FairLink: " << link << std::endl;
117  }
118 
119  if (fPersistanceCheck == kFALSE ||
120  link.GetIndex() < 0 ||
121  ioman->CheckBranch(ioman->GetBranchName(link.GetType())) == 0) {
122  InsertLink(link);
123  return;
124  }
125 
126  if (bypass == kFALSE) {
127  if (fVerbose > 1) {
128  std::cout << "BranchName " << ioman->GetBranchName(link.GetType()) << " checkStatus: " << ioman->CheckBranch(ioman->GetBranchName(link.GetType())) << std::endl;
129  }
130  if (link.GetType() > ioman->GetBranchId("MCTrack") && ioman->CheckBranch(ioman->GetBranchName(link.GetType())) != 1) {
131  if (fVerbose > 1) {
132  std::cout << "BYPASS!" << std::endl;
133  }
134  bypass = kTRUE;
135  }
136  }
137 
138  if (bypass == kTRUE) {
139  //FairRootManager* ioman = FairRootManager::Instance();
140  if (link.GetType() > ioman->GetBranchId("MCTrack")) {
141  TClonesArray* array = (TClonesArray*)ioman->GetObject(ioman->GetBranchName(link.GetType()));
142  if (fVerbose > 1) {
143  std::cout << "Entries in " << ioman->GetBranchName(link.GetType()) << " Array: " << array->GetEntries() << std::endl;
144  }
145  FairMultiLinkedData* links = (FairMultiLinkedData*)array->At(link.GetIndex());
146  if (fVerbose > 1) {
147  std::cout << "FairMultiLinkedData has " << links->GetNLinks() << " Entries: " << std::endl;
148  std::cout << *links << std::endl;
149  }
150  AddLinks(*links, mult);
151  return;
152  } else {
153  InsertLink(link);
154  }
155  } else {
156  InsertLink(link);
157  }
158 
159 }
160 
162 {
163  std::set<FairLink>::iterator it = fLinks.find(link);
164  if (it != fLinks.end()) {
165  FairLink myTempLink = *it;
166  myTempLink.AddWeight(link.GetWeight());
167  fLinks.erase(it);
168  fLinks.insert(myTempLink);
169  } else {
170  fLinks.insert(link);
171  }
172  return;
173 }
174 
175 
176 Bool_t FairMultiLinkedData::IsLinkInList(Int_t type, Int_t index)
177 {
178  if (LinkPosInList(type, index) > -1) {
179  return kTRUE;
180  }
181  return kFALSE;
182 }
183 
184 Int_t FairMultiLinkedData::LinkPosInList(Int_t type, Int_t index)
185 {
186  std::set<FairLink>::iterator it = std::find(fLinks.begin(), fLinks.end(), FairLink(type, index));
187  if (it != fLinks.end()) {
188  return std::distance(fLinks.begin(), it);
189  }
190  return -1;
191 }
192 
193 void FairMultiLinkedData::DeleteLink(Int_t type, Int_t index)
194 {
195  /* Int_t pos = LinkPosInList(type, index);
196  if (pos < 0) return;
197  else{
198  fLinks.erase(fLinks.begin()+pos);
199  }*/
200 }
201 
203 {
204  FairMultiLinkedData result;
205  for (std::set<FairLink>::iterator it = fLinks.begin(); it != fLinks.end(); it++) {
206  if (it->GetType() == type) {
207  FairLink myLink = *it;
208  result.InsertLink(myLink);
209  }
210  }
211  return result;
212 }
213 
215 {
217  TString branchName = ioman->GetBranchName(myLink.GetType());
218  if (ioman->CheckBranch(branchName) > 0) {
219  TClonesArray* myArray = (TClonesArray*)ioman->GetObject(branchName);
220  if (myArray != 0) {
221  if (myArray->GetEntries() > myLink.GetIndex()) {
222  return myArray->At(myLink.GetIndex());
223  } else { std::cout << "-E- FairMultiLinkedData::GetData index out of bounds" << std::endl; }
224  } else { std::cout << "-E- FairMultiLinkedData::GetData no TClonesArray" << std::endl; }
225  } else { std::cout << "-E- FairMultiLinkedData::GetData Branch does not exist in Memory" << std::endl; }
226 
227  return 0;
228 }
229 
231 {
232  std::set<FairLink> tempLinks;
233  for (std::set<FairLink>::iterator it = fLinks.begin(); it != fLinks.end(); it++) {
234  FairLink tempLink = *it;
235  tempLink.SetWeight(weight);
236  tempLinks.insert(tempLink);
237  }
238  fLinks = tempLinks;
239 }
240 
242 {
243  std::set<FairLink> tempLinks;
244  for (std::set<FairLink>::iterator it = fLinks.begin(); it != fLinks.end(); it++) {
245  FairLink tempLink = *it;
246  tempLink.SetWeight(weight + tempLink.GetWeight());
247  tempLinks.insert(tempLink);
248  }
249  fLinks = tempLinks;
250 }
251 
253 {
254  std::set<FairLink> tempLinks;
255  for (std::set<FairLink>::iterator it = fLinks.begin(); it != fLinks.end(); it++) {
256  FairLink tempLink = *it;
257  tempLink.SetWeight(weight * tempLink.GetWeight());
258  tempLinks.insert(tempLink);
259  }
260  fLinks = tempLinks;
261 }