/* PlugStripColl This class holds the contents of all the non-zero PlugStrips after conversion to floats. // A PlugStripColl can be puffed or unpuffed, not both. The state is signalled // by the _isPuffed variable. When it is puffed, only _container has valid // information. When it is unpuffed, only _address, _energy, _counts, and // _bufferSize have valid information. This could probably be done more // intelligently. \author Benn Tannenbaum benn@physics.ucla.edu */ #include "CalorObjects/PlugStripColl.hh" //------------------------------- // Collaborating Class Headers -- //------------------------------- #include "Experiment/Experiment.hh" #include "AbsEnv/AbsEnv.hh" #include "ErrorLogger_i/gERRLOG.hh" // ROOT Headers : #include "TBuffer.h" #include "EdmUtilities/CdfClassImp.hh" // Constructors leave PlugStripColl in puffed state. PlugStripColl::PlugStripColl() : StorableObject() , _address(0) , _energy(0) , _counts(0) , _bufferSize(0) , _isPuffed(true) {} PlugStripColl::PlugStripColl( const PlugStripColl& rhs) : StorableObject(rhs) , _container(rhs._container) , _address(rhs._address) , _energy(rhs._energy) , _counts(rhs._counts) , _bufferSize(rhs._bufferSize) , _isPuffed(rhs._isPuffed) {} //----------------------------------------------------------------------------- // Destruction //----------------------------------------------------------------------------- PlugStripColl::~PlugStripColl() {} void PlugStripColl::destroy() { PlugStripColl::deallocate(); delete this; } void PlugStripColl::deallocate(){ deleteArrays(); StorableObject::deallocate(); } void PlugStripColl::deleteArrays() { // Avoid memory leaks! if (_address != 0) { delete[] _address ; _address = 0 ; } if (_energy != 0) { delete[] _energy; _energy = 0 ; } if (_counts != 0) { delete[] _counts; _counts = 0 ; } } //----------------------------------------------------------------------------- // EDM: functions required by StorableObject //----------------------------------------------------------------------------- std::string PlugStripColl::class_name() const { return PlugStripColl::Class_Name(); } Version_t PlugStripColl::class_version() const { return PlugStripColl::Class_Version(); } void PlugStripColl::print(std::ostream& os) const { StorableObject::print(os); _container.print(os); } bool PlugStripColl::postread(EventRecord* record) {return true;} bool PlugStripColl::prewrite(EventRecord* record) {return true;} bool PlugStripColl::activate(EventRecord* record) {return true;} bool PlugStripColl::deactivate(EventRecord* record) {return true;} void PlugStripColl::Streamer(TBuffer& iobuffer) { // Leaves PlugStripColl in unpuffed state. unsigned int start = 0 ; unsigned int byte_count = 0 ; //-------------------------------------------------------------------------- // Read object from buffer //-------------------------------------------------------------------------- if (iobuffer.IsReading()) { Version_t original_version = iobuffer.ReadVersion(&start, &byte_count) ; StorableObject::Streamer(iobuffer) ; if (original_version == 1) { _container.Streamer(iobuffer); } else if (original_version == 2) { readFromBuffer(iobuffer); } else { ERRLOG( ELerror, "Unsupported PlugStripColl version" ) << "@SUB=PlugStripColl::Streamer()" << "PlugStripColl cannot be read." << endmsg; } iobuffer.CheckByteCount(start, byte_count, PlugStripColl::IsA()) ; } //-------------------------------------------------------------------------- // Write object to buffer //-------------------------------------------------------------------------- else if (iobuffer.IsWriting()) { byte_count = iobuffer.WriteVersion(PlugStripColl::IsA(), kTRUE) ; StorableObject::Streamer(iobuffer) ; writeToBuffer(iobuffer); iobuffer.SetByteCount(byte_count, kTRUE) ; } //-------------------------------------------------------------------------- // Unanticipated action //-------------------------------------------------------------------------- else { ERRLOG( ELerror, "PlugStripColl::Streamer()" ) << "@SUB=PlugStripColl::Streamer" << "NOTHING DONE." << endmsg; } } // functions to do writing & reading of event void PlugStripColl::writeToBuffer(TBuffer &iobuffer) { // Modified EMB Apr 22 2003 // Leaves PlugStripColl in unpuffed state. // First unpuff, if it isn't already... if (_isPuffed) unpuffEvent(); // Stream out the number of elements in the arrays to follow: iobuffer << _bufferSize; // Stream out the arrays themselves: iobuffer.WriteFastArray(_address, _bufferSize); iobuffer.WriteFastArray(_energy, _bufferSize); iobuffer.WriteFastArray(_counts, _bufferSize); return; } void PlugStripColl::readFromBuffer(TBuffer &iobuffer) { // I split out the bit that fills the contents from the arrays // to speed up the reading. that way, if you don't want to look at the // PES, you don't have to pay the price in speed. // Benn Tannenbaum 3 July 2002 // Modified EMB Apr 22 2003 // Leaves PlugStripColl in unpuffed state. // Protect against memory leaks: deleteArrays(); // Read in the number of elements in the arrays to follow: iobuffer >> _bufferSize; // Set up raw information arrays: _address = new uint2[_bufferSize]; _energy = new float4[_bufferSize]; _counts = new uint2[_bufferSize]; // Stream in the raw information: iobuffer.ReadFastArray(_address, _bufferSize); iobuffer.ReadFastArray(_energy, _bufferSize); iobuffer.ReadFastArray(_counts, _bufferSize); // Make sure puffed information is null: _container.clear(); _isPuffed = false; return; } void PlugStripColl::puffEvent() const { // Benn Tannenbaum 3 July 2002 // this is called from the PuffModule if one wants PES info // Modified EMB Apr 22 2003 // Leaves PlugStripColl in puffed state. // If it has already been puffed, don't do anything here! if (_isPuffed) return; // Generate PlugStrip objects from the raw information: for (int i = 0; i < _bufferSize; i++) { PlugStrip tmpStrip(_address[i], _counts[i], _energy[i]); _container.contents().push_back(tmpStrip); } // Can't do this since it's a const method. But beware, only _container // has valid information now! // // Now that it's puffed, get rid of the raw information: // _bufferSize = 0; // deleteArrays(); // Now it has been puffed! _isPuffed = true; return; } void PlugStripColl::unpuffEvent() { // Added EMB Apr 22 2003 // Leaves PlugStripColl in unpuffed state. // If it's not puffed, don't do anything here! if (!_isPuffed) return; // Protect against memory leaks: deleteArrays(); // Set up raw information arrays: _bufferSize = contents().size(); _address = new uint2[_bufferSize]; _energy = new float4[_bufferSize]; _counts = new uint2[_bufferSize]; // Convert PlugStrips to raw information: for (int i = 0; i < _bufferSize; ++i) { _address[i] = contents()[i].PesLocation(); _energy[i] = contents()[i].PesEnergy(); _counts[i] = contents()[i].PesCounts(); } // Get rid of puffed information: _container.clear(); // Now it's unpuffed! _isPuffed = false; return; } //----------------------------------------------------------------------------- // Functions to retrieve the collection from the event //----------------------------------------------------------------------------- PlugStripColl::Error PlugStripColl::find(PlugStripColl_ch& cHandle) { EventRecord::ConstIterator ciEvent(AbsEnv::theEvent(),"PlugStripColl"); if (ciEvent.is_valid()) { cHandle = ciEvent; return OK; } else { cHandle.set_null(); return ERROR; } } PlugStripColl::Error PlugStripColl::find(PlugStripColl_ch& cHandle, const StorableObject::Selector & selector) { EventRecord::ConstIterator ciEvent(AbsEnv::theEvent(),"PlugStripColl"); while (ciEvent.is_valid() && !selector(*ciEvent)) { ++ciEvent; } if (ciEvent.is_valid()) { cHandle = ciEvent; return OK; } else { cHandle.set_null(); return ERROR; } } PlugStripColl::Error PlugStripColl::find(PlugStripColl_ch& cHandle, const std::string& description) { AbsEvent* theEvent = AbsEnv::theEvent(); StorableObject::SelectByDescription selector( description ); EventRecord::ConstIterator iter( theEvent, selector ); if(iter.is_invalid()) return ERROR; cHandle = PlugStripColl_ch(iter); return OK; } PlugStripColl* PlugStripColl::clone(void) { return new PlugStripColl (*this); } //============================================================================= // ROOT I/O Hook //============================================================================= CdfClassImp(PlugStripColl)