ARA ROOT v3.11 Software

AraWebPlotter/AraHistoHandler.cxx

00001 #include "AraHistoHandler.h"
00002 #include <ctime>
00003 #include <iostream>
00004 
00005 #define TIME_BIN_SIZE 60
00006 
00007 ClassImp(AraHistoHandler);
00008 
00009 AraHistoHandler::~AraHistoHandler()
00010 {
00011 
00012 }
00013 
00014 AraHistoHandler::AraHistoHandler(TFile *fpHist,  const char *name, const char *title )
00015 {
00016   strncpy(fName,name,179);
00017   strncpy(fTitle,title,179);
00018   //Default constructor
00019   fHistoFile=fpHist;
00020   char testString[180];
00021   if(fHistoFile->IsOpen()) {
00022     fDirectory = (TDirectory*) fHistoFile->Get(name);
00023     if(fDirectory) {
00024        fDirectory->cd();
00025       //Now try and read in all the histos
00026       for(int i=0 ; i!=fDirectory->GetListOfKeys()->GetEntries() ; i++) {
00027          TH1D *hist = (TH1D*)fDirectory->Get(fDirectory->GetListOfKeys()->At(i)->GetName());    
00028         UInt_t keyValue=0;
00029         sprintf(testString,"%s_%%u",fName);
00030         sscanf(hist->GetName(),testString,&keyValue);
00031         Int_t count=(Int_t)hist->GetBinContent(0);
00032         //      std::cerr << i << "\t" << fDirectory->GetListOfKeys()->At(i) << "\t" << fDirectory->GetListOfKeys()->At(i)->GetName() << "\t" << keyValue << "\t" << hist->GetBinContent(0) << "\n";
00033         //      hist->SetBinContent(0,0);
00034 
00035         theCountMap[keyValue]=count;
00036         hasChangedMap[keyValue]=0;
00037         theHistoMap[keyValue]=hist;
00038       }
00039     }
00040     else {
00041       fDirectory=fHistoFile->mkdir(name);
00042     }
00043   }    
00044 }
00045 
00046 void AraHistoHandler::addFile(TFile *fpNext)
00047 {
00048   //This is going to be in theory for a read only mode of operation
00049   char testString[180];
00050   if(fpNext->IsOpen()) {
00051     TDirectory *tempDir = (TDirectory*) fpNext->Get(fName);
00052     if(tempDir) {
00053       tempDir->cd();
00054       //Now try and read in all the histos
00055       for(int i=0 ; i!=tempDir->GetListOfKeys()->GetEntries() ; i++) {
00056         TH1D *hist = (TH1D*)tempDir->Get(tempDir->GetListOfKeys()->At(i)->GetName());   
00057         UInt_t keyValue=0;
00058         sprintf(testString,"%s_%%u",fName);
00059         sscanf(hist->GetName(),testString,&keyValue);
00060         Int_t count=(Int_t)hist->GetBinContent(0);
00061 
00062         //Now we need to check if we have this key or not
00063 
00064         histoMap::iterator it=theHistoMap.find(keyValue);
00065         countMap::iterator countIt=theCountMap.find(keyValue);
00066         if(it==theHistoMap.end()) {
00067           //Then we don't have this key
00068           //     std::cerr << "Making " << histName << "\n";
00069            fDirectory->cd();
00070            TH1D *firstHist = (TH1D*) hist->Clone(testString);
00071            theHistoMap[keyValue]=firstHist;
00072            theCountMap[keyValue]=count;
00073            hasChangedMap[keyValue]=1;
00074            tempDir->cd();
00075         }
00076         else {
00077           countIt->second+=count;
00078           it->second->Add(hist);          
00079         }
00080       }
00081     }
00082   }
00083 }
00084 
00085 void AraHistoHandler::Write()
00086 {
00087   fDirectory->cd();
00088   for(histoMap::iterator it=theHistoMap.begin();it!=theHistoMap.end();it++) {
00089     UInt_t keyValue=it->first;
00090     //Check to see if we have actually chnaged anything
00091     if(hasChangedMap[keyValue]) {
00092       Int_t count=theCountMap[keyValue];
00093       //    std::cout << "Count for " << fName << "\t" << keyValue << "\t" << count << "\t" << (it->second)->GetName() << "\n" ;
00094       (it->second)->SetBinContent(0,count); //Dodgy use of underflow bin
00095       //    std::cout << (it->second)->GetBinContent(0) << "\n";
00096       
00097       (it->second)->Write(0,TObject::kWriteDelete);
00098       hasChangedMap[keyValue]=0;
00099     }
00100     
00101   }
00102  
00103 }
00104 
00105 
00106 void AraHistoHandler::addHisto(UInt_t unixTime, TH1D *histo)
00107 {
00108   //  std::cout << unixTime << "\t" << histo << "\n";
00109   char histName[180];
00110   UInt_t keyValue=unixTime/TIME_BIN_SIZE;  
00111   sprintf(histName,"%s_%u",this->GetName(),keyValue);
00112   histoMap::iterator it=theHistoMap.find(keyValue);
00113   countMap::iterator countIt=theCountMap.find(keyValue);
00114   if(it==theHistoMap.end()) {
00115     //This is the first entry
00116     //     std::cerr << "Making " << histName << "\n";
00117     TH1D *firstHist = (TH1D*) histo->Clone(histName);
00118     theHistoMap[keyValue]=firstHist;
00119     theCountMap[keyValue]=1;
00120     hasChangedMap[keyValue]=1;
00121     
00122   }
00123   else {
00124     countIt->second+=1;
00125     it->second->Add(histo);
00126     hasChangedMap[keyValue]=1;
00127     //    std::cerr << "Pre\n";
00128     //    std::cerr << "Adding to " <<  histName << "\t" << theCountMap[keyValue] << "\n";
00129     
00130   }
00131 }
00132 
00133 TH1D *AraHistoHandler::getTimeHisto(UInt_t firstTime, UInt_t lastTime)
00134 {
00135   if(theHistoMap.size()==0) return NULL;  
00136   histoMap::iterator firstIt=theHistoMap.lower_bound(firstTime/TIME_BIN_SIZE);
00137   if(firstIt==theHistoMap.end()) return NULL;
00138   histoMap::iterator lastIt=theHistoMap.upper_bound(lastTime/TIME_BIN_SIZE); 
00139   if(lastIt==theHistoMap.end()) lastIt--; //Decrement if we have already reached the end
00140 
00141   Int_t numInAverage=0;
00142   TH1D *outputHisto=NULL;
00143   char histName[180];
00144   sprintf(histName,"%s_temp",this->GetName());
00145   
00146   lastIt++; //To include lastIt
00147   for(histoMap::iterator it=firstIt;it!=lastIt;it++) {
00148     //    std::cout << (it->first) << "\t" << (it->second).second << "\n";
00149     UInt_t keyValue=it->first;
00150     TH1D *hist=(it->second);
00151     numInAverage+=theCountMap[keyValue];
00152     if(!outputHisto) {
00153       outputHisto = (TH1D*) hist->Clone(histName);
00154     }
00155     else {
00156       outputHisto->Add(hist);
00157     }
00158   }
00159 
00160   Double_t scaleFactor=1./numInAverage;
00161   outputHisto->Scale(scaleFactor);
00162 
00163   return outputHisto;
00164 
00165 }
00166 
00167 TH1D *AraHistoHandler::getTimeHisto(AraPlotTime::AraPlotTime_t plotTime)
00168 {
00169 
00170   if(theHistoMap.size()==0) return NULL;  
00171   UInt_t lastTime=getLastTime();
00172  // std::cout << this->GetName() << "\t" << plotTime << "\t" << getStartTime(lastTime,plotTime) << "\t" << lastTime << "\n";
00173   return getTimeHisto(AraPlotTime::getStartTime(lastTime,plotTime),lastTime);
00174 }
00175 
00176 
00177 TH1D *AraHistoHandler::getCurrentTimeHisto(AraPlotTime::AraPlotTime_t plotTime)
00178 {
00179 
00180   if(theHistoMap.size()==0) return NULL;  
00181   UInt_t lastTime=time(NULL);
00182   return getTimeHisto(AraPlotTime::getStartTime(lastTime,plotTime),lastTime);
00183 }
00184 
00185 UInt_t AraHistoHandler::getLastTime()
00186 {
00187    if(theHistoMap.size()==0) return 0;  
00188    histoMap::iterator lastIt=theHistoMap.end(); //One past the end of the amp
00189    lastIt--;//end of map  
00190    return TIME_BIN_SIZE*(lastIt->first);
00191 
00192 }
00193 
00194 
00195 
00196 TH2D *AraHistoHandler::getTimeColourHisto(UInt_t firstTime, UInt_t lastTime,Int_t numPoints)
00197 {
00198 
00199   if(theHistoMap.size()==0) return NULL;  
00200   histoMap::iterator firstIt=theHistoMap.lower_bound(firstTime/TIME_BIN_SIZE);
00201   if(firstIt==theHistoMap.end()) return NULL;
00202   histoMap::iterator lastIt=theHistoMap.upper_bound(lastTime/TIME_BIN_SIZE); 
00203   if(lastIt==theHistoMap.end()) lastIt--; //Decrement if we have already reached the end
00204   
00205   UInt_t firstKey=firstIt->first;
00206   UInt_t lastKey=lastIt->first;
00207   UInt_t stepSize=(lastKey-firstKey)/(numPoints);
00208   //  if(stepSize==0) 
00209   stepSize++;
00210 
00211   Int_t safetyFactor=10;
00212   TH1D **sliceHistos = new TH1D*[numPoints+safetyFactor];
00213   Double_t *numEnts = new Double_t[numPoints+safetyFactor];
00214   for(int i=0;i<numPoints+safetyFactor;i++) {
00215     sliceHistos[i]=NULL;
00216     numEnts[i]=0;
00217   }
00218   
00219   
00220   char histName[180];
00221   
00222   lastIt++; //To include lastIt
00223   Int_t yBins=0;
00224   Double_t minY=0;
00225   Double_t maxY=0;
00226   for(histoMap::iterator it=firstIt;it!=lastIt;it++) {
00227     //    std::cout << (it->first) << "\t" << (it->second).second << "\n";
00228     UInt_t keyValue=it->first;
00229     Int_t bin=(keyValue-firstKey)/stepSize;    
00230     TH1D *hist=(it->second);
00231     numEnts[bin]+=theCountMap[keyValue];
00232     if(!sliceHistos[bin]) {
00233       sprintf(histName,"%s_temp_%d",this->GetName(),bin);
00234       sliceHistos[bin] = (TH1D*) hist->Clone(histName);
00235       if(!yBins) {
00236         yBins=sliceHistos[bin]->GetNbinsX();
00237         minY=sliceHistos[bin]->GetBinLowEdge(1);
00238         maxY=sliceHistos[bin]->GetBinLowEdge(yBins)+sliceHistos[bin]->GetBinWidth(yBins);
00239       }
00240     }
00241     else {
00242       sliceHistos[bin]->Add(hist);
00243     }
00244   }
00245   Double_t minTime=(firstKey-0.5)*TIME_BIN_SIZE;
00246   Double_t maxTime=(lastKey+0.5)*TIME_BIN_SIZE;
00247   sprintf(histName,"%s_output2d",this->GetName());
00248   TH2D *outputHisto= new TH2D(histName,histName,numPoints,minTime,maxTime,yBins,minY,maxY);
00249   for(int i=0;i<numPoints;i++) {
00250     Double_t time=(firstKey+(i*stepSize))*TIME_BIN_SIZE;
00251     if(sliceHistos[i]) {
00252       Double_t scaleFactor=1./numEnts[i];
00253       sliceHistos[i]->Scale(scaleFactor);
00254       for(int biny=1;biny<sliceHistos[i]->GetNbinsX();biny++) {
00255         Double_t yVal=sliceHistos[i]->GetBinCenter(biny);
00256         Double_t zVal=sliceHistos[i]->GetBinContent(biny);
00257         outputHisto->Fill(time,yVal,zVal);
00258       }
00259       delete sliceHistos[i];
00260     }
00261   }
00262 
00263   return outputHisto;
00264   
00265 
00266 }
00267 
00268 
00269 TH2D *AraHistoHandler::getTimeColourHisto(AraPlotTime::AraPlotTime_t plotTime, Int_t numPoints)
00270 {
00271   if(theHistoMap.size()==0) return NULL;  
00272   UInt_t lastTime=getLastTime();
00273  // std::cout << this->GetName() << "\t" << plotTime << "\t" << getStartTime(lastTime,plotTime) << "\t" << lastTime << "\n";
00274   return getTimeColourHisto(AraPlotTime::getStartTime(lastTime,plotTime),lastTime,numPoints);
00275 
00276 }
00277 
00278 TH2D *AraHistoHandler::getCurrentTimeColourHisto(AraPlotTime::AraPlotTime_t plotTime, Int_t numPoints)
00279 {
00280 
00281   if(theHistoMap.size()==0) return NULL;  
00282   UInt_t lastTime=time(NULL);
00283   return getTimeColourHisto(AraPlotTime::getStartTime(lastTime,plotTime),lastTime,numPoints);
00284 }

Generated on Fri Jul 26 15:27:35 2013 for ARA ROOT v3.11 Software by doxygen 1.4.7