Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MadChain.cxx

Go to the documentation of this file.
00001 #ifndef madchain_cxx
00002 #define madchain_cxx
00003 #include <iostream>
00004 //root includes
00005 #include "Mad/MadChain.h"
00006 
00007 //ClassImp(MadChain);
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   //estimate MC,TH entry numbers
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   // Read contents of entry.
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;  //just in case tree entries are not correlated
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;  //just in case tree entries are not correlated
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;  //just in case tree entries are not correlated
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   //chainSR->BuildIndex("fHeader.fRun","fHeader.fSnarl");
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 // Print contents of entry.
00146 // If entry is not specified, print current entry
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   // fill fLookUp with run,snarl of each entry in the chain
00184   // do it as efficiently as possible by only reading the header
00185 
00186   if(!fChainSR) return;
00187 
00188   /*
00189   RecRecordImp<RecCandHeader>* recrecord = 0;
00190   //  fChainSR->SetBranchAddress(fChainSR->GetListOfBranches()->At(0)->GetName(),
00191   //                         &recrecord);
00192   fChainSR->SetBranchAddress("NtpStRecord",
00193                              &recrecord);
00194   fChainSR->SetBranchStatus("*",0);
00195   fChainSR->SetBranchStatus("fHeader.*",1);
00196   fChainSR->SetBranchAddress("NtpStRecord",
00197                              &recrecord);
00198   */
00199   /*
00200   fChainSR->SetBranchStatus("fHeader.fRun",1);  
00201   fChainSR->SetBranchStatus("fHeader.fSnarl",1);  
00202   Int_t run, snarl;
00203   if(isST) {
00204     fChainSR->SetBranchAddress("NtpStRecord",&stRecord);
00205     fChainSR->SetBranchAddress("fHeader.fRun",&run);
00206     fChainSR->SetBranchAddress("fHeader.fSnarl",&snarl);
00207   }
00208   else{
00209     fChainSR->SetBranchAddress("NtpSRRecord",&Record);
00210     fChainSR->SetBranchAddress("fHeader.fRun",&run);
00211     fChainSR->SetBranchAddress("fHeader.fSnarl",&snarl);
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   //  Int_t current_tree=-1; //fChainSR->GetTreeNumber();
00220   for(Long64_t i = 0; i<N; i++){
00221     std::pair<Int_t,Int_t> p;
00222     /* // this trash doesn't work
00223     // mock up what MakeClass generated code does
00224     Long64_t centry = fChainSR->LoadTree(i);
00225     if(fChainSR->GetTreeNumber() != current_tree){
00226       current_tree=fChainSR->GetTreeNumber();
00227       fChainSR->SetBranchStatus("*",0);
00228       //      fChainSR->SetBranchStatus("fHeader.*",1);
00229       //      fChainSR->SetBranchAddress("NtpStRecord", &recrecord);
00230     }    
00231     nbytes+=fChainSR->GetEntry(centry);
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     //    p.first=run; p.second=snarl;
00246     //    if(i%1000==0) recrecord->GetHeader().Print();
00247     // p.first = recrecord->GetHeader().GetRun();
00248     // p.second = recrecord->GetHeader().GetSnarl();
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     //    recrecord->Clear();
00258   }
00259   std::cout<<"Done                                                "<<std::endl;
00260   std::cout<<"lookup table has "<<fLookUp.size()<<" entries"<<std::endl;
00261 
00262   /*
00263   std::map< std::pair<Int_t,Int_t> , Long64_t>::const_iterator it
00264     =fLookUp.begin();
00265   int cntr=0;
00266   for(; it!=fLookUp.end(); it++){
00267     if(cntr%1000==0){      
00268       std::cout<<cntr<<" "<<it->first.first<<" "
00269                <<it->first.second<<" "<<it->second<<std::endl;
00270     }
00271     cntr++;
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

Generated on Mon Feb 15 11:06:54 2010 for loon by  doxygen 1.3.9.1