00001 #ifndef madchain_cxx
00002 #define madchain_cxx
00003 #include <iostream>
00004
00005 #include "Mad/MadChain.h"
00006
00007
00008
00009 MadChain::MadChain(TChain *chainSR,TChain *chainMC,
00010 TChain *chainTH,TChain *chainEM, bool build_lookup)
00011 : fLookupBuilt(false)
00012 {
00013
00014 Init(chainSR,chainMC,chainTH,chainEM);
00015 if(build_lookup) BuildLookup();
00016 }
00017
00018 MadChain::~MadChain()
00019 {
00020 if(!fChainSR) return;
00021 delete fChainSR->GetCurrentFile();
00022 if(fChainEM) delete fChainEM->GetCurrentFile();
00023 if(!fChainMC) return;
00024 delete fChainMC->GetCurrentFile();
00025 if(!fChainTH) return;
00026 delete fChainTH->GetCurrentFile();
00027 }
00028
00029 Int_t MadChain::GetEntryNumber(Int_t run,Int_t snarl)
00030 {
00031 if(!fLookupBuilt) return fChainSR->GetEntryNumberWithIndex(run,snarl);
00032 else{
00033 std::pair<Int_t,Int_t> p(run,snarl);
00034 std::map< std::pair<Int_t,Int_t> , Long64_t>::const_iterator it
00035 = fLookUp.find(p);
00036 if(it==fLookUp.end()) return -1;
00037 else return it->second;
00038 }
00039 }
00040
00041 Int_t MadChain::Get(Int_t entry)
00042 {
00043
00044
00045 if(entry<fCurrentSR||fCurrentSR==-1) {
00046 fCurrentMC=entry;
00047 fCurrentTH=entry;
00048 fCurrentEM=entry;
00049 }
00050 else if(entry>fCurrentSR) {
00051 fCurrentMC+=(entry-fCurrentSR);
00052 fCurrentTH+=(entry-fCurrentSR);
00053 fCurrentEM+=(entry-fCurrentSR);
00054 }
00055
00056
00057 if (!fChainSR) return 0;
00058 Int_t status = fChainSR->GetEntry(entry);
00059 fCurrentSR = entry;
00060
00061 if(isEM) {
00062 fChainEM->GetEntry(fCurrentEM);
00063 int xtra = 0;
00064 while(Record->GetHeader().GetSnarl()!=emRecord->GetHeader().GetSnarl()){
00065 xtra+=1;
00066 fChainEM->GetEntry(fCurrentEM+xtra);
00067 }
00068 fCurrentEM+=xtra;
00069 }
00070
00071 if(isMC) {
00072 fChainMC->GetEntry(fCurrentMC);
00073 int xtra = 0;
00074 while(Record->GetHeader().GetSnarl()!=mcRecord->GetHeader().GetSnarl()){
00075 xtra+=1;
00076 fChainMC->GetEntry(fCurrentMC+xtra);
00077 }
00078 fCurrentMC+=xtra;
00079 }
00080
00081 if(isTH) {
00082 fChainTH->GetEntry(fCurrentTH);
00083 int xtra = 0;
00084 while(Record->GetHeader().GetSnarl()!=thRecord->GetHeader().GetSnarl()){
00085 xtra+=1;
00086 fChainTH->GetEntry(fCurrentTH+xtra);
00087 }
00088 fCurrentTH+=xtra;
00089 }
00090 return status;
00091 }
00092
00093 void MadChain::Init(TChain *chainSR,TChain *chainMC,
00094 TChain *chainTH,TChain *chainEM)
00095 {
00096
00097 Zero();
00098
00099 if(chainSR==0) {
00100 std::cerr << "No file added" << std::endl;
00101 return;
00102 }
00103 fChainSR = chainSR;
00104 if(strcmp(fChainSR->GetName(),"NtpSR")==0){
00105 fChainSR->SetBranchAddress("NtpSRRecord",&Record);
00106 }
00107 else if(strcmp(fChainSR->GetName(),"NtpSt")==0){
00108 isST = true;
00109 fChainSR->SetBranchAddress("NtpStRecord",&stRecord);
00110 }
00111 fCurrentSR = -1;
00112 Nentries = Int_t(fChainSR->GetEntries());
00113
00114
00115 if(chainEM==0) {
00116 isEM = false;
00117 }
00118 else {
00119 fChainEM = chainEM;
00120 fChainEM->SetBranchAddress("NtpEMRecord",&emRecord);
00121 fCurrentEM = -1;
00122 }
00123
00124 if(chainMC==0) {
00125 isMC = false;
00126 isTH = false;
00127 return;
00128 }
00129 fChainMC = chainMC;
00130 fChainMC->SetBranchAddress("NtpMCRecord",&mcRecord);
00131 fCurrentMC = -1;
00132
00133 if(chainTH==0) {
00134 isTH = false;
00135 return;
00136 }
00137 fChainTH = chainTH;
00138 fChainTH->SetBranchAddress("NtpTHRecord",&thRecord);
00139 fCurrentTH = -1;
00140
00141 }
00142
00143 void MadChain::Show(Int_t entry)
00144 {
00145
00146
00147 if (!fChainSR) return;
00148 fChainSR->Show(entry);
00149 if (fChainEM) fChainEM->Show(entry);
00150 if (!fChainMC) return;
00151 fChainMC->Show(entry);
00152 if (!fChainTH) return;
00153 fChainTH->Show(entry);
00154 }
00155
00156 void MadChain::Zero(){
00157
00158 Record = 0;
00159 stRecord = 0;
00160 emRecord = 0;
00161 mcRecord = 0;
00162 thRecord = 0;
00163
00164 isMC = true;
00165 isTH = true;
00166 isEM = true;
00167 isST = false;
00168 Nentries = -1;
00169
00170 fChainSR = 0;
00171 fChainMC = 0;
00172 fChainTH = 0;
00173 fChainEM = 0;
00174
00175 fCurrentSR = 0;
00176 fCurrentMC = 0;
00177 fCurrentTH = 0;
00178 fCurrentEM = 0;
00179
00180 }
00181
00182 void MadChain::BuildLookup(){
00183
00184
00185
00186 if(!fChainSR) return;
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 Long64_t N = fChainSR->GetEntries();
00217 Int_t nbytes=0;
00218 std::cout<<"MadChain building lookup table"<<std::endl;
00219
00220 for(Long64_t i = 0; i<N; i++){
00221 std::pair<Int_t,Int_t> p;
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 nbytes+=fChainSR->GetEntry(i);
00235
00236 if(isST) {
00237 p.first = stRecord->GetHeader().GetRun();
00238 p.second = stRecord->GetHeader().GetSnarl();
00239 }
00240 else{
00241 p.first = Record->GetHeader().GetRun();
00242 p.second = Record->GetHeader().GetSnarl();
00243 }
00244
00245
00246
00247
00248
00249
00250 fLookUp.insert( std::pair< std::pair<Int_t,Int_t>, Long64_t>(p, i) );
00251 if(i%10000==0){
00252 std::cout<<"progress: "<<i<<" events and ";
00253 std::cout.precision(3);
00254 std::cout<<(nbytes/1024.0)<<" kb read \r";
00255 std::cout.flush();
00256 }
00257
00258 }
00259 std::cout<<"Done "<<std::endl;
00260 std::cout<<"lookup table has "<<fLookUp.size()<<" entries"<<std::endl;
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274 fChainSR->SetBranchStatus("*",1);
00275 if(isST) {
00276 fChainSR->SetBranchAddress("NtpStRecord",&stRecord);
00277 }
00278 else{
00279 fChainSR->SetBranchAddress("NtpSRRecord",&Record);
00280 }
00281
00282
00283 fLookupBuilt=true;
00284 }
00285
00286 #endif // #ifdef madchain_cxx